一、SpringCloudConfig定義
spring cloud config是一個基於http協議的遠程配置實現方式。通過統一的配置管理服務器進行配置管理,客戶端通過https協議主動的拉取服務的的配置信息,完成配置獲取。
二、為什么要使用監聽的形式重新獲取配置文件內容,而不是重啟服務
1、無需重啟服務,方便管理
三、使用(注冊中心內容不再展示)
1、單個服務通知的形式來更新配置文件內容
(1)、遠程gitHub內容(config-client.yml)
spring: profiles: active: dev --- server: port: 8081 spring: profiles: dev application: name: demo-client eureka: client: service-url: defaultZone: http://localhost:8080/eureka/ # 是否將自己注冊為服務 register-with-eureka: true # 需要檢索檢索服務 fetch-registry: true name: test --- server: port: 8082 spring: profiles: test application: name: demo-client eureka: client: service-url: defaultZone: http://localhost:8080/eureka/ # 是否將自己注冊為服務 register-with-eureka: true # 需要檢索檢索服務 fetch-registry: true name: test
(2)、ConfigServer的配置
1、pom.xml的引用如下:
<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>
2、直接上配置文件內容(application.yml)
spring: application: name: demo-config cloud: config: server: git: uri: https://github.com/SweetPiglet/spring-cloud-config.git username: your username password: your password eureka: client: service-url: defaultZone: http://localhost:8080/eureka/ # 是否注冊為服務 register-with-eureka: true # 是否進行掃描 fetch-registry: true
(3)、ConfigClient配置內容
1、pom.xml的配置如下:
<!--引入監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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>
2、配置文件的配置(bootstrap.yml)
# 引入ConfigServer的內容
spring:
cloud:
config:
uri: http://localhost:9000/
name: config-client
profile: dev
label: master
# 暴露監控點
management:
endpoints:
web:
exposure:
include: "*"
3、controller層的內容
/** * wsq */ @RestController @RefreshScope public class TestController { # 需要刷新什么屬性,就寫什么屬性 @Value("${name}") private String name; @GetMapping("/test") public String test(){ return name; } }
(4)、運行程序
首先啟動eurekaserver,之后啟動configserver,最后啟動configclient。
之后進行接口的訪問:
http://localhost:9000/config-client-dev.yml
http://localhost:8081/test
之后修改配置文件的內容,將name原來的test改成test1
然后在window黑窗口中輸入:
curl -X POST '''http://localhost:8081/actuator/refresh'
之后再次訪問連接,查看變化,發現沒有重啟服務依然可以達到刷新配置文件的效果
2、監聽+消息隊列來實現配置的更新
(1)、出現原因
如果運維工程師修改了很多個配置文件,也就是說需要訪問每一個服務,當然可以寫一個腳本,但是呢,如果有服務的增加就需要改動腳本,那么有沒有一種不需要進行配置的形式呢
(2)、作用
運維工程師直接通知一下configserver,configserver通過消息隊列的topic形式,發一個通知,只要是訂閱了該通知的,都可以收到通知,進行相應配置的更新
(3)、做法
1、首先,rabbitmq和erlang的安裝,不多說自行百度
2、基於上面的代碼,進行微調即可
configserver需要添加的內容如下:
pom.yml
<!--監聽--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--rabbitmq--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
application.yml
# 連接rabbitmq spring: rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest # 配置監聽 management: endpoints: web: exposure: include: 'bus-refresh'
configclient的變更如下:
pom.xml
<!--rabbitmq--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
bootstarp.yml
# 引入rabbitmq
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 暴露監控點
management:
endpoints:
web:
exposure:
include: "*"
(4)測試
首先啟動eurekaserver,之后啟動configserver,最后啟動configclient。
之后進行接口的訪問:
http://localhost:9000/config-client-dev.yml
http://localhost:8081/test
之后修改配置文件的內容,將name原來的test改成test1
然后在window黑窗口中輸入:
curl -X POST '''http://localhost:9000/actuator/bus-refresh'
之后再次訪問連接,查看變化,發現沒有重啟服務依然可以達到刷新配置文件的效果
完工
三、最后提一下定點通知:
curl -X POST '''http://localhost:9000/actuator/bus-refresh/config-client:8081'
也就是后面加上服務名加端口的形式來進行服務的定點刷新
四、git的修改后自動通知:
注意localhost只是演示,大家要填好自己的服務器ip