前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud Bus 消息总线

SpringCloud Bus 消息总线

作者头像
cheese
发布2023-10-25 11:21:57
2310
发布2023-10-25 11:21:57
举报
文章被收录于专栏:Java PorterJava Porter

概述

书接上回

  • 《SpringCloudConfig分布式配置中心》一文中说到
    • 假如有多个微服务客户端3355/3366/3377。。。。。。每个微服务都要执行一次post请求,手动刷新?太麻烦了
  • 分布式自动刷新配置功能
  • Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新。

SpringCloudBus消息总线

  • 是什么

  • Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,
  • 它整合了Java的事件处理机制和消息中间件的功能。
  • Spring Clud Bus目前支持RabbitMQ和Kafka。
  • 特点
    • Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。
  • 为什么被称为总线
    • 什么是总线

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

  • 基本原理

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

RabbitMQ环境配置

安装软件

开启RabbitMQ管理功能

  • 进入RabbitMQ的sbin目录
    • F:\software\RabbitMQ Server\rabbitmq_server-3.7.14\sbin
  • 执行RabbitMQ命令
    • rabbitmq-plugins enable rabbitmq_management
  • 添加了可视化插件
  • 访问http://localhost:15672/
  • 输入默认账密guest进入管理界面

SpringCloud Bus动态刷新全局广播

  • 完成RabbitMQ环境配置之后,就达到了SpringCloudBus动态刷新全局广播的环境要求
  • 演示广播效果,增加复杂度,再以3355为模板再制作一个3366

创建Module

  • 新建cloud-config-client-3366
  • pom文件
代码语言:javascript
复制
<?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>cloud2023</artifactId>
    <groupId>top.ljzstudy.springcloud</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

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

  <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.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>
  • 编写yaml文件
代码语言:javascript
复制
server:
  port: 3366

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

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • 主启动类
代码语言:javascript
复制
package top.ljzstudy.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3366.class, args);
    }
}
  • 业务类Controller
代码语言:javascript
复制
package top.ljzstudy.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${server.port}")
    private String serverPort;

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

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

设计思想

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

修改原有模块配置

给cloud-config-center-3344配置中心服务端添加消息总线支持
  • pom文件
代码语言:javascript
复制
<!--添加消息总线RabbitMQ支持-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yaml文件
代码语言:javascript
复制
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'
给cloud-config-client-3355客户端添加消息总线支持
  • pom文件
代码语言:javascript
复制
<!--添加消息总线RabbitMQ支持-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yaml文件
代码语言:javascript
复制
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
给cloud-config-client-3366客户端添加消息总线支持
  • pom文件
代码语言:javascript
复制
<!--添加消息总线RabbitMQ支持-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yaml文件
代码语言:javascript
复制
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

测试

  • 分别启动注册中心7001,配置中心3344,和两个客户端3355与3366
  • 模拟不同角色测试
    • 运维工程师
      • 修改Gitee上配置文件增加版本号;
代码语言:javascript
复制
  - 发送post请求刷新配置,实现一次修改,处处生效
代码语言:javascript
复制
POST http://localhost:3344/actuator/bus-refresh

HTTP/1.1 204 
Date: Thu, 21 Sep 2023 02:26:34 GMT
Keep-Alive: timeout=60
Connection: keep-alive

<Response body is empty>

Response code: 204; Time: 5503ms; Content length: 0 bytes
代码语言:javascript
复制
GET http://config-3344.com:3344/master/config-dev.yml

HTTP/1.1 200 
Content-Type: text/plain
Content-Length: 75
Date: Thu, 21 Sep 2023 02:29:31 GMT
Keep-Alive: timeout=60
Connection: keep-alive

config:
  info: master branch,springcloud-config/config-dev.yml version=24

Response code: 200; Time: 1312ms; Content length: 75 bytes
  • 客户端
    • 访问http://localhost:3355/configInfo
代码语言:javascript
复制
GET http://localhost:3355/configInfo

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 58
Date: Thu, 21 Sep 2023 02:56:25 GMT
Keep-Alive: timeout=60
Connection: keep-alive

master branch,springcloud-config/config-dev.yml version=24
代码语言:javascript
复制
  - 访问[http://localhost:3366/configInfo](http://localhost:3366/configInfo)
代码语言:javascript
复制
GET http://localhost:3366/configInfo

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 90
Date: Thu, 21 Sep 2023 02:55:20 GMT
Keep-Alive: timeout=60
Connection: keep-alive

serverPort: 3366	

 configInfo: master branch,springcloud-config/config-dev.yml version=24
  • 获取配置信息,发现都已经刷新了
  • 实现了一次修改,广播通知,处处生效

SpringCloud Bus动态刷新定点通知

  • 不想全部通知,只想定点通知,指定具体某一个实例生效而不是全部
    • 只通知3355
    • 不通知3366
  • 运维人员在发起post刷新请求时指定
    • [http://localhost:](http://localhost:)配置中心的端口号/actuator/bus-refresh/{destination}
    • /bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例
  • 再次修改远程库配置
  • 配置中心访问验证是否生效
代码语言:javascript
复制
GET http://config-3344.com:3344/master/config-dev.yml

HTTP/1.1 200 
Content-Type: text/plain
Content-Length: 75
Date: Thu, 21 Sep 2023 03:18:22 GMT
Keep-Alive: timeout=60
Connection: keep-alive

config:
  info: master branch,springcloud-config/config-dev.yml version=10


Response code: 200; Time: 1703ms; Content length: 75 bytes
  • 发起post刷新配置
代码语言:javascript
复制
POST http://localhost:3344/actuator/bus-refresh/config-client:3355

HTTP/1.1 204 
Date: Thu, 21 Sep 2023 03:21:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive

<Response body is empty>

Response code: 204; Time: 5392ms; Content length: 0 bytes
  • 客户端3355/3366查看配置
代码语言:javascript
复制
GET http://localhost:3355/configInfo

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 58
Date: Thu, 21 Sep 2023 03:22:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive

master branch,springcloud-config/config-dev.yml version=10

Response code: 200; Time: 16ms; Content length: 58 bytes
代码语言:javascript
复制
GET http://localhost:3366/configInfo

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 90
Date: Thu, 21 Sep 2023 03:22:29 GMT
Keep-Alive: timeout=60
Connection: keep-alive

serverPort: 3366	

 configInfo: master branch,springcloud-config/config-dev.yml version=24

Response code: 200; Time: 16ms; Content length: 90 bytes

通知信息流图

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
    • 书接上回
      • SpringCloudBus消息总线
      • RabbitMQ环境配置
        • 安装软件
          • 开启RabbitMQ管理功能
          • SpringCloud Bus动态刷新全局广播
            • 创建Module
              • 设计思想
                • 修改原有模块配置
                  • 给cloud-config-center-3344配置中心服务端添加消息总线支持
                  • 给cloud-config-client-3355客户端添加消息总线支持
                  • 给cloud-config-client-3366客户端添加消息总线支持
                • 测试
                • SpringCloud Bus动态刷新定点通知
                • 通知信息流图
                相关产品与服务
                微服务引擎 TSE
                微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档