一、爲什麼要統一管理管理微服務配置
在微服務框架中,微服務的配置管理一般有一下需求:
1、集中管理配置
一個使用微服務架構的應用系統可能會包含成百上千個微服務,集中管理配置非常用必要。
2、不同環境不同配置
如:數據源配置在不同的環境(開發、測試、預發佈、生產等)中是不同的。
3、運行期可動態調整
如:可根據各個微服務的負載情況,動態調整數據源連接池大小,並且在調整配置時不停止微服務。
4、配置修改後可自動更新
如:配置內容發送變化,微服務能夠自動更新配置。
對於微服務架構而言,一個通用的配置管理機制是必不可少的,常見做法是使用配置服務器管理配置。
二、Spring Cloud Config簡介
Spring Cloud Config爲分佈式系統外部化配置提供了服務端和客戶端的支持,它包括Config Server和Config Client兩部分。由於Config Server和Config Client都實現了對Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常適合Spring應用。
Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置的內容(也可以屬於SVN),因此可以很方便的實現對配置的版本控制與內容審計。
Config Client是Config Server的客戶端,用於操作存儲在Config Server中的配置屬性。所有的微服務在啓動時,會請求Config Server以獲取所需要的配置屬性,然後緩存這些屬性以提高性能。
三、編寫Config Server與Config Client
3.1、Config Server
a、在gti上新建一個項目,如spring-cloud-config,然後新建幾個配置文件,如:
裏面的內容這裏寫的:
profile: 環境-1.0
如spring-cloud-demo-test.yml中是profile:test-1.0
爲了測試版本控制,這裏拉一個分支,如:v2.0
裏面的內容這裏寫的:
profile: 環境-2.0
b、新建一個工程,如config-server,添加如下依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
c、編寫啓動類,添加@EnableConfigServer註解
package com.liuy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * config服務啓動類 * @description config服務啓動類 * @author luis * @version 1.0 * @date:2017年8月29日下午5:09:14 */ @SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
d、編寫application.yml配置文件
server: port: 5020 spring: application: name: config-server cloud: config: server: git: uri: https://git.oschina.net/wadjz/spring-cloud-config.git username: 用戶名 password: 密碼
這樣,一個config Server就好了,我們可以使用Config Server的端點獲取配置文件的內容。端點與配置文件的映射規則如下:
/{application}/{profile}[/{lable}] /{application}-{profile}.yml /{lable}/{application}-{profile}.yml /{application}-{profile}.properties /{lable}/{application}-{profile}.properties
以上端點都可以映射到{application}-{profile}.yml這個配置文件,{application}表示微服務的名稱,{profile}表示環境,{lable}表示對應git倉庫的分支,默認master。
按照以上規則,使用如下URL能訪問到GIT倉庫裏的master的spring-cloud-demo-test.yml
http://localhost:5020/spring-cloud-demo-test.yml
http://localhost:5020/v2.0/spring-cloud-demo-test.yml
下面URL可獲取應用名稱、項目profile等配置文件的詳情:
http://localhost:5020/spring-cloud-demo/test
http://localhost:5020/spring-cloud-demo/test/v2.0
3.2、Config Client
a、創建一個項目,如config-client,添加以下依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
b、編寫啓動類
package com.liuy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * config客戶端啓動類 * @description config客戶端啓動類 * @author luis * @version 1.0 * @date:2017年8月29日下午5:09:14 */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
c、編寫application.yml配置文件
server: port: 5021
d、編寫bootstrap.yml配置文件
spring: application: # 對應config Server所獲取的配置文件的{application} name: spring-cloud-demo cloud: config: # config Server的地址 uri: http://localhost:5020/ # profile對應config Server所獲取的配置文件的{profile} profile: dev # 指定git的分支,對應config Server所獲取的配置文件的{label} label: master
注意:這個必須配置在bootstrap.yml配置文件中。
e、編寫Controller
@RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String hello() { return this.profile; } }
通過註解@Value("${profile}"),綁定GIT倉庫配置文件中的profile屬性。
f、測試,訪問http://localhost:5021/profile