Springcloud config 分散式配置中心
微服務把單一服務拆成一個一個的子服務,每個服務粒度相對較小。在系統中會出現大量的服務。每個服務都需要必要的配置檔案。
Springcloud config 為微服務架構中的微服務提供集中化的外部配置支援。 配置伺服器為各個不同的微服務應用提供了一箇中心化的配置。
集中管理配置檔案
1. 在不同的環境配置。 動態的配置更新。 分環境 dev test prod
2.在執行期間動態調整配置。 動態調整tomcat 執行緒池
3.當配置檔案發生了改變的時候。 服務不需要重新啟動
4.將配置資訊以rest介面進行暴露 動態訪問重新整理機制
1.引pom.xml
<dependencies> <!--新增訊息匯流排RabbitMQ支援--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</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>
主要是spring-boot-starter-actuator監控這個依賴
2.新建yml配置檔案
server:
port: 3344
spring:
application:
name: cloud-config-server #註冊到eureka
cloud:
config:
server:
git:
uri: https://gitee.com/***/springcloud-config.git
# 搜尋目錄
search-paths:
- springcloud-config
label: master
rabbitmq:
port: 5672
username: guest
password: guest
host: localhost
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
#暴露監控的端點
management:
endpoints:
web:
exposure:
include: "*"
主要是配置 暴露監控埠
3.新建main方法
@Slf4j @SpringBootApplication @EnableConfigServer public class ConfigMain3344 { public static void main(String[] args) { SpringApplication.run( ConfigMain3344.class,args); log.info("****************ConfigMain3344 啟動 ************ "); }
@EnableConfigServer這個註解集成了eureka功能
分散式配置中心服務就新建好了
二。再建兩個客戶端服務測試
1.引pom.xml
<dependencies> <!--新增訊息匯流排RabbitMQ支援--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</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>
2.新建main方法
@SpringBootApplication @Slf4j @EnableEurekaClient public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run( ConfigClientMain3355.class,args); log.info("****************ConfigClientMain3355 啟動 ************ "); } }
3.新建controller呼叫測試
@RefreshScope @RestController @Slf4j public class ConfigController { @Value("${config.info}") private String configInfo; @GetMapping("configInfo") public String getConfig(){ return configInfo; } }
重點是@RefreshScope這個註解讓配置檔案中自定義的變數能通過@Value註解正常注入
4.建yml配置檔案
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名字
name: config # 配置檔名字
profile: dev # 讀取字尾
uri: http://localhost:3344 # 配置中心的地址
rabbitmq:
port: 5672
username: guest
password: guest
host: localhost
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#暴露監控的端點
management:
endpoints:
web:
exposure:
include: "*"
eq: master config-dev.yml
/{lable}/{application}-{profile}.yml 最常用
/{application}-{profile}.yml
/{application}/{profile}/{lable}
label : 分支 branch
name 服務名字
profile: 環境 dev test prod
測試:當配置檔案發生變化後,只能手動重新整理用POST請求訪問http://localhost:3355/actuator/refresh
再訪問客戶端介面就能看到改變,缺點就是每個微服務都要手動重新整理
三。動態重新整理:
一次廣播處處生效重新整理
配置中心服務重新整理一次,所有微服務的配置都重新整理
Springcloud bus 配合 Spring Cloud config 使用 可以實現配置的動態重新整理
Spring cloud bus 目前只支援Rabbitmq 和 Kafka
Spring cloud bus 能管理和傳播分散式系統的訊息。 就像一個執行器 廣播狀態 事件推送等 。也可以當作微服務 的通訊通道 。
系統中的所有微服務例項都連結上來。 由於該主題產生的訊息會被所有的例項監聽和消費,所以被稱為訊息匯流排。
利用訊息匯流排觸發一個服務端configserver(配置中心服務) 的/bus/refresh 端點。 從而重新整理客戶端配置
1.pom.xml加依賴,上文中已有
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
2.配置yml,上文中已配
rabbitmq:
port: 5672
username: guest
password: guest
host: localhost
#暴露監控的端點
management:
endpoints:
web:
exposure:
include: "*"
3.測試
傳送POST請求:
localhost:3344/actuator/bus-refresh 所有微服務的配置都重新整理
localhost:3344/actuator/bus-refresh/{destination} 重新整理指定微服務的配置
呼叫微服務具體介面可看到結果