SpringCloud07_消息总线(Bus)

本文介绍了Spring Cloud Bus的概念和原理,它通过RabbitMQ或Kafka作为消息中间件实现配置的动态刷新。文中详细阐述了如何配置RabbitMQ,以及在服务端和客户端分别进行的配置步骤。通过使用消息总线,可以实现配置服务的统一刷新,避免了客户端之间的直接通信。此外,还展示了如何进行定点通知,只刷新指定的服务实例。最后,总结了动态刷新和消息总线在微服务架构中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、消息总线Bus

(1)基本概念与原理
总线:

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

消息总线Bus:

  • 概念:配合spring cloud config实现配置的自动动态刷新,支持RabbitMQ和Kafka消息中间件;
  • 原理:ConfigClient实例监听MQ中同一个topic(默认SpringCloudBus),当一个服务刷新数据时候,就会把这个信息放入topic,这样它监听的同一个tpoic的服务就能得到通知,然后更新自身配置。

(2)RabbitMQ配置与安装

  • 安装Erlang
  • 安装RabbitMQ
  • 进入RabbitMQ安装目录下sbin目录
  • 输入命名启动管理功能:rabbitmq-pligins enable rabbitmq_management
  • 查看安装插件

(3)实现
有两种实现方式:

  • 通知其中一个客户端,再由该客户端通知其他客户端;
    在这里插入图片描述

  • 通知一个服务端,而刷新所有客户端配置;
    在这里插入图片描述

以上两种方式选择第二种,原因如下:

  • 破坏各客户端之前平衡性,增加该客户端压力

以下代码实现在上一节消息配置基础上完成。

1.3.1 服务端配置

第一步:服务端增加消息总线依赖
pom.xml

  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
<?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>cloud2022</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>cloud-config</groupId>
    <artifactId>cloud-config</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.commons</groupId>
            <artifactId>commons</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

第二步:配置RabbitMQ与Bus刷新端点
application.yml

#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#暴露bus刷新端点
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
server:
  port: 3344

eureka:
  client:
    register-with-eureka: true #是否要注册
    fetchRegistry: true #是否抓取注册信息
    service-url:
      defaultZone: http://eureka7001:7001/eureka #,http://eureka7002:7002/eureka
  instance: #修改主机名
    instance-id: config3344
    prefer-ip-address: true #访问路径显示IP

spring:
  application:
    name : config
  cloud:
    config:
      server:
        git:
#          uri: git@github.com:wozaixianaichilamian/springboot_config.git #仓库路径
          search-paths: -springboot_config #仓库名称
          uri: https://gitee.com/wozaixianaichilamian/springboot_config
      label: master #分支名称

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver
    
#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#暴露bus刷新端点
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
1.3.2 客户端端配置

第一步: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>cloud2022</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>config-cilent</groupId>
    <artifactId>config-cilent</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

</project>

第二步:配置RabbitMQ,无需配置刷新

#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "*"
server:
  port: 3355

eureka:
  client:
    register-with-eureka: true #是否要注册
    fetchRegistry: true #是否抓取注册信息
    service-url:
      defaultZone: http://eureka7001:7001/eureka #,http://eureka7002:7002/eureka
  instance: #修改主机名
    instance-id: config3355
    prefer-ip-address: true #访问路径显示IP

spring:
  application:
    name : config-client
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件地址
      profile: dev #读取后缀名称
      uri: http://localhost:3344 #配置中心地址
     # 以上组合:http://config3344:3344/mastre/config-dev

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver

    #   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "*"

第三步:启动消息服务端与客户端
在这里插入图片描述

第四步:修改gitu配置
在这里插入图片描述

第五步:cmd使用post刷新服务端
在这里插入图片描述
第七步:访问客户端与服务端:刷新服务端同时客户端也进行了刷新
第八步:登录RabbitMQ:可以看到ConfigClient实例监听MQ中同一个topic。(SpringCloudBus)在这里插入图片描述
(4)动态刷新的定点通知

  • 只通知一部分客户端进行刷新,另一部分不刷新。
  • 服务端永远自动刷新

命令公式:

http://localhost:{配置端口号}/actuator/bus-refresh/{需要变更配置的服务或者实例}

curl -POST "http://localhost:3344/actuator/bus-refresh/config-cilent:3355"
//config0cilent 为配置文件yml中的application.name,3355为服务端口号

(5)总结
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值