微服務系列之-Config配置刷新操作集成

一.前言

我們前面兩個篇幅講解了配置中心的本地配置讀取模式、遠程配置讀取模式。這一篇幅來講解如何配置遠程刷新機制來動態刷新服務配置,不用再用以前的老辦法通過停止服務、重新發包來更新服務配置。

原理圖以及運行關鍵步驟:
在這裏插入圖片描述

  1. 提交配置觸發post請求給server端的bus/refresh接口
  2. server端接收到請求併發送給Spring Cloud Bus總線
  3. Spring Cloud bus接到消息並通知給其它連接到總線的客戶端
  4. 其它客戶端接收到通知,請求Server端獲取最新配置
  5. 全部客戶端均獲取到最新的配置

二.功能實現

1.服務端配置:

  1. 添加依賴
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  1. 文件配置
 bus:
      enabled: true
      trace:
        enabled: true
    inetutils:
      ignoredInterfaces:
        - docker*
        - veth*
        - lo
  rabbitmq:
        host: localhost
        port: 5672
        username: admin
        password: 123456
management:
  endpoints:
      web:
        exposure:
          include: info,bus-refresh
  endpoint:
    bus-refresh:
      enabled: true
  1. 啓動類關鍵註解
    在這裏插入圖片描述
    這裏我們用rabbitmq爲消息總線,具體的rabbitmq的安裝以及配置參考:Windows下啓動RabbitMQ服務及安裝詳解
    在這裏插入圖片描述

2.客戶端配置:

  1. 依賴
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 主要配置
    本地模式下不需要配置rabbitmq
config:
      uri: http://localhost:8084/
      label: master
#      failFast: true
      #      本地配置文件訪問
      profile: gr
      retry:
        initial-interval: 2000
        max-interval: 2000
        max-attempts: 2000
        multiplier: 1.2
    # 動態刷新配置
    bus:
      refresh:
        enabled: true

management:
  #  security:
  #    enabled: false
  #  endpoints:
  #    web:
  #      exposure:
  #        include: info,bus-refresh
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
  1. 啓動類
    在這裏插入圖片描述

  2. 刷新類
    在這裏插入圖片描述在這裏插入圖片描述
    通過配置中心日誌輸出可以看到,消息總線已經成功運行
    在這裏插入圖片描述

三.結果驗證

  1. 正確讀取數據
    在這裏插入圖片描述

  2. 遠程配置修改刷新
    在這裏插入圖片描述誤區:因爲springboot2.0改動比較大,之前一直使用"/bus/refresh"方法已經調整,後面的都是用“actuator/bus-refresh”路徑來刷新,嗶了狗,害我白白浪費了一大把的時間各種找問題,發現配的都正確就是刷新不了配置。

curl -v -X POST "http://localhost:8084/actuator/bus-refresh"

在這裏插入圖片描述

總體而言還是很容易構建配置刷新操作,但是有時一點小誤區痛徹心扉,不過這次出錯發現這個問題,而且還做了記錄下次就不會忘了,之前很早之氣也搭建了這個框架,第二次又掉坑,下次不會了,哈哈!!!

項目地址:微服務項目集成框架

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章