六、服务总线


一、Spring Cloud Bus


1. 是什么

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

2. 能干嘛

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

3. 什么是总线

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

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

4. WIndows 安装 RabbitMQ


4.1 安装 Erlang

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


4.2 安装 RabbitMQ

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


4.3 启动 RabbitMQ
  • rabbitmq-plugins enable rabbitmq_management
    在这里插入图片描述
    在这里插入图片描述

4.4 访问

http://localhost:15672/
用户名:guest,密码:guest


5. 动态刷新全局广播通知


5.1 设计思想
  1. 利用消息总线,触发一个客户端 Config Client 的 /bus/refresh 端点,而刷新所有客户端的配置。
    在这里插入图片描述
  2. 【推荐】利用消息总线,触发一个服务端 Config Server 的 /bus/refresh 端点,而刷新所有客户端的配置。
    在这里插入图片描述
  • 图二的架构显然更加适合,图一不适合的原因如下。
  1. 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责。
  2. 破坏了微服务各节点的对等性。
  3. 有一定的局限性。
    例如:微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。

5.2 修改 Module cloud-config-center3344
  • 给配置中心服务端,添加消息总线支持。

5.2.1 POM
<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

5.2.2 YML
spring:
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

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

5.3 修改 Module cloud-config-client3355
  • 给客户端,添加消息总线支持。

5.3.1 POM
<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

5.3.2 YMLbootstrap.yml
spring:
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

5.4 新建 Module cloud-config-client3366
  • 给客户端添,加消息总线支持。

5.4.1 POM
<?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>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client3366</artifactId>

    <dependencies>
    	<!--添加消息总线RabbitMQ支持-->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

5.4.2 YMLbootstrap.yml
server:
  port: 3366

spring:
  application:
    name: cloud-config-client
    
  cloud:
    # Config客户端配置http://config3344.com:3344/master/config-dev.yml
    config:
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 读取后缀名称
      profile: dev
      # 配置中心地址
      uri: http://localhost:3344
   
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

5.4.3 主启动
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {

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

5.4.4 ConfigClientController
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${server.port}")
    private String serverPort;

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

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return "serverPort: " + serverPort + "\t\n\nconfigInfo: " + configInfo;
    }
}

5.5 测试
  1. 修改 Github上配置文件增加版本号。
  2. curl -X POST "http://localhost:3344/actuator/bus-refresh"
    在这里插入图片描述
  3. 配置中心
    http://config3344.com:3344/config-dev.yml
  4. 客户端
    http://localhost:3355/configInfo
    http://localhost:3366/configInfo
  • 一次修改,广播通知,处处生效。
    在这里插入图片描述
    在这里插入图片描述

6. 动态刷新定点广播通知

  • 不想全部通知,只通知 3355,不通知 3366
    公式:http://localhost:3344/actuator/bus-refresh/{destination}
  • /bus/refresh 请求不再发送到具体的服务实例上,而是发给 Config Server,并通过 destination 参数类指定需要更新配置的 服务 或 实例。

6.1 测试
  • 刷新 3355
    curl -X POST "http://localhost:3344/actuator/bus-refresh/cloud-config-client:3355"

7. 通知总结

在这里插入图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑士梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值