SpringCloud+git配置中心(Kafka+bus消息總線實現全局刷新)

git創建全局配置文件

  1. 登陸git賬戶,然後新建一個倉庫
    在這裏插入圖片描述
  2. 填寫倉庫名稱,選公有倉庫
    在這裏插入圖片描述
    3.創建一個新文件,命名爲config-dev.properties(注意這裏名稱有講究),我這裏個人習慣用properties配置,喜歡yml配置的同理修改即可,文件內容可以根據實際項目配置,我這裏隨便配個玩一玩兒
    在這裏插入圖片描述
    在這裏插入圖片描述

zoopkepper+kafka搭建

參考: zookeeper+kafka環境搭建配置

搭建config服務配置中心

  • pom文件導入,這裏我不寫版本號是因爲在父工程已經繼承了依賴,這一塊對微服務搭建不瞭解的可以先去了解一下微服務環境搭建和版本搭配相關問題
<!--actuator監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--這三個包配套導入萬無一失-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        
       <!--服務配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency> 
  • application.properties配置文件
server.port=3355

spring.application.name=service-config-server
##註冊eureka
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/

##uri就是前面創建的倉庫地址
spring.cloud.config.server.git.uri=https://github.com/*******/config-server.git

##當然遠程訪問需要你的git賬號和密碼
spring.cloud.config.server.git.username=78**3****@qq.com
spring.cloud.config.server.git.password=c**n****

##這個表示遠程倉庫的配置文件在什麼路徑下
spring.cloud.config.server.git.search-paths=config-server

#文件在遠程倉庫哪個分支下
spring.cloud.config.server.git.default-label=master

#指定kafka的代理地址,裝好kafka默認端口號就是9092
spring.kafka.bootstrap-servers=localhost:9092

# 指定默認消費者group id
spring.kafka.consumer.group-id=test

#actuator監控器暴露端口,不指定的話默認只暴露/health和/info,將不能實現全局監控效果
management.endpoints.web.exposure.include=*

#打開自動刷新功能
spring.cloud.bus.refresh.enabled=true
  • 啓動類
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class configServer3355 {
    public static void main(String[] args) {
        SpringApplication.run(configServer3355.class,args);
    }
}

搭建微服務客戶端

  • pom.xml
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        
        <!--跟配置中心的差不多,區別在這裏-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.2.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • application.properties配置文件
server.port=3333
spring.application.name=consumer-config-client-3333
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/
  • bootstrap.properties配置文件,注意客戶端多了一個這個配置文件,spring boot的加載屬由文件的優先級決定的,你想要在加載屬性之前去config server配置中心上取配置文件,意思也就是說bootstrap.properties的加載是先於application.properties的。
#指定git上的分支
spring.cloud.config.label=master

#指定git上的配置文件名,前面說過git上的配置文件命名是有說法的config-dev
spring.cloud.config.name=config
spring.cloud.config.profile=dev

#指定向誰取配置文件,這裏是向前面做的配置中心3355取配置
spring.cloud.config.uri=http://localhost:3355/

#暴露端口
management.endpoints.web.exposure.include=*

#打開自動刷新
spring.cloud.bus.refresh.enabled=true

#指定kafka的地址
spring.kafka.bootstrap-servers=localhost:9092
  • 啓動類+controller
@SpringBootApplication
@EnableEurekaClient
public class ConfigClient3333 {
   public static void main(String[] args) {
       SpringApplication.run(ConfigClient3333.class,args);
   }
}

@RestController
@RefreshScope
public class HelloController {
       @Value("${chuan.haha}")
       private String hello;

       @RequestMapping("/hello")
       public String from() {
           return this.hello;
       }
}

相同的方法再做一個客戶端(爲了體現一處配置中心修改處處生效,所以再做一個客戶端)

  • 最後的啓動效果,先啓動Eureka服務,再啓動3355配置中心,再啓動剩下兩個客戶端。
    在這裏插入圖片描述

工作完成我們來進行測試一下

  • 配置中心訪問git倉庫的規則,application相當如本案例的“config”,profie相當於“dev”,lable就是指git分支
    /{application}/{profile}/[label]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

  • 如圖配置中心訪問成功在這裏插入圖片描述

  • 兩個客戶端的訪問結果怎麼樣子呢?
    在這裏插入圖片描述
    在這裏插入圖片描述

  • 看來都訪問到了配置中心的配置了,結束了嗎?還沒有,我們的目的是修改配置時候讓每個客戶端都生效,此時我們打開我們的github,把配置文件的內容修改一下,把版本號修改爲2然後提交。
    在這裏插入圖片描述
    在這裏插入圖片描述

  • 點開3355配置中心的的web進行刷新,果然變爲2了
    在這裏插入圖片描述

  • 點開3344和3333客戶端刷新呢?會怎麼樣?
    在這裏插入圖片描述
    此時發現無論怎麼刷新,3344和3333客戶端都還是1版本,怎麼回事,說好的全局配置呢,是哪裏配置出錯了嗎?

別慌還有最後一步

  • 其實這裏並不是配置或者代碼有問題,先總結一下,我們的流程是怎麼走的,首先我們是3355配置中心向git倉庫拉去配置文件,然後3344和3333客戶端分別向3355再拉配置文件,也就是github->3355->3344&3333

  • 借用一個圖可以看清楚點
    在這裏插入圖片描述
    這裏可以看到,我們綠色這個配置中心做了,上面的git倉庫也有了,右邊的2個客戶端我們也有了(當然圖裏有4個客戶端),那我們還缺什麼?顯然下面還有一個小電腦圖標寫着POST /bus/refresh

下載一個postman

  • 下載一個postman,沒聽說這個工具的可以瞭解一下,應該很簡單,可以百度直接下載到,免費的,就是模擬發送post請求的一個工具。
    在這裏插入圖片描述
    這裏我們根據圖,向3355配置中心發送一個post請求

最後的最後測試

  • 刷新3333和3344
    在這裏插入圖片描述
    在這裏插入圖片描述
    終於出現我們要的效果啦!

總結

  • 首先我是一個新人,這是我寫的第一篇博客,我也是自己學習做的一個小案例做爲練手,順便寫寫學習筆記,能夠幫助大家一點點的話我也是心滿意足了,
  • 還有關於應該注意的坑,我也沒想到有什麼坑,只要細心一點相信都不會有什麼太大問題,主要是這裏github修改後不要重啓客戶端,因爲重啓的話一定是會讀取最新的配置,我們的目的就是爲了不重啓客戶端自動讀取配置嘛,重啓了就沒有意義了,在工作中也是,總不能修改完配置文件幾十個微服務全部重啓吧。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章