spring cloud config+bus实现配置中心的自动刷新

  • 流程图:

根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:

1、提交代码触发post给config server发送bus/refresh

2、config server接收到请求从git端更新配置并且发送给Spring Cloud Bus

3、Spring Cloud bus接到消息并通知给其它客户端

4、其它客户端接收到通知,请求Server端获取最新配置

5、全部客户端均获取到最新的配置

 

  • 上代码

  1. 我的环境: springboot:2.3.1  springcloud:Hoxton.SR6(均为当前最新)
  2. 三个服务:Eureka服务 ConfigServer服务 ConfigClient服务
  3. 能学到config和bus的就不上EurekaServer的代码了(很简单,eureka服务端口号8888)

ConfigServer服务代码(端口号8889)

  •  pom.xml
 <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>

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


    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
  • application.yml
server:
  port: 8889
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/joseph349/spingcloudconfig.git
          search-paths: config-repo2
          default-label: master
          username:
          password:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:                  #SpringCloud 2.0.0版本以后暴露接口方式
    web:
      exposure:
        include: "*"

#服务注册到eureka地址
eureka:
  client: #客户端注册进eureka服务列表内
#    register-with-eureka: true #表示是否将自己注册进eurekaServer默认为true
#    fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须为true才能配合ribbon使用负载均衡
    service-url:
      defaultZone: http://localhost:8888/eureka
  • 启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

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

}
  • 我的git配置(如果用的是dev环境,文件名必须含有dev)

 


ConfigClient服务(端口号8881)(敲黑板...下面的文件都很重要)

  • pom.xml
 <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!--bus消息总线动态刷新配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
  • bootstrap.properties(bootstrap优先级高于application文件)
server.port=8881
spring.application.name=config-client

spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri= http://localhost:8889/
##默认 false,这里设置 true,表示开启读取配置中心的配置
#spring.cloud.config.discovery.enabled=true
##对应 eureka 中的配置中心 serviceId,默认是 configserver
#spring.cloud.config.discovery.serviceId=config-server
## 开启消息跟踪
#spring.cloud.bus.trace.enabled=true
##启动失败时能够快速响应
#spring.cloud.config.fail-fast=true

eureka.client.service-url.defaultZone: http://localhost:8888/eureka/

##暴露shutdown端点服务
management.endpoints.web.exposure.include=*

##rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  • 启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {

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

}
  • 测试类
@RestController
@RefreshScope
public class TestController {
    @Value("${neo.hello.hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return hello;
    }
}
  • 测试结果

依次启动EurekaServer服务,ConfigServer服务,ConfigClient服务

第一次测试 http://localhost:8881/hello 结果如下图

然后修改git中的值为 neo.hello.hello=hello dev22222222

重要的一步,利用actuaror刷新服务(pom中已加入该依赖)

http://localhost:8881/actuator/bus-refresh 一定要POST请求

这时configClient服务控制台很显示刷新记录

再次测试http://localhost:8881/hello,如下图

当然这只是测试,如果产品正式上线肯定不会这么做,可以用gitLab/gitEe/gitHub去设置的,这里就不做演示了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值