第八篇:消息总线(Spring Cloud Bus)

前面几篇文章我们聊了Spring Cloud Config配置中心,当我们在更新Git上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用Spring Cloud Bus可以完美解决这一问题。

1.消息总线

Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用AMQP消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

2.消息总线的步骤

  1. 提交代码触发post给客户端A发送bus/refresh
  2. 客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus
  3. Spring Cloud bus接到消息并通知给其它客户端
  4. 其它客户端接收到通知,请求Server端获取最新配置
  5. 全部客户端均获取到最新的配置

3.准备

用到上篇文章工程,还需要电脑上安装上RabbitMQ(RabbitMQ怎么按请自行百度)

4.创建config-client-high-availability-rabbitmq项目

复制上一篇的config-client-high-availability项目,改成config-client-high-availability-rabbitmq,并在引入rabbitmq依赖spring-cloud-starter-bus-amqp

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>spring-cloud-config</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client-high-availability-rabbitmq</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-starter-->
        <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>
        <!--Rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

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

    </dependencies>

    <!--Maven打包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写配置文件application.properties,加RabbitMQ的配置

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
# 从配置中心读取文件
spring.cloud.config.discovery.enabled=true
# 置中心的servieId,即服务名
# 读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用
spring.cloud.config.discovery.serviceId=config-server
server.port=8881

# RabbitMq的配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

在controller层TestController类上加@RefreshScope注解,表明配置文件自动刷新

@RestController
@RefreshScope //配置文件自动刷新
public class TestClient {

    /**
     * http://localhost:8881/actuator/bus-refresh
     */

    @Value("${foo}")
    String foo;

    @RequestMapping(value = "/test")
    public String hi() {
        return foo;
    }

}

依次启动eureka-server,config-server-high-availability,config-client-high-availability-rabbitmq,访问http://localhost:8881/test,显示

now is 100

这是去仓库修改foo的值改为now is 200,配置文件foo的值发生了变化,此时用postman发送请求执行http://localhost:8881/actuator/bus-refresh

这是config-server-high-availability会重新读取配置文件,再访问http://localhost:8881/test,显示

now is 200

知识点

/actuator/bus-refresh接口可以指定服务,即使用"destination"参数,比如 “/actuator/bus-refresh?destination=haha**” 即刷新服务名为hahas的所有服务

源码:https://gitee.com/niugit/spring-cloud-config

原文地址:https://www.cnblogs.com/niudaben/p/12441424.html

时间: 2024-08-24 15:36:37

第八篇:消息总线(Spring Cloud Bus)的相关文章

SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

一.安装rabbitmq 二.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.ap

史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

最新Finchley版本请访问:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/或者http://blog.csdn.net/forezp/article/details/81041062 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改. 本文还是基于

Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus

在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很多个客户端都需要刷新改配置,通过这种方式去刷新也是一种非常痛苦的事情.那有没有什么办法让系统自动完成呢? 之前我们提到用githook或者jenkins等外部工具来触发.现在说另外一种思路, 如果refresh命令可以发送给config server,然后config server自动通知所有con

通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)

通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配置中心的总线服务会将此事件广播给加入总线的所有客户端,客户端收到事件后会从新读取配置中心的内容. 增加POM依赖 配置中心的服务端(spring-cloud-config-server)和客户端(spring-cloud-config-client)都加入Spring Cloud Bus引用包: <

第九章 消息总线: Spring Cloud Bus

在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实例都连接上来, 由于该主题中产生的消息会被所有实例监听和消费, 所以我们称它为消息总线. 在总线上的各个实例都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息, 例如配置信息的变更或者其他一些管理操作等 由于消息总线在微服务架构系统中被广泛使用, 所以它同配置中心一样, 几乎是微服务架构中的必备组件. Spring Cloud 作为微服务架构综合性的解决方案,对此自然也有自己的实现, 这

spring cloud 使用spring cloud bus自动刷新配置

Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ.Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化或者其他的管理指令.使用Spring Cloud Bus后的架构如图9-2所示. 图9-2 使用Spring Cloud Bus的架构图 由图可知,微服务A的所有实例通过消息总线连接到了一起,每个实例都会订阅配置更新事件.当其中一个微服务节点的/bus/refresh端点被请求时,该实例就会向消息总

【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践

一.整合步骤 1)加入Maven坐标 <!-- actuator监控模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.3.RELEASE</version> </dependency> &l

(十六) 整合spring cloud云架构 -使用spring cloud Bus刷新配置

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务. 当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多.配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效. 如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响. 如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯

(十六)JAVA springcloud ssm b2b2c多用户商城系统-使用spring cloud Bus刷新配置

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务. 当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多.配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效. 如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响. 如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯