Spring Cloud Eureka--BUS组件刷新配置

x33g5p2x  于2021-11-22 转载在 Spring  
字(2.6k)|赞(0)|评价(0)|浏览(238)

BUS 配置刷新

  • 消息总线,实现动态配置刷新,通过配置中心向MQ服务发送一个刷新指令,其他配置中心客户端接收刷新指令,执行配置刷新

1.添加依赖(2349)

bus rabbitmq binder-rabbit

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

2.修改09的添加依赖

actuator

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

3.修改09yml配置

暴露bus-refresh刷新路径
rabbitmq连接配置

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/pigeon01/springcloud1
          search-paths: config
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin
    virtual-host: /    
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

4.修改234的yml配置

  • 添加rabbitmq连接
  • 修改config的配置
  • 提交推送到远程仓库
spring:
  application:
    name: item-service
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin
    virtual-host: /
#item 8001 user8101 order8201
server:
  port: 8001
  cloud:
    config:
      override-none: true  #防止从配置中心下载的配置,覆盖本地命令参数
eureka:
  client:
    service-url:
      # 可以从云服务商购买不同地点的 eureka服务器
      # 这里可以改成云服务商提供的地点
      # 自己的服务器只能用defaultZone
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

消息服务案例

1. BUS配置刷新测试

1.向http://localhost:6001/actuator/bus-refresh提交一个post请求
2.观察2.3.4控制台是否重新连接6001,刷新配置

刷新指令如何只发送到03模块??

有选择地可以发送至想发送的模块,使用的是主题交换机

2.测试03的配置添加新用户,让03刷新配置

1.修改userServiceImpl 添加@RefreshScope

  • 如果不添加这个注 解,即使刷新到新的配置,也不会向对象重新注入新配置

2.重启03

3.修改config目录的user-service-dev.yml

添加99用户

4.提交到远程仓库

5.让03刷新配置

POST http://localhost:6001/actuator/bus-refresh/user-service

6.访问新的用户数据

GET http://localhost:3002/user-service/99

选择正确网卡,注册ip不注册主机名

选择正确网卡

bootstrap.yml

ignore-interfaces:   #忽略的网卡

spring:
  cloud:
    inetutils:
      preferred-networks:
        - 172\.18\.10\..+

注册ip,不注册主机名

application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/pigeon01/springcloud1
          search-paths: config
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin
    virtual-host: /
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

实现效果

相关文章