• springCloud Bus消息总线


    springCloud Bus

    是什么?

        springCloud Bus 是用来将分布式系统的节点与轻量级消息系统连接起来的框架,它整合了java的事件处理机制和消息中间件的功能。 Bus支持两种消息代理:RabbitMQ和 Kafka。

    作用:

         Bus可以管理和传播分布式系统间的消息,就像一个分布式的执行器,可以用于广播状态更改、事件推送等,也可以当做微服务间的通信通道。

    比如:分布式自动刷新配置功能:SpringCloud Bus 与 SpringCloud Config 使用可以实现配置的动态刷新。

    Bus动态刷新全局广播设计思想:

    1:利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置。

    2:利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,从而刷新所有的客户端配置。

    显然第二种方式更加适合,1不适合原因:

       打破了微服务单一职责的性,因为微服务本身是业务模块,不应该承担配置刷新的职责。

       破坏了微服务各个节点的对等性。

       有一定的局限性,例如微服务迁移时,网络地址变化就需要配置。

    使用:

    以下以RabbitMQ为例:需要先安装rabbitMQ自己百度

    1:动态刷新全局配置:

    在config服务端和客户端添加RabbitMQ的支持

    <!-- 添加消息总线RabbitMQ支持 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    
    <!--需要添加actuator健康监测 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    在需要刷新的位置添加 @RefreshScope

     config-center yml:

      # rabbitmq相关配置
    spring:
      rabbitmq:
         host: localhost
         port: 5672
         username: guest
         password: guest
    
    # rabbitmq相关配置, 暴露bus刷新配置的端点
    management:
      endpoints:
        web:
          exposure:
            include: 'bus-refresh'

    client yml:

    # rabbitmq相关配置
    spring:
      rabbitmq:
         host: localhost
         port: 5672
         username: guest
         password: guest
    
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    最后像config center 服务端发送刷新通知,让服务配置中心通知各个客户端更新配置:

    使用 postman发送post 请求到   http://localhost:config服务端端口号/actuator/bus-refresh 刷新配置

    2:动态刷新定点刷新:

    公式:http://localhost: 配置中心的端口号/actuator/bus-refresh/微服务名字:微服务端口号

    注意区分大小写,以下示例:

  • 相关阅读:
    JAVA List对象与json串互相转换
    git错误提示:fatal: remote origin already exists.
    eclipse中js中ctrl+鼠标点击方法出现 the resource is not on the include path of a javaScript project
    JS根据日期获取这周的周一
    jquery基本操作
    redux+react-redux+示例的快速上手体验
    用 async/await 来处理异步
    Git使用详细教程
    SVN里直接把本地目录纳入管理
    修改npm安装的全局路径和配置环境变量的坑
  • 原文地址:https://www.cnblogs.com/dw3306/p/12702256.html
Copyright © 2020-2023  润新知