spring cloud學(xué)習(xí)入門之config配置教程
前言
本文主要給大家分享了關(guān)于spring cloud的入門教程,主要介紹了config配置的相關(guān)內(nèi)容,下面話不多說了,來一起看看看詳細(xì)的介紹吧。
簡介
Spring cloud config 分為兩部分 server client
- config-server 配置服務(wù)端,服務(wù)管理配置信息
- config-client 客戶端,客戶端調(diào)用server端暴露接口獲取配置信息
config-server
創(chuàng)建config-server
首先創(chuàng)建config-server工程.
文件結(jié)構(gòu):
├── config-server.iml ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── lkl │ │ └── springcloud │ │ └── config │ │ └── server │ │ └── Application.java │ └── resources │ ├── application.properties │ └── bootstrap.properties └── test └── java
pom.xml內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.lkl.springcloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--表示為web工程--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--暴露各種指標(biāo)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
創(chuàng)建啟動類
Application.Java
package com.lkl.springcloud.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liaokailin on 16/4/28.
*/
@Configuration
@EnableAutoConfiguration
@RestController
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其中 @EnableConfigServer為關(guān)鍵注解
在resources文件下創(chuàng)建application.properties
server.port=8888
配置工程監(jiān)聽端口8888,默認(rèn)情況下client通過讀取http://localhost:8888獲取配置信息
創(chuàng)建bootstrap.properties
spring.cloud.config.server.git.uri: https://github.com/liaokailin/config-repo
該配置信息通過fork https://github.com/spring-cloud-samples/config-repo (本地下載)得到
通過spring.cloud.config.server.Git.uri指定配置信息存儲的git地址
運行config-server
spring boot工程很方便啟動,運行Application.java即可
獲取git上的資源信息遵循如下規(guī)則:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application:表示應(yīng)用名稱,在client中通過spring.config.name配置
profile:表示獲取指定環(huán)境下配置,例如開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境 默認(rèn)值default,實際開發(fā)中可以是 dev、test、demo、
production等
label: git標(biāo)簽,默認(rèn)值master
如果application名稱為foo,則可以采用如下方式訪問:
http://localhost:8888/foo/default
http://localhost:8888/foo/development
只要是按照上面的規(guī)則配置即可訪問.
config-client
創(chuàng)建config-client
目錄結(jié)構(gòu)如下:
├── pom.xml ├── spring-cloud-config-client.iml └── src ├── main │ ├── java │ │ └── com │ │ └── lkl │ │ └── springcloud │ │ └── config │ │ └── client │ │ └── Application.java │ └── resources │ └── bootstrap.yml └── test └── java
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lkl.springcloud</groupId> <artifactId>spring-cloud-config-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>1.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
創(chuàng)建啟動類Application.java
package com.lkl.springcloud.config.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liaokailin on 16/4/28.
*/
@SpringBootApplication
@RestController
public class Application {
@Value("${name:World!}")
String bar;
@RequestMapping("/")
String hello() {
return "Hello " + bar + "!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
創(chuàng)建bootstrap.properties文件,其內(nèi)容如下:
spring.application.name: foo spring.cloud.config.env:default spring.cloud.config.label:master spring.cloud.config.uri:http://localhost:8888
其中 spring.application.name 為應(yīng)用名稱,spring.cloud.config.uri 配置config-server暴露的獲取配置接口,其默認(rèn)值為http://localhost:8888
第二三項配置在前面已經(jīng)提到過,配置的都為默認(rèn)值,因此bootstrap.properties只需要配置應(yīng)用名即可.
訪問 http://localhost 得到 `Hello liaokailin` 獲取到git上的配置信息
訪問 http://localhost/env 得到所有的配置信息,可以發(fā)現(xiàn)獲取配置信息成功.
{
profiles: [ ],
configService:https://github.com/liaokailin/config-repo/foo.properties: {
name: "liaokailin",
foo: "devoxxfr"
},
configService:https://github.com/liaokailin/config-repo/application.yml: {
info.description: "Spring Cloud Samples--lkl",
info.url: "https://github.com/spring-cloud-samples",
eureka.client.serviceUrl.defaultZone: "http://localhost:8761/eureka/"
},
commandLineArgs: {
spring.output.ansi.enabled: "always"
},
servletContextInitParams: { },
...}
ok ~ it's work ! more about is here(本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java實現(xiàn)的上傳并壓縮圖片功能【可等比例壓縮或原尺寸壓縮】
這篇文章主要介紹了Java實現(xiàn)的上傳并壓縮圖片功能,可實現(xiàn)圖片的等比例壓縮或原尺寸壓縮,涉及java文件讀寫、轉(zhuǎn)換、傳輸?shù)认嚓P(guān)操作技巧,需要的朋友可以參考下2018-07-07
Java中的system.getProperty()的作用及使用方法
System.getProperty()?方法用于獲取系統(tǒng)屬性的值,該方法接受一個字符串參數(shù),表示要獲取的系統(tǒng)屬性的名稱,返回值為字符串類型,表示該屬性的值,接下來通過本文給大家介紹Java中的system.getProperty()的作用及使用方法,感興趣的朋友跟隨小編一起看看吧2023-05-05
spring cloud學(xué)習(xí)入門之config配置教程
這篇文章主要給大家介紹了關(guān)于spring cloud學(xué)習(xí)入門之config配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring cloud具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
SpringBoot借助spring.factories文件跨模塊實例化Bean
這篇文章主要介紹了SpringBoot借助spring.factories文件跨模塊實例化Bean,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04
spring boot中使用RabbitMQ routing路由詳解
本篇文章主要介紹了spring boot中使用RabbitMQ routing路由詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
idea install 時提示jdk的某個jar包的包不存在的問題
這篇文章主要介紹了idea install 時提示jdk的某個jar包的包不存在的問題,本文給大家分享解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

