Spring Cloud Bus实现自动刷新

一 项目简介
1 安装RabbitMQ
2 为项目整合Spring Cloud Bus并实现自动刷新。
二 实战
1 新建项目microservice-config-client-refresh-cloud-bus
2 为项目添加spring-cloud-starter-bus-amqp依赖
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <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>
  </dependencies>
3 修改bootstrap.yml内容
spring:
  application:
    name: microservice-foo    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      uri:http://localhost:8080/
      profile: dev            # profile对应config server所获取的配置文件中的{profile}
      label: master           # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
三 测试
1 启动microservice-config-server。
2 启动microservice-config-client-refresh-cloud-bus。
3 将microservice-config-client-refresh-cloud-bus端口改为8082,再启动一个节点。
4 访问http://localhost:8081/profile,此时获得结果:dev-1.0
5 将Git仓库中的microservice-foo-dev.properties文件内容修改为
profile=dev-1.0-bus
6 发送POST请求到一个Config Client实例的/bus/refresh
7 访问两个Config Client实例的/bus/refresh端点,会发现两个节点都返回profile=dev-1.0-bus

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/80873936