SpringCloud配置中心使用bus消息总线动态刷新

1、config-server服务端
引入依赖

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

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

添加配置

spring
  rabbitmq:
    username: guest
    password: guest
    host: 127.0.0.1
    port: 5672
    virtual-host: /

#暴露端口
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

2、config-client客户端
引入依赖

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

添加配置

spring
  rabbitmq:
    username: guest
    password: guest
    host: 127.0.0.1
    port: 5672
    virtual-host: /

注:springboot的版本不能高于spring-cloud-stater-bus-amqp:如前者为2.0.1后者为2.0.0启动报错

测试:
1、修改githup仓库配置:添加hello2和hello8
2、用Insomnia模拟post请求http://localhost:9011/actuator/bus-refresh
3、config-client客户端控制台输出:Received remote refresh request. Keys refreshed [config.client.version, hello2, hello8]

猜你喜欢

转载自blog.csdn.net/weixin_40682142/article/details/87875591