Spring Cloud Config2.x 版本使用webhooks沒法刷新client配置的解決方案

前言:本次開發環境爲SpringBoot 2.1.4.RELEASE、SpringCloud Greenwich.SR一、SpringCloudConfig 2.1.1.RELEASE

發現問題

使用config手動經過訪問/actuator/bus-refresh能夠正常刷新,可是經過配置webhooks訪問/monitor沒法刷新配置。html

解決問題

官方文檔排查

https://cloud.spring.io/sprin... bus的文檔中對spring.cloud.bus.id有以下描述:java

clipboard.png

應用有一個ServiceID,默認的值是app:index:id的組裝。
規則是:git

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,不然給一個隨機值

##### 代碼排查
設置客戶端的打印日誌級別github

logging:
  level:
    org.springframework.cloud.bus: debug

控制檯會打印出org.springframework.cloud.bus.DefaultBusPathMatcher中匹配規則的日誌(若是客戶端不刷新,通常這裏的日誌打印出的匹配規則和待匹配字符串是不一致的),webhooks端的過來匹配規則由三部分數據組成,使用「:」拼接,獲得的結果以下:web

spring.application.name:spring.cloud.config.profile:**

若是咱們serviceID不進行設置,當前服務那麼會使用默認配置(默認配置代碼體如今:org.springframework.cloud.bus.BusEnvironmentPostProcessor#getDefaultServiceId),以下:spring

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

若是發現app:index:id中的index不一致, 舉例yml配置:框架

vcap:
  application:
    instance_index: ${spring.cloud.config.profile}

或者直接修改bus.id的配置,以下:dom

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}

Diboot 輕代碼開發框架spa

相關文章
相關標籤/搜索