天天看點

springCloud 配置中心與github同步配置無法重新整理config-client

本文的配置适用于springCloud 2.X版本

(1)config-server所需要的依賴
<!--gradle的方式-->
<!--消息隊列包-->
implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
<!--github在倉庫發生變化時需要發送一個post請求,springCloud為了能接受這個含payload參數的請求,是以要引入這個包-->
implementation 'org.springframework.cloud:spring-cloud-config-monitor'
           
(2)config-server配置檔案
<!--要在其中添加-->
spring
    rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest

#禁用安全組,将接口暴露在外面,友善測試     
management:
  endpoints:
    web:
      exposure:
        include: "*"
           
(3)config-client所需要的依賴
<!--gradle的形式-->
implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
           
(4)config-clinet配置檔案
<!--要在其中添加-->
spring:
  cloud:
    bus:
      trace:
        #開啟監聽
        enabled: true
        #代表該執行個體,在github重新整理的時候要用到。
      id: ${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}
      
#禁用安全組,将接口暴露在外面,友善測試      
management:
  endpoints:
    web:
      exposure:
        include: "*"
           
(5)将重新整理的配置顯示出來
<!--在github更新完後重新整理完本地配置,如果要擷取這個重新整理的值,需要在controller中加上@RefreshScope的注解-->

<!--例如-->
@RestController
@RefreshScope
public class ProductController {

    @Value("${server.servlet.application-display-name}")
    private String displayName;

    /**
     * 提供輸出hello資訊
     *
     * @param name 名字
     * @return "hello @name,this is producer service message!"
     */
    @RequestMapping("hello")
    public String hello(@RequestParam String name) {
        return "hello " + name + ", this is " + displayName + " service message!";
    }
}
           
(6)在github中的配置
<!--需要在倉庫的設定中添加 webhooks-->

<!--webhooks的配置如下:-->
1.Payload URL(在倉庫更新時github會向你的配置中心發送一個請求,來重新整理你配置中心的配置)
2.Content type(參數類型)
3.secret 用作給POST的body加密的字元串。采用HMAC算法
4.選擇隻有在送出代碼的時候才會觸發

<!--其中具體配置-->
1.Payload URL:必須是可通路的域名!而且格式為http://域名/monitor?path=* 。在springboot2之前使用的是/bus/refresh。後來改成/actuator/bus-refresh,但是github他會附帶一個payload參數,這玩意好像解析不了,會給你預設為String。然後就報錯。是以要使用最新的接口/monitor,這時springCloud專門提供的。path是官方文檔說你所要重新整理到那個用戶端,*表示所有的config-client
2.Content type:application/json
           

遇到的錯誤:

springCloud 配置中心與github同步配置無法重新整理config-client
springCloud 配置中心與github同步配置無法重新整理config-client

看上面的body

{"timestamp":"2019-07-18T09:31:48.081+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 298] (through reference chain: java.util.LinkedHashMap[\"commits\"])","path":"/actuator/bus-refresh"}
           

是以之後的版本要使用/monitor這個

遇到的坑

你按照網上的一步步配置之後,你會發現,隻能重新整理config-server的
無論你怎麼搞都是重新整理不了config-client中的配置

有幸在github上廖師兄也提到了這個問題,解決方案是在config-client中加入

了一個配置。

位址在這裡可以去看看:https://github.com/spring-cloud/spring-cloud-bus/issues/124

spring:
  cloud:
    bus:
      id: ${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}
           

鳴謝:

https://blog.csdn.net/w405722907/article/details/86712781
https://blog.csdn.net/shijiujiu33/article/details/95975365

https://blog.csdn.net/antma/article/details/81369872

(含有詳細的配置,請參考)