电竞比分网-中国电竞赛事及体育赛事平台

分享

Spring Cloud(六)《基于github webhook動態(tài)刷新服務配置》

 小傅哥 2021-12-13

微信公眾號:bugstack蟲洞棧
沉淀、分享、成長,專注于原創(chuàng)專題案例,以最易學習編程的方式分享知識,讓自己和他人都能有所收獲。目前已完成的專題有;Netty4.x實戰(zhàn)專題案例、用Java實現JVM、基于JavaAgent的全鏈路監(jiān)控、手寫RPC框架、架構設計專題案例[Ing]等。

前言介紹

在實際開發(fā)中經常會有一個叫做配置中心的服務,這個服務經過變更參數來動態(tài)刷新線上業(yè)務數據行為配置。比如;行為開關、活動數據、黑白名單、本地/預發(fā)/線上環(huán)境切換等等,這些配置信息往往需要在我們不重啟系統(tǒng)的時候就可以被更新執(zhí)行。那么我們一般會使用具備此類屬性在分布式系統(tǒng)中適合的組件進行開發(fā)配置中心,像是zookeeper、redis發(fā)布訂閱、或者http定時輪許拉取,他們都可以做成統(tǒng)一配置中心服務。而在Spring Cloud Config 中,默認采用 Git 來存儲配置信息,所以使用 Spring Cloud Config 構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,在加上Github的Webhook鉤子服務,可以在我們push等行為操作的時候,自動執(zhí)行我們的http行為,以達到自動刷新配置服務。

環(huán)境準備

  1. jdk 1.8、idea2018、Maven3

  2. Spring Boot 2.0.6.RELEASE

  3. Spring Cloud Finchley.SR2

  4. 需要有一個Git賬號,用來創(chuàng)建配置中心以及開啟Webhooks服務,添加回調

案例說明

通過在個人Git創(chuàng)建配置服務工程,開啟Webhooks服務添加回調鉤子http://xxx:port/actuator/refresh在更新配置后自動刷新服務配置內容,如圖;
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-9dT8z67X-1573801291240)(https:///assets/images/pic-content/2019/11/springcloud-6-1.png)]

代碼示例

itstack-demo-springcloud-06├── itstack-demo-springcloud-config-client
│   └── src
│       └── main
│           ├── java
│           │   └── org.itstack.demo
│           │        ├── web
│           │        │   └── ConfigClientController.java      
│           │        └── ConfigClientApplication.java
│           └── resources   
│               ├── application.yml
│               └── bootstrap.yml
└── itstack-demo-springcloud-config-server
    └── src
        └── main
            ├── java
            │   └── org.itstack.demo   
            │        └── ConfigServerApplication.java
            └── resources   
                └── application.yml

完整代碼歡迎關注公眾號:bugstack蟲洞棧 回復“SpringCloud專題”進行下載

itstack-demo-springcloud-config-client | 配置獲取客戶端方,提供自動刷新Http

web/ConfigClientController.java & 添加注解@RefreshScope自動刷新配置

/**
 * 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
 * 論壇:http://
 * Create by 付政委 on @2019
 */@RestController@RefreshScopepublic class ConfigClientController {@Value("${info.profile:error}")private String profile;@GetMapping("/config")public Mono<String> config() {return Mono.justOrEmpty(profile);}}

ConfigClientApplication.java & 普通配置即可

/**
 * 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
 * 論壇:http://
 * Create by 付政委 on @2019
 */@SpringBootApplicationpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}

application.yml & 需要配置endpoints,這樣才可以暴漏刷新服務

spring:
  application:name: itstack-demo-springcloud-config-client
server:
  port: 9001# /actuator/refresh 這個 Endpoint 暴露出來
management:
  endpoints:web:  exposure:include: refresh

bootstrap.yml & 配置中心服務配置,http://localhost:7397

spring:
  cloud:config:  uri: http://localhost:7397  # 配置中心的具體地址;itstack-demo-springcloud-config-server
      name: config-client         # 對應 {application} 部分,例如;config-client-dev = 只取最后一個符號'-'之前的
      profile: dev                # 對應 {profile} 部分
      label: master               # 對應 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存儲,則該參數無用

#配置文件會被轉換成 Web,訪問規(guī)則如下;
#/{application}/{profile}[/{label}]#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties

itstack-demo-springcloud-config-server | 配置提供服務端方,鏈接Git配置工程地址

ConfigServerApplication.java & 添加注解@EnableConfigServer設置成配置服務中心

/**
 * 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
 * 論壇:http://
 * Create by 付政委 on @2019
 */@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}

application.yml &

server:
  port: 7397spring:
  application:name: itstack-demo-springcloud-config
  cloud:config:  server:git:  uri: https://github.com/fuzhengwei/itstack-demo-config  # 換成自己的配置Git倉庫的地址,如果沒有可以新建工程地址,也可以克隆我的
          search-paths: config-repo                               # Git倉庫地址下的底層配置文件名稱,如果配置多個用逗號','分割。

# 如果配置中心需要訪問權限,則開啟配置
# spring.cloud.config.server.git.username:Github賬戶
# spring.cloud.config.server.git.password:Github密碼

測試驗證

  1. 準備好自己Github的配置倉庫,也可以克隆我的Git;https://github.com/fuzhengwei/itstack-demo-config {有一組配置配置文件}

  2. 配置Webhooks,在https://github.com/換你自己的fuzhengwei/換你自己的itstack-demo-netty/settings/hooks/new

  3. 分別啟動服務

    1. itstack-demo-springcloud-config-server 配置Server

    2. itstack-demo-springcloud-config-client 配置Client

  4. 訪問配置服務,端口7397;http://localhost:7397/config-client/dev

    {
    "name": "config-client",
    "profiles": [
    "dev"
    ],
    "label": null,
    "version": "ea0b1a1017595d542aa01b8b2bda68f9620dd81a",
    "state": null,
    "propertySources": [
    {
    "name": "https://github.com/fuzhengwei/itstack-demo-config/config-repo/config-client-dev.yml",
    "source": {
    "info.profile": "dev bus"
    }
    }
    ]
    }
    info:profile: dev bus
    • /{application}/{profile}[/{label}]

    • /{application}-{profile}.yml

    • /{label}/{application}-{profile}.yml

    • /{application}-{profile}.properties

    • /{label}/{application}-{profile}.properties

    1. 訪問配置文件;http://localhost:8080/config-client-dev.yml {可以直接訪問查看配置信息}

    1. 訪問規(guī)則{配置文件會被轉換成 Web 接口,規(guī)則如下}

    1. 訪問結果

  5. 訪問使用配置的客戶端,端口9001;http://localhost:9001/config {可以提交配置代碼反復刷新測試}

dev bus

綜上總結

  1. Spring Cloud Config 可以很方便的依賴于Github提供的回調鉤子進行更新配置,同時也支持本地配置

  2. Webhooks 不止可以用于變更配置,還可以用于一起啟動觸發(fā)工程打包部署發(fā)布的行為

  3. 不要局限于知識點,往往每一個新知識所帶來的架構設計更值得學習,這些都可以靈活的用于項目系統(tǒng)中

    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多