springcloud config配置中心與github webhook實現動態更新配置

1、搭建config-server

  1. 新建config工程
    並將其註冊到eureka上

  2. 啓動類註解

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableConfigServer
    
  3. yml

    spring:
      application:
        name: config
      cloud:
        config:
          server:
            git:
              uri: https://github.com/xxx.git
              username: xxx
              password: xxxx
              basedir: 本地配置倉庫路徑
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    
    
    

    uri: git倉庫地址;username/password:github賬戶名和密碼;basedir:將遠程git拉取到本地的指定路徑

  4. github上創建相應倉庫及配置文件

  5. 瀏覽器端訪問url格式

    例如:http://localhost:8080/release/order-dev.yml

    release: 倉庫分支名稱;order:對應應用的名稱;-dev是環境,後面會詳述

  6. 結果(在release分支下的order-dev.yml與master中的相比,多了label: release

    [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-0drU4HFT-1571739178959)(/Users/jc/Library/Application Support/typora-user-images/image-20190920165111177.png)]

2、使用統一配置來配置order應用

核心要義:應用拿着自己需要的配置文件的名稱與環境標識,去找config-server應用(統一配置中心)從github上拉取對應的配置文件到本地

  1. 在order工程中引入依賴

    <!--        引入config-client依賴以使用統一配置-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-client</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
       	  <!-- >https://mvnrepository.com/artifact/org.springframework.cloud/spring->cloud-starter-bus-amqp -->
      		<dependency>
        		 <groupId>org.springframework.cloud</groupId>
       		<artifactId>spring-cloud-starter-bus-amqp</artifactId>
     		</dependency>
    
  2. yml

    spring:
      application:
        name: order
      cloud:
        config:
          discovery:
            enabled: true
            service-id: CONFIG
          profile: test
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    

    只留下應用名order和config相關配置,service-id:eureka上註冊的配置中心應用名;還有eureka的地址,以防eureka改地址後,連接不到eureka,就更不要提獲取CONFIG等了

    這裏的name+profile就拼接爲url中的order-dev

    將yml名稱修改爲bootstrap.yml

    作用是啓動引導,先執行yml,拉取遠程配置後,再啓動springboot,不然肯定要報錯數據庫連接失敗什麼的導致啓動失敗

至此,還沒做到自動更新配置,及github更新配置後,config-client對應應用要重啓纔可獲取最新配置

3、消息通知實現自動更新

即每次遠程配置更新後,配置中心通過消息通知對應應用

以order應用爲例:

  1. 配置中心引入依賴
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bus-amqp -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  1. 配置中心yml
spring:
application:
 name: config
cloud:
 config:
   server:
     git:
       uri: https://github.com/xxx.git
       username: xxx
       password: xxx
       basedir: 本地配置路徑
rabbitmq:
 host: ipxxxx
 port: 5672
 username: guest
 password: guest
eureka:
client:
 service-url:
   defaultZone: http://localhost:8761/eureka
management:
endpoints:
 web:
   exposure:
     include: "*"

其中,rabbitmq因爲我是部署在遠端服務器的docker上,故需要配置

此外,management.endpoints.web.exposure.include:"*"是暴露所有端口,目的是暴露出/actuator/bus-refresh

  1. order應用引入依賴(同配置中心),且yml加上rabbitmq相應配置,原因同上。

  2. 對應controller加@RefreshScope實現自動刷新

package com.jachincloud.order.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
  * @description:
  * @Author: JachinDo
  * @Date: 2019/09/20 17:09
*/
@RequestMapping("/env")
@RestController
@RefreshScope
public class EnvController {

 @Value("${env}")
 private String env;

 @GetMapping("/print")
 public String print() {
     return env;
 }
}
  1. 啓動上述兩個應用

  2. 此時,更改github上對應配置文件後,只需訪問/actuator/bus-refresh接口後,即可實現自動更新。

命令行訪問:在這裏插入圖片描述

  1. 在github上配置webhooks自動訪問/actuator/bus-refresh

注意紅色部分!!
在這裏插入圖片描述
至此,實現了github更新配置文件,應用自動拉取並更新,無需重啓應用

注意:url中的端口爲配置中心運行端口

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