spring cloud config笔记
spring cloud config
官網的spring cloud config文檔:Spring Cloud Config
server端
server端主要讀取遠程源的配置信息到本地,然后給后臺微服務提供統一的配置中心。
pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId> </dependency>主啟動類
使用spring cloud config server需要在主啟動類中配置@EnableConfigServer注解,以確保開啟configServer的配置。
@SpringBootApplication @EnableConfigServer public class ConfigServer {public static void main(String[] args) {SpringApplication.run(ConfigServer.class, args);} }application.yml
需要在application.yml配置文件中配置git的遠程倉庫地址。配置后,spring cloud config server將會和遠程倉庫的文件進行信息的同步。
server:port: 9001spring:application:name: cloud-config9001cloud:config:server:git:uri: xxx.git #倉庫地址search-paths: springcloud-config #掃描路徑label: master除了github倉庫以外,spring cloud config server還支持重其他源讀取配置信息,如AWS的S3
spring:profiles:active: awss3cloud:config:server:awss3:region: us-east-1bucket: bucket1資源路徑
配置好并啟動好spring cloud config項目后,我們就可以通過以下資源路徑來獲取配置信息啦。其中:
label 分支
application 配置名(一般為微服務應用名)
profile 配置概述(如:dev)
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties官方示例:
curl localhost:8888/foo/development curl localhost:8888/foo/development/master curl localhost:8888/foo/development,db/master curl localhost:8888/foo-development.yml curl localhost:8888/foo-db.properties curl localhost:8888/master/foo-db.propertiesclient端
pom.xml
maven依賴導入:
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId> </dependency>加載優先級
加載文件優先級如下:
- application.yml 用戶級的資源配置
- bootstrap.yml 系統級,優先級高
先加載優先級高的bootstrap.yml配置,然后再加載優先級第的application.yml,并且優先級高的配置會覆蓋優先級低的,也就是說bootstrap.yml先于application.yml(bootstrap.yml和application.yml都有的配置,就選擇bootstrap.yml的配置)
bootstrap.yml配置
為了確保微服務自身的配置優先級高于公共配置信息(本地倉庫的公共配置或遠程的公共配置),所以微服務自身的配置文件我們命名為bootstrap.yml。
spring:application:name: cloud-config-clientcloud:config:label: master #分支name: config #配置名profile: dev #信息后綴(對應上面的profile)uri: http://localhost:9001 #配置中心動態刷新配置
server端的配置來github等其他源,是屬于直接獲取的,server端的配置能動態刷新,但是其他config client端想要動態刷新配置,必須在bootstrap.yml添加另外一些配置信息,并且需要添加一些maven依賴。
maven依賴
監控器依賴
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>暴露監控端點
management:endpoints:web:exposure:include: *通過網絡接口發起post請求刷新
對http://localhost:8888/actuator/refresh 發起post請求,以刷新微服務的配置信息。
@RefreshScope注解
有時后你的代碼用會用到一些配置文件的信息,如:一些自定義的bean通過@value注入application.yml中的某些值,或者通過@ConfigurationProperties映射到application.yml中的某些自定義配置項。
這些信息可以通過@RefreshScope來解決動態跟新的配置信息,如下代碼中,那么我myconfigbeans就可以通過@RefreshScope以刷新注入到自定義bean中的值。
@Component @RefreshScope //使得每次獲取bean都是得到配置文件中的最新值 public class myconfigbeans {@Value("${configbeans.user}") //配置文件中的configbeans.user=“YAO”String user; }廣播刷新配置
要使用廣播以刷新配置,就要配合spring cloud bus使用,spring cloud bus和spring cloud Stream息息相關。
Stream通過對消息中間件進行抽象封裝,提供一個統一的接口供我們發送和監聽消息,而Bus則是在Stream基礎之上再次進行抽象封裝,使得我們可以在不用理解消息發送、監聽等概念的基礎上使用消息來完成業務邏輯的處理。
總結
以上是生活随笔為你收集整理的spring cloud config笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript 的 clientX
- 下一篇: NW.js使用及打包