每一個(gè)應(yīng)用程序在運(yùn)行時(shí)都需要相應(yīng)的yml配置,分布式架構(gòu)下多個(gè)服務(wù)器和應(yīng)用服務(wù)面臨著多個(gè)配置文件,在修改和發(fā)布上難度較大,需要有一個(gè)管理中心來統(tǒng)一管理,優(yōu)雅的解決了配置的動(dòng)態(tài)變更、持久化、運(yùn)維成本等問題
流程:
分布式配置中心去遠(yuǎn)程倉庫將創(chuàng)建好的yml文件讀取,application client去分布式配置中心獲取配置
Spring Cloud Config: spring cloud config server和spring cloud config client
基于Http協(xié)議
hello world
一.在gitee上創(chuàng)建一個(gè)倉庫,創(chuàng)建application.yml,如下配置:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: fsfs
二.創(chuàng)建一個(gè)項(xiàng)目config_server
1.導(dǎo)入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.yml配置:
server:
port: 8888
# 增加分布式配置中心服務(wù)端配置。連接GIT倉庫
spring:
cloud: # spring cloud常用配置前置
config: # 分布式配置中心配置前置
server: # 服務(wù)端配置
git: # git文件倉庫配置
uri: https://gitee.com/xxxx/spring_cloud_config.git # git倉庫具體地址
#username: xxxxxx # 私有倉庫必須配置用戶名和密碼。
#password: xxxxxx # 公開倉庫可以省略用戶名和密碼配置。
3.啟動(dòng)類上加@EnableConfigServer注解
@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigApplication.class, args);
}
}
三.訪問測(cè)試:
在瀏覽器地址欄輸入 http://localhost:8888/name/profile/label ,基于Config Server獲取需要的配置文件。
name-必要restful參數(shù),代表配置文件主體名稱,及一般為application
profile-必要restful參數(shù),代表配置文件擴(kuò)展環(huán)境名稱,默認(rèn)讀取default環(huán)境擴(kuò)展配置,即application-xx.yml中的xx
label-可選參數(shù),代表配置文件所在GIT分支名稱,默認(rèn)null,相當(dāng)于master分支
訪問 http://localhost:8888/application/default(沒有可以不寫)/master(分支名稱)
四.創(chuàng)建項(xiàng)目config_client獲取config_server的配置信息
1.導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.創(chuàng)建bootstrap.yml,不使用application.yml,防止沖突
新配置文件 bootstrap.yml | properties。是spring cloud config技術(shù)支持的新配置文件。
配置文件由config分布式配置中心客戶端讀取,并請(qǐng)求分布式配置中心服務(wù)端,查詢獲取配置文件之后,Spring Boot根據(jù)配置文件,初始化環(huán)境
spring:
cloud:
config: # spring cloud config 客戶端配置
uri: http://localhost:8888 # 分布式配置中心服務(wù)端地址。 默認(rèn)http://localhost:8888
name: application # 要讀取的配置文件名,默認(rèn)是spring.application.name的配置值,如果沒有配置,默認(rèn)application
# profile: default # 要讀取的配置文件環(huán)境是什么,默認(rèn)default
label: master # 要讀取的配置文件所在分支名稱。默認(rèn)null。從主干分支獲取文件。
四.改變遠(yuǎn)程倉庫的端口server.port,
反復(fù)啟動(dòng)cofig_client項(xiàng)目,觀察端口號(hào)變化
熱刷新
1.添加導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.Config Client新增配置內(nèi)容
management:
endpoints:
web:
exposure:
include: refresh,info,health
3.Config Client修改服務(wù)實(shí)現(xiàn)
在使用遠(yuǎn)程配置內(nèi)容的類上(案例中的Service,放在controler上無效)增加新注解:
@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClientService {
@Value("${my.content}")
private String content;
@Override
public String test() {
System.out.println("content = " + content);
return content;
}
}
4.測(cè)試熱刷新
使用PostMan工具,發(fā)送POST
訪問 http://localhost:8080/actuator/refresh 訪問改地址,才能刷新注入,實(shí)際已經(jīng)刷新了
再次訪問 http://localhost:8080/test
Spring Cloud Bus:消息總線
Spring Cloud Bus集成了市面上常見的RabbitMQ和Kafka等消息代理。其會(huì)連接微服務(wù)系統(tǒng)中所有擁有Bus總線機(jī)制的節(jié)點(diǎn),當(dāng)有數(shù)據(jù)變更的時(shí)候,會(huì)通過消息中間件使用消息廣播的方式通知所有的微服務(wù)節(jié)點(diǎn)同步更新數(shù)據(jù)。(如:微服務(wù)配置更新等)
基于Bus消息總線實(shí)現(xiàn)熱刷新功能,需要在所有的Eureka Client端應(yīng)用中增加spring-cloud-starter-bus-amqp依賴,這個(gè)依賴是消息總線集成的RabbitMQ消息同步組件?;谙⒖偩€的熱刷新同樣是通過actuator實(shí)現(xiàn)的,所以需要spring-boot-starter-actuator啟動(dòng)器依賴。
1.在Config Client中增加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 總線技術(shù)中的amqp相關(guān)依賴。 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2.編輯配置文件: 編輯Config Client中的配置文件。文章來源:http://www.zghlxwxcb.cn/news/detail-421893.html
spring:
rabbitmq:
host: localhost
username: guest
password: giest
management:
endpoints:
enabled-by-default: true
web:
exposure:
include: bus-refresh,info,health
3.測(cè)試
消息總線Bus基于Actuator對(duì)外提供了熱刷新服務(wù),服務(wù)地址是:http://localhost:8080/actuator/bus-refresh。此服務(wù)只能使用POST方式請(qǐng)求,可以使用PostMan測(cè)試。文章來源地址http://www.zghlxwxcb.cn/news/detail-421893.html
到了這里,關(guān)于config: 分布式配置中心 & bus: 消息總線的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!