8.SpringCloud-Bus

Spring Cloud Config很好的解决了,配置文件服务化,通过Eureka注册服务,使其具有了高可用性。但是系统启动后,对于已经加载的配置文件,无法更改。通过Spring Cloud Bus可以解决这个问题。

1.在Config Client工程基础上加入依赖

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

2.安装rabbitmq
mac下可使用brew安装
安装 brew install rabbitmq
启动 brew services start rabbitmq
rabbitmq默认端口5672

3.在需要读取配置的类类加入注解

@RefreshScope

4.修改配置文件

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: development
      discovery:
        enabled: true
        serviceId: config-server
  rabbitmq:
    host: localhost
    port: 5672
#    username:
#    password:
server:
  port: 8012
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/
management:
  security:
    enabled: false

5.按顺序启动ConfigEurekaServer,一个Config Server,2个Config Client
访问2个Config Client http://localhost:8012/hi http://localhost:8013/hi
由于配置中指定了profile: development,可以看到读取了config-client-development.properties文件

返回结果:springcloud-dev-test

6.修改配置并提交
将project的值改为springcloud-dev提交github

7.调用刷新接口
调用任一Config Client接口均可,要注意用POST方式请求
http://localhost:8012/bus/refresh http://localhost:8013/bus/refresh

8.查看配置更新情况
再次请求2个Config Client http://localhost:8012/hi http://localhost:8013/hi
可以看到已经加载修改后的配置文件
返回结果:springcloud-dev

猜你喜欢

转载自blog.csdn.net/gaojingyuan/article/details/79083088