微服務意味着要將單體應用中的業務拆分紅一個個子服務,每一個服務的粒度相對較小,所以系統中標會出現大量的服務。因爲每一個服務都須要必要的配置信息才能運行,因此一套集中式的、動態的配置管理設施是必不可少的。咱們每個微服務本身有一個 application.yml 文件,若是有上百個這樣的文件維護起來確定容易讓人崩潰,因此 SpringCloud 提供了 ConfigServer 來解決這個問題。git
SpringCloud Config 爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不一樣微服務應用的全部環境提供了一箇中心化的外部配置。github
SpringCloud Config 分爲服務端和客戶端兩部分:web
一、在 GitHub 新建一個名爲 "microservicecloud-config" 的 Repository,提交以下編碼爲 UTF-8 的文件:spring
spring: profiles: active: - dev --- spring: profiles: dev application: name: microservicecloud-config-dev --- spring: profiles: test application: name: microservicecloud-config-test
二、新建名爲 "microservicecloud-config-3344" 的子工程做爲配置中心服務,依賴以下:apache
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>zze.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-3344</artifactId> <dependencies> <!-- SpringCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: https://github.com/zze326/microservicecloud-config.git # 對應配置文件的 git 倉庫連接
四、編寫主啓動類,使用註解啓用配置中心功能:服務器
package zze.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer // 標識當前服務爲配置中心 public class Application_3344 { public static void main(String[] args) { SpringApplication.run(Application_3344.class, args); } }
五、測試:架構
啓動項目,訪問 http://localhost:3344/application-dev.yml,能夠看到,返回內容爲 gibhub 中存放的配置:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
一、新建以下配置文件,提交到 GitHub 的配置倉庫中:app
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://www.eurekaserver1.com:7001/eureka --- server: port: 8202 spring: profiles: test application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://www.eurekaserver1.com:7001/eureka
二、新建名爲 "microservicecloud-config-client-3355" 的子工程做爲使用配置中心的客戶端,依賴以下:maven
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>zze.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-client-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>
三、新建系統級配置文件,指定配置中心地址和要讀取環境的配置:
spring: cloud: config: name: microservicecloud-config-client # 須要從 github 上讀取的資源名稱 profile: dev # 環境,決定讀取哪一個環境的配置內容 label: master uri: http://localhost:3344 # SpringCloud Config Server 地址
四、新建主啓動類:
package zze.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class Application_3355 { public static void main(String[] args) { SpringApplication.run(Application_3355.class, args); } }
五、新建獲取配置信息的 Controller:
package zze.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 經過 config 客戶端從 config server 獲取配置信息 */ @RestController public class ClientConfigController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @GetMapping("/config") public String getConfig(){ String str="applicationName = %s <br> eurekaServer = %s <br> port = %s"; return String.format(str, applicationName, eurekaServers, port); } }
六、測試:
一、啓動 7001 Eureka Server 二、啓動 3344 配置中心服務 三、啓動 "microservicecloud-config-client-3355" 服務,能夠看到佔用端口爲 github 配置文件中 dev 環境配置下的 8201,訪問 localhost:8201/config:
「application.yml」是用戶級的資源配置項,而「bootstrap.yml」是系統級的,優先級更高。
SpringCloud 會建立一個「Bootstrap Context」,做爲 Spring 應用的「Application Context」的父級上下文。初始化時,「Bootstrap Context」負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個外部獲取的「Environment」。
「Bootstrap」屬性有高優先級,默認狀況下,它們不會被本地配置覆蓋。「Bootstrap Context」和「Application Context」有着不一樣的約定,因此新增了一個「bootstrap.yml」文件,保證「Bootstrap Context」和「Application Context」配置的分離。
完整示例下載 | 提取碼:3lb2