SpringCloud微服务-SpringCloud Bus 消息总线

一、概述

在这里插入图片描述
在这里插入图片描述

什么是总线
在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。

基本原理
ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。

二、RabbitMQ环境配置

(1) Erlang和RabbitMQ的版本存在严格的对应关系,官网:https://www.rabbitmq.com/which-erlang.html,在官网可以查看相应的对应关系

(2)下载对应的windows版本
Erlang官网下载地址:https://www.erlang.org/downloads
RabbitMQ官网下载地址:https://www.rabbitmq.com/download.html

(3)先安装Erlang,然后安装RabbitMQ

安装Erlang:
在这里插入图片描述
安装RabbitMQ:
在这里插入图片描述
(4)启动并且配置RabbitMQ:

进入RabbitMQ安装目录下的sbin目录,在地址栏输入cmd回车,输入以下命令启动管理功能,默认账号密码都是guest

rabbitmq-plugins enable rabbitmq_management

在这里插入图片描述

三、SpringCloud Bus动态刷新全局广播

(1)刷新客户端配置的方式

a)利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置
在这里插入图片描述
b) 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置(更加推荐)

在这里插入图片描述

图二的架构显然更加合适,图一不适合的原因如下:

1.打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
2.破坏了微服务各节点的对等性
3.有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改

(2)配置消息总线支持

给cloud-config-center-3344配置中心服务端添加消息总线支持

pom依赖

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

yml配置

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/dream_and_zen/sprincloud-config.git
          search-paths:
            - springcloud-config
      label: master

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest


eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka


management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

给cloud-config-center-3355客户端添加消息总线支持

pom依赖

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

yml配置

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

(3)进行配置测试

修改Github上配置文件增加版本号
发送Post请求,一次发送,处处生效,获取配置信息,发现都已经刷新了

curl -X POST "http://localhost:3344/actuator/bus-refresh

四、SpringCloud Bus动态刷新定点通知

不想全部通知,只想定点通知,指定具体某一个实例生效而不是全部,/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例

公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

例如:curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值