Spring Cloud Config 是 Spring Cloud 團隊建立的一個全新項目,用來爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,html
它分爲服務端與客戶端兩個部分。其中服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,java
用來鏈接配置倉庫併爲客戶端提供獲取配置信息、加密 / 解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,mysql
它們經過指定的配置中心來管理應用資源與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。git
Spring Cloud Config 實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,因此它除了適用於 Spring 構建的應用程序以外,github
也能夠在任何其餘語言運行的應用程序中使用。因爲 Spring Cloud Config 實現的配置中心默認採用 Git 來存儲配置信息,web
因此使用 Spring Cloud Config 構建的配置服務器,自然就支持對微服務應用配置信息的版本管理,而且能夠經過 Git 客戶端工具來方便的管理和訪問配置內容。spring
固然它也提供了對其餘存儲方式的支持,好比:GIT倉庫、SVN 倉庫、本地化文件系統。sql
Config Server端主要和Git/SVN服務器apache
通俗點,就是統一管理配置,包括方便切換環境配置,以及修改配置無需動代碼,省心省力;bootstrap
若是用上SpringCloud Bus,能實現無需重啓,自動感知配置變化以及應用新配置;
根據前面SpringCloud架構圖,首先第一步,要搞個 configServer來聯通遠程GIT倉庫,來讀取遠程配置;
這裏GIT倉庫,咱們通常選用GitHub https://github.com/,或者碼雲 https://gitee.com/
咱們這裏用GitHub演示
建個倉庫 microservice-config 而後 Git下載本地;
將倉庫克隆到本地
上傳一個配置文件上到git倉庫,application.yml 記住要utf-8編碼,不然亂碼,解析各類問題;
文件內容:
profile: hello
目前只要能讀取到配置便可;
新建module:microservice-config-server-4001
添加pom依賴
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-config-server-4001</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 <exclusions> 27 <exclusion> 28 <groupId>org.junit.vintage</groupId> 29 <artifactId>junit-vintage-engine</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-config-server</artifactId> 36 </dependency> 37 </dependencies> 38 39 <build> 40 <plugins> 41 <plugin> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-maven-plugin</artifactId> 44 </plugin> 45 </plugins> 46 </build> 47 48 </project>
啓動類ConfigServerApplication_4001:
1 package com.yuan.microserviceconfigserver4001; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.config.server.EnableConfigServer; 6 7 @SpringBootApplication 8 @EnableConfigServer 9 public class MicroserviceConfigServer4001Application { 10 11 public static void main(String[] args) { 12 SpringApplication.run(MicroserviceConfigServer4001Application.class, args); 13 } 14 15 }
這裏要加下註解:@EnableConfigServer
這裏咱們搞下倉庫的Http地址:
而後項目的application.yml配置下:
1 server: 2 port: 4001 3 4 spring: 5 application: 6 name: microservice-config 7 cloud: 8 config: 9 server: 10 git: 11 uri: https://github.com/Me-yuan/t226microservice-config.git
主要是要配置一個git請求地址:
本地Hosts加個本地域名映射:
啓動
而後咱們請求:http://configserver.yuan.com:4001/application-xxx.yml
返回結果了正確的文本結果;
根據前面的config原理圖,咱們須要創建Client端調用server端,最終實現client端獲取遠程git配置信息;
爲了後面演示方便,咱們提交三個配置文件到遠程git庫;
application.yml:
1 --- 2 spring: 3 profiles: 4 active: dev 5 --- 6 spring: 7 profiles: dev 8 port: 111 9 --- 10 spring: 11 profiles: test 12 port: 222
crm-dev.yml
1 port: 2 777
crm-test.yml
1 port: 2 888
提交到github
而後咱們新建一個module microservice-config-client-5001
加下依賴:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-config</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-tomcat</artifactId> 8 </dependency> 9 <dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-web</artifactId> 12 </dependency>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-config-client-5001</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 <exclusions> 27 <exclusion> 28 <groupId>org.junit.vintage</groupId> 29 <artifactId>junit-vintage-engine</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-config</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-tomcat</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-web</artifactId> 45 </dependency> 46 </dependencies> 47 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 </plugin> 54 </plugins> 55 </build> 56 57 </project>
咱們項目啓動的時候,就要調用server config端,
獲取配置信息,因此這裏要新建一個bootstrap.yml配置文件,優先級最高:
1 spring: 2 application: 3 name: application-dev 4 cloud: 5 config: 6 name: crm 7 uri: http://configserver.yuan.com:4001 8 profile: test 9 label: master 10 fail-fast: true
application.yml:
1 server: 2 port: 5001 3 context-path: /
再搞一個 ConfigClientController 類 測試顯示端口:
1 package com.yuan.microserviceconfigclient5001.controller; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @RestController 8 public class ConfigClientController { 9 10 @Value("${port}") 11 private String port; 12 13 @Value("${server.port}") 14 private String serverPort; 15 16 @GetMapping("/getPort") 17 public String getPort() { 18 return "測試你訪問的yml文件的端口是:【"+port+"】"+",serverPort:【"+serverPort+"】"; 19 } 20 21 }
最後 本地hosts咱們加給配置:
C:\Windows\System32\drivers\etc\hosts
咱們啓動項目:而後頁面訪問:
http://client-config.yuan.com:5001/getPort
便可獲取遠程端口配置信息;
更改bootstrap.yml中的profile便可訪問不一樣的yml文件;
訪問: http://configserver.yuan.com:4001/application-dev.yml
咱們如今搞個實例來演示下,eureka整合config以及服務器提供者整合config,這樣大夥能夠舉一反一,方便理解;
首先是eureka整合config
咱們先搞個配置文件到git;
eureka_config.yml
1 spring: 2 profiles: 3 active: 4 - dev 5 --- 6 server: 7 port: 2004 8 context-path: / 9 spring: 10 profiles: dev 11 eureka: 12 instance: 13 hostname: localhost 14 client: 15 register-with-eureka: false 16 fetch-registry: false 17 service-url: 18 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 19 --- 20 server: 21 port: 2005 22 context-path: / 23 spring: 24 profiles: test 25 eureka: 26 instance: 27 hostname: localhost 28 client: 29 register-with-eureka: false 30 fetch-registry: false 31 service-url: 32 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
咱們啓動 microservice-config-server-4001
再啓動 microservice-eureka-server-config
測試鏈接
http://eureka2001.yuan.com:2004/
說明成功讀取遠程Git配置,而後eureka啓動OK;
而後咱們就是把服務提供者和config整合,把服務提供者註冊到eureka;
咱們搞個配置provider_config.yml,push到遠程GIT;
1 spring: 2 profiles: 3 active: dev 4 --- 5 server: 6 port: 1007 7 context-path: / 8 9 # 數據源配置 10 spring: 11 profiles: dev 12 application: 13 name: microservice-student 14 datasource: 15 type: com.alibaba.druid.pool.DruidDataSource 16 driver-class-name: com.mysql.jdbc.Driver 17 url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8 18 username: root 19 password: 12345 20 jpa: 21 hibernate: 22 ddl-auto: update 23 show-sql: true 24 25 eureka: 26 instance: 27 hostname: localhost 28 appname: microservice-student 29 instance-id: microservice-student:1007 30 prefer-ip-address: true 31 client: 32 service-url: 33 defaultZone: http://localhost:2004/eureka 34 35 info: 36 groupId: com.yuan.testSpringcloud 37 artifactId: microservice-student-provider-config-1007 38 version: 1.0-SNAPSHOT 39 userName: http://yuan.com 40 phone: 123456 41 --- 42 server: 43 port: 1008 44 context-path: / 45 46 # 數據源配置 47 spring: 48 profiles: test 49 application: 50 name: microservice-student 51 datasource: 52 type: com.alibaba.druid.pool.DruidDataSource 53 driver-class-name: com.mysql.jdbc.Driver 54 url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8 55 username: root 56 password: 12345 57 jpa: 58 hibernate: 59 ddl-auto: update 60 show-sql: true 61 62 eureka: 63 instance: 64 hostname: localhost 65 appname: microservice-student 66 instance-id: microservice-student:1008 67 prefer-ip-address: true 68 client: 69 service-url: 70 defaultZone: http://localhost:2004/eureka 71 72 info: 73 groupId: com.yuan.testSpringcloud 74 artifactId: microservice-student-provider-config-1008 75 version: 1.0-SNAPSHOT 76 userName: http://yuan.com 77 phone: 123456
新建module:microservice-student-provider-config
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-student-provider-config</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-web</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-test</artifactId> 24 <scope>test</scope> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-data-jpa</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>mysql</groupId> 32 <artifactId>mysql-connector-java</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-tomcat</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>com.alibaba</groupId> 40 <artifactId>druid-spring-boot-starter</artifactId> 41 </dependency> 42 <!-- 修改後當即生效,熱部署 --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>springloaded</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-devtools</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>com.yuan</groupId> 53 <artifactId>microservice-common</artifactId> 54 <version>1.0-SNAPSHOT</version> 55 <scope>compile</scope> 56 </dependency> 57 58 <!--添加註冊中心Eureka相關配置--> 59 <dependency> 60 <groupId>org.springframework.cloud</groupId> 61 <artifactId>spring-cloud-starter-eureka</artifactId> 62 </dependency> 63 64 <!-- actuator監控引入 --> 65 <dependency> 66 <groupId>org.springframework.boot</groupId> 67 <artifactId>spring-boot-starter-actuator</artifactId> 68 </dependency> 69 <dependency> 70 <groupId>org.springframework.cloud</groupId> 71 <artifactId>spring-cloud-starter-config</artifactId> 72 </dependency> 73 </dependencies> 74 75 <build> 76 <plugins> 77 <plugin> 78 <groupId>org.springframework.boot</groupId> 79 <artifactId>spring-boot-maven-plugin</artifactId> 80 </plugin> 81 </plugins> 82 </build> 83 84 </project>
bootstrap.yml:
1 spring: 2 application: 3 name: microservice-student-provider-config 4 cloud: 5 config: 6 name: provider_config 7 uri: http://configserver.yuan.com:4001 # 配置configserver地址 8 profile: dev # 級別 9 label: master # 分支 git中 默認master
application.yml
1 spring: 2 application: 3 name: microservice-student-provider-config
其餘類文件從 原先的服務提供者裏直接複製一份便可,這裏不貼了;
啓動下這個項目;
說明成功註冊到服務註冊中心了;
前面咱們全部的GIT遠程端配置文件都是跟目錄的,全部請求默認都是根目錄,可是有時候,項目不少,
配置文件須要根據子目錄來劃分,這時候,就須要來配置搜索路徑了;好比aaa項目的配置文件放aaa目錄下,
bbb項目的配置文件放bbb目錄下,不配置的話 是找不到的那些配置文件的,咱們須要配置search-paths屬性實現;
microservice-config-server-4001 configserver端 加個配置
1 server: 2 port: 4001 3 4 spring: 5 application: 6 name: microservice-config 7 cloud: 8 config: 9 server: 10 git: 11 uri: https://github.com/Me-yuan/t226microservice-config.git 12 search-paths: aaa,bbb,ccc
分別搞3個目錄aaa,bbb,ccc 裏面分別放3個配置文件 nns.yml,nns2.yml,nn3.yml;
配置內容大致差很少,隨便寫;
aaa/nns.yml
1 spring: 2 profiles: 3 active: dev 4 --- 5 spring: 6 profiles: dev 7 name: aaadev 8 --- 9 spring: 10 profiles: test 11 name: aaatest
咱們啓動:microservice-config-server-4001
瀏覽器:http://configserver.yuan.com:4001/nns-test.yml
瀏覽器:http://configserver.yuan.com:4001/nns2-test.yml
瀏覽器:http://configserver.yuan.com:4001/nns-3test.yml