SpringCloud實戰之路 | 應用篇(七)分布式配置中心Spring Cloud Config+Bus
-
-
- 問題場景
- Spring Cloud Config
- 代碼實作
-
-
- (一)建立配置資訊倉庫
- (二)建構ConfigServer服務
- (三)建構Client端服務
-
- Config配置手動重新整理
- Config配置自動重新整理
-
文章内容輸出來源:拉勾教育Java高薪訓練營;
問題場景
在分布式環叢集境下會存在多個微服務,并且在實際工作中還存在不同的環境(開發dev,測試test,生産prod),當我們需要修改他們的配置資訊時(比如application.yml),不可能去一個一個修改,在一定場景下還需要在運作期間進行配置資訊的調整,達到動态重新整理配置資訊的效果,在這種場景下,我們就需要引入分布式配置中心來對所有服務配置檔案進行集中式管理。
Spring Cloud Config
Spring Cloud Config是一個分布是配置中心的解決方案,它主要包含Server端和Client端兩個部分。
- Server端: 提供配置檔案存儲,以接口形式将配置檔案内容提供出去,通過使用@EnableConfigServer注解在Spring boot 應用中嵌入
- Client端: 通過接口擷取配置資料并初始化自己的應用服務
SpringCloud實戰之路 | 應用篇(七)分布式配置中心Spring Cloud Config+Bus
代碼實作
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,各用戶端服務配置即可實作自動重新整理