1. 程式人生 > 實用技巧 >SpringCloudConfig-手動重新獲取配置檔案內容(單個服務通知+通過訊息佇列廣播)

SpringCloudConfig-手動重新獲取配置檔案內容(單個服務通知+通過訊息佇列廣播)

一、SpringCloudConfig定義

spring cloud config是一個基於http協議的遠端配置實現方式。通過統一的配置管理伺服器進行配置管理,客戶端通過https協議主動的拉取服務的的配置資訊,完成配置獲取。

二、為什麼要使用監聽的形式重新獲取配置檔案內容,而不是重啟服務

1、無需重啟服務,方便管理

三、使用(註冊中心內容不再展示)

1、單個服務通知的形式來更新配置檔案內容

(1)、遠端gitHub內容(config-client.yml)

spring:
  profiles:
    active: dev
---
server:
  port: 8081
spring:
  profiles: dev
  application:
    name: demo
-client eureka: client: service-url: defaultZone: http://localhost:8080/eureka/ # 是否將自己註冊為服務 register-with-eureka: true # 需要檢索檢索服務 fetch-registry: true name: test --- server: port: 8082 spring: profiles: test application: name: demo-client eureka: client: service
-url: defaultZone: http://localhost:8080/eureka/ # 是否將自己註冊為服務 register-with-eureka: true # 需要檢索檢索服務 fetch-registry: true name: test

(2)、ConfigServer的配置

  1、pom.xml的引用如下:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <
artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

  

  2、直接上配置檔案內容(application.yml)

spring:
  application:
    name: demo-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/SweetPiglet/spring-cloud-config.git
          username: your username
          password: your password
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    # 是否註冊為服務
    register-with-eureka: true
    # 是否進行掃描
    fetch-registry: true

(3)、ConfigClient配置內容

  1、pom.xml的配置如下:

                <!--引入監控-->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
                <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>                

  2、配置檔案的配置(bootstrap.yml)

# 引入ConfigServer的內容
spring:
  cloud:
    config:
      uri: http://localhost:9000/
      name: config-client
      profile: dev
      label: master
# 暴露監控點 management: endpoints: web: exposure: include: "*"

  3、controller層的內容

/**
 * wsq
 */
@RestController
@RefreshScope
public class TestController {
    # 需要重新整理什麼屬性,就寫什麼屬性
    @Value("${name}")
    private String name;
    @GetMapping("/test")
    public String test(){
        return name;
    }
}

(4)、執行程式

首先啟動eurekaserver,之後啟動configserver,最後啟動configclient。

之後進行介面的訪問:

http://localhost:9000/config-client-dev.yml

http://localhost:8081/test

之後修改配置檔案的內容,將name原來的test改成test1

然後在window黑視窗中輸入:

curl -X POST '''http://localhost:8081/actuator/refresh'

之後再次訪問連線,檢視變化,發現沒有重啟服務依然可以達到重新整理配置檔案的效果

2、監聽+訊息佇列來實現配置的更新

(1)、出現原因

如果運維工程師修改了很多個配置檔案,也就是說需要訪問每一個服務,當然可以寫一個指令碼,但是呢,如果有服務的增加就需要改動指令碼,那麼有沒有一種不需要進行配置的形式呢

(2)、作用

運維工程師直接通知一下configserver,configserver通過訊息佇列的topic形式,發一個通知,只要是訂閱了該通知的,都可以收到通知,進行相應配置的更新

(3)、做法

  1、首先,rabbitmq和erlang的安裝,不多說自行百度

  2、基於上面的程式碼,進行微調即可

  configserver需要新增的內容如下:

  pom.yml

           <!--監聽-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

  

  application.yml

# 連線rabbitmq
spring:  
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
# 配置監聽
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

  

  configclient的變更如下:

  pom.xml

            <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

 

 bootstarp.yml

# 引入rabbitmq
spring:  
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
# 暴露監控點
management:
  endpoints:
    web:
      exposure:
        include: "*"

(4)測試

首先啟動eurekaserver,之後啟動configserver,最後啟動configclient。

之後進行介面的訪問:

http://localhost:9000/config-client-dev.yml

http://localhost:8081/test

之後修改配置檔案的內容,將name原來的test改成test1

然後在window黑視窗中輸入:

curl -X POST '''http://localhost:9000/actuator/bus-refresh'

之後再次訪問連線,檢視變化,發現沒有重啟服務依然可以達到重新整理配置檔案的效果

完工

三、最後提一下定點通知:

curl -X POST '''http://localhost:9000/actuator/bus-refresh/config-client:8081'

也就是後面加上服務名加埠的形式來進行服務的定點重新整理