SpringCloud實戰之路 | 應用篇(七)分佈式配置中心Spring Cloud Config+Bus
問題場景
在分佈式環集羣境下會存在多個微服務,並且在實際工作中還存在不同的環境(開發dev,測試test,生產prod),當我們需要修改他們的配置信息時(比如application.yml),不可能去一個一個修改,在一定場景下還需要在運行期間進行配置信息的調整,達到動態刷新配置信息的效果,在這種場景下,我們就需要引入分佈式配置中心來對所有服務配置文件進行集中式管理。
Spring Cloud Config
Spring Cloud Config是一個分佈是配置中心的解決方案,它主要包含Server端和Client端兩個部分。
- Server端: 提供配置文件存儲,以接口形式將配置文件內容提供出去,通過使用@EnableConfigServer註解在Spring boot 應用中嵌入
- Client端: 通過接口獲取配置數據並初始化自己的應用服務
代碼實現
Spring Cloud Config通過將配置文件託管到到遠程的倉庫中(默認是git),每個服務從git上獲取到配置信息來進行加載。
(一)創建配置信息倉庫
登錄到github,創建版本庫(這裏取名爲cloud-config-repo),上傳配置文件信息
配置文件命名規則:{application}-{profile}.yml或.properties
application對應項目名稱,profile對應環境(dev:開發,test:測試,prod:生產)
例如:這裏創建了三個配置文件中的cloud-service-user-dev
其中:cloud-service-user對應{application},dev對應{profile}
(二)構建ConfigServer服務
引入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>cloud-parent</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-server</artifactId>
<dependencies>
<!--eureka client 客戶端依賴引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--config配置中心服務端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
創建入口啓動類
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer // 開啓配置中心功能
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
創建配置文件yml
server:
port: 9006
#註冊到Eureka服務中心
eureka:
client:
service-url:
# 註冊到集羣,就把多個Eurekaserver地址使用逗號連接起來即可;註冊到單實例(非集羣模式),那就寫一個就ok
defaultZone: http://LagouEurekaServerA:8761/eureka,http://LagouEurekaServerB:8762/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/Nicococococo/cloud-config-repo.git #配置git服務地址 即上文創建的git倉庫地址
username: [email protected] #配置git用戶名
password: 123456 #配置git密碼
search-paths:
- cloud-config-repo #對應上文創建的git的資源庫名稱
# 讀取分支
label: master
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細節
endpoint:
health:
show-details: always
(三)構建Client端服務
每個微服務引入maven依賴
<!--Config 客戶端依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- Actuator可以幫助你監控和管理Spring Boot應用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改微服務application.yml配置⽂件,添加配置中心的地址
server:
port: 8080
#註冊到Eureka服務中心
eureka:
client:
service-url:
# 註冊到集羣,就把多個Eurekaserver地址使用逗號連接起來即可;註冊到單實例(非集羣模式),那就寫一個就ok
defaultZone: http://EurekaServerA:8761/eureka,http://EurekaServerB:8762/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-service-user
datasource:
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免將駝峯命名轉換爲下劃線命名
cloud:
# config客戶端配置,和ConfigServer通信,並告知ConfigServer希望獲取的配置信息在哪個文件中
config:
name: cloud-service-user #配置文件名稱 對應上文在git倉庫中創建的配置文件名稱的{application}
profile: dev #後綴名稱 對應上文在git倉庫中創建的配置文件名稱的{profile}
label: master #分支名稱
uri: http://localhost:9006 #ConfigServer配置中心地址
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細節
endpoint:
health:
show-details: always
完成Client端的修改
到這裏就完成Spring Cloud Config的搭建了,在啓動服務後,會自動從git上拉去配置文件信息來進行加載
Config配置手動刷新
在配置文件進行修改後可以通過手動向client客戶端發送post請求http://localhost:8080/actuator/refresh,即可刷新配置信息,但是需要在Client客戶端中使用到配置信息的類上加上@RefreshScope
Config配置自動刷新
在微服務架構中可以通過結合消息總線Bus實現分佈式配置的自動更新
config server引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
config server添加配置
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
重啓服務,更改配置之後發送請求http://localhost:9003/actuator/bus-refresh,各客戶端服務配置即可實現自動刷新