使用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原理应该如下
在这里插入图片描述

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