SpringCloud Bus消息总线

    科技2026-01-10  15

    在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。

    SpringCloud中也有对应的解决方案,SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来,可以很容易搭建消息总线,配合SpringCloud config 实现微服务应用配置信息的动态更新。

     

     

    消息代理属于中间件。设计代理的目的就是为了能够从应用程序中传入消息,并执行一些特别的操作。开源产品很多如ActiveMQ、Kafka、RabbitMQ、RocketMQ等 目前springCloud仅支持RabbitMQ和Kafka。本文采用RabbitMQ实现这一功能。

     

    搭建分布式配置中心

    Config架构

     

    当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

     

    消息代理属于中间件。设计代理的目的就是为了能够从应用程序中传入消息,并执行一些特别的操作。开源产品很多如ActiveMQ、Kafka、RabbitMQ、RocketMQ等 目前springCloud仅支持RabbitMQ和Kafka。本文采用RabbitMQ实现这一功能。

     

    Git环境搭建

    使用码云环境搭建git服务器端  

    码云环境地址:https://gitee.com/yushengjun/events

    Git服务器上传配置文件

    命名规范 服务名称-版本.yml 例如configclient_dev.yml

     

    搭建Eureka 注册中心

    搭建config-server

    Maven依赖

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.0.2.RELEASE</version>

    </parent>

    <dependencies>

    <!-- SpringBoot整合Web组件 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <!--spring-cloud 整合 config-server -->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-config-server</artifactId>

    <version>2.0.2.RELEASE</version>

    </dependency>

     

    <!-- SpringBoot整合eureka客户端 -->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

    <version>2.0.2.RELEASE</version>

    </dependency>

     

     

    </dependencies>

    <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

    <repositories>

    <repository>

    <id>spring-milestones</id>

    <name>Spring Milestones</name>

    <url>https://repo.spring.io/libs-milestone</url>

    <snapshots>

    <enabled>false</enabled>

    </snapshots>

    </repository>

    </repositories>

     

     

    application.yml

    ###服务注册到eureka地址

    eureka:

      client:

        service-url:

               defaultZone: http://localhost:8100/eureka

    spring:

      application:

        ####注册中心应用名称

        name: config-server

      cloud:

        config:

          server:

            git:

              ###git环境地址

              uri: https://gitee.com/itmayi/config_003.git

              ####搜索目录

              search-paths:

                - config  

          ####读取分支      

          label: master

    ####端口号      

    server:

      port: 8888

     

     

    启动config-server

    @SpringBootApplication

    @EnableEurekaClient

    @EnableConfigServer

    public class AppConfigServer {

     

    public static void main(String[] args) {

    SpringApplication.run(AppConfigServer.class, args);

    }

     

    }

     

    读取配置文件http://127.0.0.1:8888/configclient-dev.yml

    搭建config-client

    Maven依赖

     

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.0.1.RELEASE</version>

    </parent>

     

    <dependencyManagement>

    <dependencies>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-bus-parent</artifactId>

    <version>2.0.0.RC2</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    </dependencies>

    </dependencyManagement>

    <dependencies>

    <!-- SpringBoot整合Web组件 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

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

    <version>2.0.2.RELEASE</version>

    </dependency>

    <!-- SpringBoot整合eureka客户端 -->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

    <version>2.0.2.RELEASE</version>

    </dependency>

    <!--核心jar包 -->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-bus-amqp</artifactId>

    </dependency>

    <!-- actuator监控中心 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

     

    </dependencies>

    <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

    <repositories>

    <repository>

    <id>spring-milestones</id>

    <name>Spring Milestones</name>

    <url>https://repo.spring.io/libs-milestone</url>

    <snapshots>

    <enabled>false</enabled>

    </snapshots>

    </repository>

    </repositories>

     

     

    application.yml

     

    spring:

      application:

        ####注册中心应用名称

        name:  configclient

      cloud:

        config:

        ####读取后缀

          profile: dev

          ####读取config-server注册地址

          discovery:

            service-id: config-server

            enabled: true

    #####    eureka服务注册地址    

    eureka:

      client:

        service-url:

               defaultZone: http://localhost:8100/eureka    

    server:

      port: 8882

     

     

    启动config-client

    @RestController

    public class ConfigClientController {

    // 年龄

    @Value("${userAge}")

    private String userAge;

     

    @RequestMapping("/userAge")

    public String userAge() {

    return userAge;

    }

     

    }

     

     

    @SpringBootApplication

    @EnableEurekaClient

    public class AppConfigClient {

    public static void main(String[] args) {

    SpringApplication.run(AppConfigClient.class, args);

    }

    }

    刷新配置文件

    1.手动采用actuator端点刷新数据

    2.采用bus广播通知

     

    手动actuator端点刷新数据

     

    Maven依赖信息

    <!-- actuator监控中心 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

     

    Bootstrap.xml新增

    开启监控断点

    management:

      endpoints:

        web:

          exposure:

            include: "*"

    生效前提

    在需要刷新的Bean上添加@RefreshScope注解。

    @RestController

    // @SpringBootApplication

    @RefreshScope

    public class ConfigClientController {

     

    http://127.0.0.1:8882/actuator/refresh 

    @Value("${itmayieduInfo}")

    private String itmayieduInfo;

    当配置更改时,标有@RefreshScope的Bean将得到特殊处理来生效配置

    手动刷新接口

    Post请求手动刷新

     

    http://127.0.0.1:8882/actuator/refresh  启动刷新器 从cofnig server读取

     

     

    http://127.0.0.1:8882/actuator/bus-refresh

     

    使用消息总线

    三个项目中都加上该依赖信息

    Maven依赖

    <!--核心jar包 -->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-bus-amqp</artifactId>

    </dependency>

    <!-- actuator监控中心 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

     

    配置文件

    ###开启bus刷新

    management:

      endpoints:

        web:

          exposure:

            include: bus-refresh

     

     

     

    刷新接口 http://127.0.0.1:8882/actuator/bus-refresh

     

     

     

     

     

     

     

     

     

    Processed: 0.017, SQL: 9