Spring Cloud学习记录 08Spring Cloud Bus服务总线

Spring Cloud学习记录 08Spring Cloud Bus服务总线,第1张

Spring Cloud学习记录 08Spring Cloud Bus服务总线 Bus 服务总线

Spring Cloud Bus 是轻量的消息代理,将分布式节点连接,用于广播配置文件的更新 或 监控管理

补充说明&优点

  • Bus 是轻量级消息代理
  • Bus 主要实现 配置的自动更新 (无需配置)
  • Bus 底层基于 RabbitMQ实现
  • 广播配置文件、监控管理
存在问题

上篇文章的 Config配置中心 启动后,此时更改 远端仓库 中某一微服务的配置文件,是不会同步本地缓存中的配置文件。因此在某一微服务运行后是不会及时更新配置文件,而是需要重启微服务才能更新生效

问题示例

  • 以下主要展示 更改配置不会同步的问题
  • 上篇章代码测试
  1. 修改仓库 user-service 微服务的配置文件 user-dev.yml

    添加 test.name 属性 (用于测试)

    依旧应用 user-service 进行测试

  2. 修改Controller user-service (在接口进行测试返回数据)

    @RestController
    @RequestMapping ("user")
    public class UserController {
        
        @Autowired
        private UserService service;
        
        @Value("${test.name}")
        private String name;
        
        @GetMapping ("name")
        public String testConfig() {
            return name;
        }
        
        //···
    }
    

    添加一个接口用于返回配置中的属性name的接口

  3. 运行测试1(查看刚刚在仓库中添加的属性)
    依次启动 Eureka 、config-server 、user-server 三个服务
    访问 http://localhost:9091/user/name ,下图返回表示成功

  4. 修改仓库 user-service 微服务的配置文件 user-dev.yml
    主要修改 test.name 属性的值

    # 其他就不展示了
    test : 
        name : Sanscan12
    
  5. 浏览器再次访问 http://localhost:9091/user/name

    此时 配置中的 test.name 属性值没有更变

  6. 重启 user-service 微服务 ,再次访问http://localhost:9091/user/name

    Git仓库中配置文件的修改并没有及时更新到 微服务,只有重启微服务才能生效

Bus应用

Bus 构架图

说明:

  • 应用前提需要安装 RabbitMQ 才能实现功能
  • 配置中心 需要自行暴露触发总线地址 /actuator/bus-refresh
  • 请求 /actuator/bus-refresh 是固定的
  • 当配置中心微服务接收到 /actuator/bus-refresh 请求时,向 RabbitMQ发送配置改变消息,这时也会更新本地缓存
  • 微服务 会监听 RabbitMQ ,如果坚监听到有仓库配置改变的消息,会重新读取本地缓存中的配置文件(实现不用重启提醒更新的效果)

应用示例

  1. 首先安装 RabbitMQ ,安装即可无需配置

  2. config-server 添加 Bus依赖

    
        org.springframework.cloud
        spring-cloud-bus
    
    
        org.springframework.cloud
        spring-cloud-stream-binder-rabbit
    
    
  3. config-server 添加配置 application.yml

    # ...
    spring :
        # ···
        # 配置 rabbitmq信息,如果都是与默认值一致无需配置
        rabbitmq :
            host : localhost
            port : 5672
            username : guest
            password : guest
    
    management :
        endpoints :
            web :
                exposure :
                	# 暴露触发消息总线的地址 
                	# POST /actuator/bus-refresh
                    include : bus-refresh
    

    消息总线的地址,主要用途在触发更新配置的请求!

  4. user-service 添加依赖 Bus、监控管理

    
        org.springframework.cloud
        spring-cloud-bus
    
    
        org.springframework.cloud
        spring-cloud-stream-binder-rabbit
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
  5. user-service 添加配置 bootstrap.yml (配置与默认一致可跳过)

    # ....
    # 配置 rabbitmq信息,如果都是与默认值一致无需配置
    rabbitmq :
        host : localhost
        port : 5672
        username : guest
        password : guest
    
  6. user-service 修改 UserController 添加注解 @RefreshScope

    @RestController
    @RequestMapping ("user")
    @RefreshScope // 自动刷新配置文件
    public class UserController {
    	// ....
    }
    
  7. 测试

    1. 依次启动 Eureka 、config-server 、user-server 三个服务
    2. 访问 http://localhost:9091/user/name ,查看页面返回的属性值
    3. 修改 Git仓库 user-service 微服务的配置文件 user-dev.yml
      主要修改 test.name 属性的值 (值可以随意,自要展示效果即可)
    4. POST请求:http://localhost:14000/actuator/bus-refresh (配置中心的端口 14000)触发消息总线进行刷新配置
    5. 重新访问 http://localhost:9091/user/name (返回更改后的值表示成功)

    说明: POST请求可通过 IDEA内置HTTP调用 (善用Ctrl+F)


仓库代码 : https://gitee.com/Sanscan12/spring-cloud-examples.git

官方文档 : Spring Cloud Bus 参考手册

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5116677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存