前言:本次開發環境爲SpringBoot 2.1.4.RELEASE、SpringCloud Greenwich.SR1、SpringCloudConfig 2.1.1.RELEASE
發現問題
使用config手動通過訪問/actuator/bus-refresh可以正常刷新,但是通過配置webhooks訪問/monitor無法刷新配置。
解決問題
官方文檔排查
https://cloud.spring.io/spring-cloud-bus/spring-cloud-bus.html bus的文檔中對spring.cloud.bus.id有如下描述:
應用有一個ServiceID,默認的值是app:index:id的組裝。
規則是:
app :如果vcap.application.name存在,使用vcap.application.name,否則使用spring.application.name(默認值爲application)
index :配置值的情況下優先使用vcap.application.instance_index,否則依次使用spring.application.index、local.server.port、server.port(默認值0)
id: 如果vcap.application.instance_id存在,使用vcap.application.instance_id,否則給一個隨機值
代碼排查
設置客戶端的打印日誌級別
logging:
level:
org.springframework.cloud.bus: debug
控制檯會打印出org.springframework.cloud.bus.DefaultBusPathMatcher中匹配規則的日誌(如果客戶端不刷新,一般這裏的日誌打印出的匹配規則和待匹配字符串是不一致的),webhooks端的過來匹配規則由三部分數據組成,使用“:”拼接,得到的結果如下:
spring.application.name:spring.cloud.config.profile:**
如果我們serviceID不進行設置,當前服務那麼會使用默認配置(默認配置代碼體現在:org.springframework.cloud.bus.BusEnvironmentPostProcessor#getDefaultServiceId),如下:
private String getDefaultServiceId(ConfigurableEnvironment environment) {
return "${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.application.index:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}";
}
對應官方文檔,以及我們的配置文件,我們可以依據serviceID的匹配規則來設置對應的參數去匹配webhooks。
如果發現app:index:id中的index不一致, 舉例yml配置:
vcap:
application:
instance_index: ${spring.cloud.config.profile}
或者直接修改bus.id的配置,如下:
spring:
application:
name: client
cloud:
config:
discovery:
service-id: CONFIG
enabled: true
profile: dev
bus:
id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value}