使用Spring Cloud Config搭建配置中心(實現自動刷新)

在實際開發中,一般分爲四個環境

  • pro環境:生產環境。
  • pre環境:灰度環境。
  • test環境:測試環境。
  • dev環境:開發環境。

登錄github,分別創建config-pro.properties、config-test.properties、config-dev.properties、
config-second-test.properties
在這裏插入圖片描述
內容分別爲

username=pro
password=pro
username=test
password=test
username=dev
password=dev
username=secondtest
password=secondtest

創建Config Server端

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dfyang</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml文件如下,如果倉庫時公開的,則不需要填寫用戶名和密碼,只需填倉庫地址即可

server:
  port: 8085
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/dfyang55/test/
          #username: 
          #password: 

啓動類上加上@EnableConfigServer註解

package com.dfyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

啓動項目
使用Config Server的端點獲取配置文件的內容,端點與配置文件的映射規則如下:

  • /{application}/{profile}/[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

{application}:微服務名稱
{label}:對應得Git倉庫分支,默認master
{profile}:可看作文件名末尾的部分

eg.config-second-test.properties可以是test,也可以是second-test

(1)訪問 http://localhost:8085/config-pro.properties
在這裏插入圖片描述
(2)訪問 http://localhost:8085/config-second/test
(訪問 http://localhost:8085/config/second-test 將得到同樣效果)
在這裏插入圖片描述

創建Config Client端

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dfyang</groupId>
    <artifactId>config-client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml,這裏只修改端口號

server:
  port: 8086

創建bootstrap.properties,跟application.yml在一起(bootstrap.yml會先於application.yml 加載)
這裏我們需要加載config-dev.properties
{application} = config
{profile} = dev
{label} = master

#對應Config Server所獲取的配置文件中的{application}
spring.application.name=config
#Git倉庫的分支
spring.cloud.config.label=master
#對應Config Server所獲取的配置文件的{profile},這裏表示獲取開發版的配置信息
spring.cloud.config.profile=dev
#Config Server的地址
spring.cloud.config.uri= http://localhost:8085/

編寫測試接口

package com.dfyang.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ClientController {
    @Value("${username}")
    String username;

    @Value("${password}")
    String password;

    @RequestMapping(value = "/info")
    public String info(){
        return "(生產環境)用戶名:" + username + ",密碼:" + password;
    }
}

啓動項目,訪問 http://localhost:8086/info
在這裏插入圖片描述

如何在配置文件修改後,Config Client能夠讀取到新的數據?

Config Client引入依賴
在SpringCloud2.0以後,沒有/refresh手動調用的刷新配置地址,所以我們這裏需要使用actuator來實現刷新。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

開啓refresh,也可以輸入"*" 開啓所有endpoint

management:
  endpoints:
    web:
      exposure:
        include: refresh

加上@RefreshScope註解

@RestController
@RefreshScope
public class ClientController {
	//......
}

重啓Config Client
修改config-dev.properties文件

username=dev1
password=dev1

訪問 http://localhost:8085/config-dev.properties
可以看到我們修改的內容
在這裏插入圖片描述
訪問 http://localhost:8086/info
發現並沒有得到更新,因爲這裏我們需要手動刷新
在這裏插入圖片描述
在這裏插入圖片描述
再次訪問 http://localhost:8086/info
在這裏插入圖片描述
使用/actuator/refresh需要手動執行,如果我們有100臺機器使用該配置文件,意味着我們需要手動刷新100次,
那麼如何解決這個問題?

使用Spring Cloud Bus

Spring Cloud Bus使用輕量級的消息代理連接分佈式系統的節點,從而廣播傳播狀態的更改。
這裏使用RabbitMQ作爲消息代理。
Linux安裝RabbitMQ

修改項目

引入依賴

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

bootstrap添加rabbitmq信息

#rabbitmq信息
spring.rabbitmq.host=192.168.195.123
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=151310

由於在Spring boot 2.0之後/bus/refresh整合到了actuator裏面,所以還是老老實實把全部都開啓

management:
  endpoints:
    web:
      exposure:
        include: "*"

創建或者copy Config Client項目,僅端口號不同(我這裏是8087)

啓動兩個項目,訪問 http://localhost:8086/infohttp://localhost:8087/info ,目前均爲如下
在這裏插入圖片描述
修改config-dev.properties
在這裏插入圖片描述
在這裏插入圖片描述
再次訪問如下,也就是說如果使用Spring Cloud Bus,即使我們有一百臺使用同一個配置,配置發生了變更,我們只需要執行一次刷新即可。
在這裏插入圖片描述
Spring Cloud Bus原理應該如下
在這裏插入圖片描述

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