- SpringCloud分佈式配置中心
SpringCloud Config爲微服務架構中的微服務提供集中心化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一箇中心化的外部配置。
它與Eureka一樣分爲服務端和客戶端
服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端獲取和加載配置信息,加密/解密信息等訪問接口。
客戶端則是通過指定的配置中心來管理應用資源,以及業務相關的配置內容,並在啓動的時候從配置中心獲取並配置信息,配置服務器默認從git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端來方便的管理和訪問配置內容。
-
- 創建一個git倉庫
-
- 本地新建倉庫並克隆git上剛纔創建的倉庫
-
- 本地倉庫中新建application.yml並上傳
文件內容
spring: profiles: active: - dev --- spring: profiles: dev #開發環境 application: name: microservicecloud-config-shun-dev --- spring: profiles: test #測試環境 application: name: microservicecloud-config-shun-test #請保存爲UTF-8格式
|
上傳
遠程倉庫中
-
- SpringCloud config服務端與github遠程通信
其實就是使用SpringCloud從github遠程倉庫讀取配置文件
-
-
- 父工程下新建maven module
-
-
-
- pom文件
-
<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>czs</groupId> <artifactId>microserviceone</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microserviceone-serverconfig-3344</artifactId>
<dependencies> <!-- springCloud Config服務端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.10.0.201712302008-r</version> </dependency> <!-- 圖形化監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔斷 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
|
|
-
-
- 啓動類-------注意@EnableConfigServer註解
-
package shun.cloud.config;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
/** * @author czs * @version 創建時間:2018年8月27日 下午9:57:15 */ @EnableConfigServer // 服務端,要與github通信就必須開啓 @SpringBootApplication public class ConfigApp_3344 {
public static void main(String[] args) { SpringApplication.run(ConfigApp_3344.class, args); }
}
|
-
-
- yml配置文件
-
server: port: 3344
spring: application: name: microservicecloud-config cloud: config: server: git: uri: [email protected]:chenzongshun/microserviceConfigOne.git #GitHub上面的git倉庫名字 |
-
-
- 訪問測試
-
Git上的配置
訪問到的配置
訪問方式
http://127.0.0.1:3344/application-dev.yml
http://127.0.0.1:3344/master/application-test.yml
http://127.0.0.1:3344/application/dev/master
-
- SpringCloud config客戶端配置與測試
即客戶端通過服務端獲得github上的配置
-
-
- 本地倉庫新建microservicecloud-config-client.yml並上傳
-
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ |
-
-
- 新建microserviceone-clientconfig-3355項目
-
-
-
- pom文件
-
<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>czs</groupId> <artifactId>microserviceone</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microserviceone-clientconfig-3355</artifactId> <dependencies> <!-- SpringCloud Config客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
|
-
-
- bootstrap.yml與application.yml
-
Github上面的配置文件名字---->下面兩個yml會用到
Bootstarp.yml--------->注意紅色字體
spring: cloud: config: name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒有yml後綴名,如果項目有application.yml,那麼微服務名稱就必須要是這個名字 profile: test #本次訪問的配置項 label: master uri: http://localhost:3344 #本微服務啓動後先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址 |
application.yml--------->注意紅色字體
spring: application: name: microservicecloud-config-client #如果有bootstrap.yml文件,那麼這個名字就必須是bootstrap.yml裏面獲取git上配置文件的去掉後綴的文件名字 |
怎麼樣?沒有看見設置端口吧?因爲待會會連接上config服務端,從服務端獲取git上之前上傳的yml文件中,就包含了端口號,通過bootstrap.yml中的profile來選擇啓用哪種配置
-
-
- 獲取服務端信息類以及啓動類
-
測試返回類
package shun.cloud.test;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
/** * 從服務端獲取到程序的名字,默認配置方式,服務端口,然後打印並返回 * @author Administrator */ @RestController public class ConfigClientRest {
@Value("${spring.application.name}") private String applicationName;
@Value("${eureka.client.service-url.defaultZone}") private String eurekaServers;
@Value("${server.port}") private String port;
@RequestMapping("/config") public String getConfig() { String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; System.out.println("******str: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; } }
|
啓動類
package shun.cloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** * @author czs * @version 創建時間:2018年8月28日 上午11:23:29 */ @SpringBootApplication public class Clientconfig3355_App {
public static void main(String[] args) throws Exception { SpringApplication.run(Clientconfig3355_App.class, args); }
}
|
-
-
- 開始進行訪問測試
-
啓動3344
等待3344服務啓動完畢後再次啓動3355,如果報錯找不到那就再來一遍,還報錯查找其它原因
先來看看github上面的文件內容
先來看看test環境
再來看看配置成dev環境