Config
SpringCloud Config是微服務架構中的微服務提供的集中化外部配置支持,配置服務器爲各個不同的微服務的所有環境提供一箇中心化的外部配置。Config分爲客戶端和服務端兩部分,服務端是分佈式配置中心,是一個獨立的微服務,用來連接配置服務器併爲客戶端提供獲取配置信息,加密解密信息等訪問接口。客戶端則通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載信息配置服務默認採用git來存儲配置信息。
作用
1,集中管理配置文件
2,不同的環境不同的配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release
3,運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息。
4,當配置發生變動的時候,服務不再需要重啓來感知自身配置的變化並且及時地應用新的配置。
5,將配置信息以Rest接口的形式暴露。
基本使用
1,創建Config服務端。添加依賴。
<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--common模塊-->
<dependency>
<groupId>cn.izzer</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
接下來是yml的配置,Config會默認加載github上的配置文件,可是目前在國內上GitHub的話還是挺慢的,所以這裏最好用碼雲,就不會受網絡的限制。
2,創建配置倉庫。
3,將倉庫通過git clone到本地,將配置文件上傳到碼雲。
4,在yml裏面使用HTTPS或者SSH的方式引入倉庫地址,這裏我使用的是HTTPS。
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/Yintianhao/SpringCloudConfigCenter.git
search-paths:
- SpringCloudConfigCenter
username: Yintianhao(賬號)
password: XXXXX(賬號密碼)
force-pull: true
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
5,創建啓動類。主要是需要注意加上@EnableConfigServer的註解。
6,測試是否能夠通過localhost:3344正常訪問git上的配置文件。
http://localhost:3344/master/config-dev.yml
master是分支,config-dev表示文件名,這裏是指開發環境的配置文件,一般還有生產環境等等。
bootstrap.yml
與application不同的是bootstrap一般用來表示系統級的配置項,優先級比application高。SpringCloud會創建一個BootstrapContext,作爲Spring應用的ApplicationContext的父上下文,初始化的時候,BootstrapContext負責從外部資源加載配置屬性並且解析配置,這兩個上下文共享一個從外部獲取的Environment。Bootstrap的屬性具有高優先級,默認情況下他們都不會被本地配置覆蓋,Bootstrap context和Application Context有着不同的約定,所以新增一個Bootstrap.yml文件,保證BootstrapContext和application Context配置的分離。
搭建Config客戶端來獲取服務端的配置
依賴跟服務端不同的地方主要是這裏引用的是客戶端的依賴。其餘都和服務端差不多。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>cn.izzer</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在這之後新建Bootstrap.yml文件。
# bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config #配置文件名稱
profile: dev #讀取後綴名稱
uri: http://localhost:3344 #配置中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
啓動類就不寫了,在這之後,通過Controller層調用服務端的接口來獲得配置信息。
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configinfo")
public String getConfigInfo(){
return configInfo;
}
}
進行簡單的測試:
動態刷新的問題
這裏帶來的問題就是客戶端無法和同步更新服務端檢測到的git上的配置信息的改變,所以需要配置動態刷新。這裏就需要用到actuator的監控功能了,從而可以避免每次都需要重啓客戶端來同步服務端的配置信息。需要的配置信息就是上面yml文件中的最後的management節點中的內容。除此之外就是需要在controller層加入@RefreshScope的註解來開啓刷新。但是這裏我個人覺得還是有一點不方便,儘管這不需要重啓客戶端,但是與此同時又需要利用curl來發送post請求來激活刷新,如果客戶端數量不多那還好,但如果在生產環境中有需要個客戶端服務,那麼這裏的工作量也是不小的。我想應該後面會有相應的技術解決方案,之後學到了再把這個坑填上。
curl測試很簡單。
在執行完這個命令之後就可以使用動態刷新了。