SpringCloud Bus 消息总线

一、概述

在 SpringCloud Config 中,一旦 GItHub 中的配置信息发生修改了,config 客户端如果想要获取到最新的配置信息,就必须发送 POST 请求来刷新客户端。而当有多个微服务客户端时,那么每个微服务都要执行一次post请求,进行手动刷新?那有没有什么方式可以实现一次通知,处处生效?

1.1 介绍

SpringCloud Bus 配合 SpringCloud Config 使用,可以实现配置的动态刷新。
在这里插入图片描述
SpringCloud Bus 是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了 Java 的事件处理机制和消息中间件的功能。
SpringCloud Bus 目前支持 RabbitMQ 和 Kafka。

1.2 作用

SpringCloud Bus 能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、时间推送等,也可以当作微服务间的通信通道。
在这里插入图片描述

1.3 总线概念

在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。

在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。

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

1.4 两种动态刷新方式

(1)利用消息总线触发一个客户端 /bus/refresh,而刷新所有客户端的配置
在这里插入图片描述
(2)利用消息总线触发一个服务端 ConfigServer 的 /bus/refresh 的断点,而刷新所有客户端的配置
在这里插入图片描述
第二种方式更加适合,第一种方式不适合的原因如下:

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

下面的案例采用的第二种方式来进行动态刷新。

二、RabbitMQ 环境配置

使用 Docker 安装,安装步骤可参考:docker安装RabbitMQ

安装完成后,访问:http://localhost:15672,输入账号密码:guest/guest
在这里插入图片描述

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

3.1 新建子模块 cloud-config-client-3366

3.1.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3366</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

3.1.2 bootstrap.yml

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master  # 分支名称
      name: config  # 配置文件名称
      profile: dev  # 读取后缀名成 上述3个综合:master 分支上 config-dev.yml 的配置文件被读取 http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.1.3 主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientMain3366 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3366.class, args);
    }
}

3.1.4 业务类 Controller

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

3.2 配置中心服务端添加消息总线支持

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

pom.xml:

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

application.yml:

server:
  port: 3344

spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/penggithub123/springcloud-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

# 暴露 bus 刷新配置的端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

3.3 客户端 3355 添加消息总线支持

给 cloud-config-client-3355 配置中心服务端添加消息总线支持。

pom.xml:

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

bootstrap.yml:

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master  # 分支名称
      name: config  # 配置文件名称
      profile: dev  # 读取后缀名成 上述3个综合:master 分支上 config-dev.yml 的配置文件被读取 http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

3.4 客户端 3366 添加消息总线支持

给 cloud-config-client-3366 配置中心服务端添加消息总线支持。

pom.xml:

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

bootstrap.yml:

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master  # 分支名称
      name: config  # 配置文件名称
      profile: dev  # 读取后缀名成 上述3个综合:master 分支上 config-dev.yml 的配置文件被读取 http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

3.5 测试

依次启动 eureka7001、configCenter3344、configClient3355、configClient3366 服务。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改 GitHub 上配置文件,将版本号从 3 改为 4
在这里插入图片描述
配置中心服务端可以立即获得最新的配置信息:
在这里插入图片描述
但是,客户端并不能立即获取到最新的配置信息,需要通过控制台发送以下 POST 请求(我没成功,一直报405,网上的方法也都试过了):curl -X POST "http://localhost:3344/actuator/busrefresh"

如果只想指定具体某一个实例生效而不是全部 ,可以使用下面这种方式:

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

/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例。

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值