Spring Cloud Bus消息总线 Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件功能,Spring Cloud Bus目前支持整合RabbitMQ和Kafka。上一个config配置中心,需要对每一个配置读取的客户端手动发送POST命令刷新,使用Spring Cloud Bus可是实现更加简单的方式来实现。

1. 简介

1.1 总线

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

1.2 基本原理

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

2. RabbitMQ安装

为了节省时间直接使用Docker安装RabbitMQ,Linux安装和使用Docker我之前简单的写过,下面直接使用Docker安装RabbitMQ.

2.1 拉取镜像

docker pull rabbitmq

2.2 查看所有镜像

docker images

2.3 启动容器

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v `pwd`/data:/springcloud/rabbitmq --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin a9ea0b189b4f

上面几个参数说明

  • -d 后台运行容器;
  • --name 指定容器名;
  • -p 指定服务运行的端口(5672:应用访问端口;15672:控制台Web端口号);
  • -v 映射目录或文件;
  • --hostname 主机名(RabbitMQ的一个重要注意事项是它根据所谓的 “节点名称” 存储数据,默认为主机名);
  • -e 指定环境变量;(RABBITMQ_DEFAULT_VHOST:默认虚拟机名;RABBITMQ_DEFAULT_USER:默认的用户名;RABBITMQ_DEFAULT_PASS:默认用户名的密码)

2.4 启动rabbitmq_management

docker exec -it rabbitmq rabbitmq-plugins enable rabbitmq_management

rabbitmq 为镜像的应用名称

2.5 开放端口

上 面映射的web端口是15672,需要放行对应的端口才能访问

2.6 访问Web页面

完成之后开始访问web页面

使用指定的账号和密码登录

此时就完成了RabbitMQ的安装,同时需要开放RabbitMQ的应用运行端口5672

3. Bus全局广播

新建一个 cloud-config-client3366 和之前的 cloud-config-client3355项目几乎一致。整个设计思想为 由cloud-config-center3344配置中心服务端 通知 cloud-config-client3366cloud-config-client3355 更新配置。

3.1 center3344

cloud-config-center3344 添加消息总线依赖支持

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

然后在配置文件中添加下面的相关配置

server:
  port: 3344

spring:
  application:
    name: cloud-config-center

  cloud:
    config:
      server:
        git:
          uri: https://github.com/yremp/spring-cloud-config.git
          search-paths: spring-cloud-config
      label: master

  rabbitmq:
    host: 39.105.173.178
    port: 5672
    username: admin
    password: admin
    virtual-host: "/"

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'

3.2 client3355

添加依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

增加配置

server:
  port: 3355

spring:
  application:
    name: config-client

  cloud:
    config:
      label: master  #分支名称
      name: config   #配置文件名称
      profile: dev   #读取后缀名称   #上面的配置读取master的config-dev.yml
      uri: http://localhost:3344   #配置中心地址

  rabbitmq:
    host: 39.105.173.178
    port: 5672
    username: admin
    password: admin
    virtual-host: "/"

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

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

Controller

package live.yremp.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 ConfigController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configinfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}

3.3 client3366

依赖添加

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

修改配置 bootstrap.yml

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master  #分支名称
      name: config   #配置文件名称
      profile: dev   #读取后缀名称   #上面的配置读取master的config-dev.yml
      uri: http://localhost:3344   #配置中心地址

  rabbitmq:
    host: 39.105.173.178
    port: 5672
    username: admin
    password: admin
    virtual-host: "/"

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

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

Controller

package live.yremp.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 ConfigController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configinfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}

4. 测试环境

先去GitHub看一下config-dev.yml ,verson=4

启动几个项目,先分别查看一下,首先是 Eureka7001 ,查看服务的注册情况

访问cloud-config-center3344 查看配置读取情况

访问cloud-config-client3355 查看从 cloud-config-center3344 读取的配置情况

访问cloud-config-client3366 查看从 cloud-config-center3344 读取的配置情况

都是正常读取,下面我修改配置文件的version版本号为5

此时去访问 cloud-config-center3344 读取新的配置

再测试两个客户端

想要多个客户端自动更新配置需要向 cloud-config-center3344 配置中心发送一个POST请求

curl -X POST "http://localhost:3344/actuator/bus-refresh"

发送完成之后没有任何响应信息,这就是正常情况,此时不重启两个客户端项目重新访问读取配置文件

在项目后台的控制台也有相应的信息输出

2020-07-24 14:20:12.249  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2020-07-24 14:20:12.259  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/localhost:config-client:3355 - deregister  status: 200
2020-07-24 14:20:12.279  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2020-07-24 14:20:12.279  INFO 9200 --- [o-z3bA3D7J9kQ-1] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2020-07-24 14:20:12.279  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2020-07-24 14:20:12.289  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2020-07-24 14:20:12.289  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2020-07-24 14:20:12.289  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2020-07-24 14:20:12.289  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Application is null : false
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2020-07-24 14:20:12.369  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : The response status is 200
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1595571612379 with initial instances count: 3
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application CONFIG-CLIENT with eureka with status DOWN
2020-07-24 14:20:12.379  WARN 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1595571612379, current=DOWN, previous=STARTING]
2020-07-24 14:20:12.379  INFO 9200 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/localhost:config-client:3355: registering service...
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application CONFIG-CLIENT with eureka with status UP
2020-07-24 14:20:12.379  WARN 9200 --- [o-z3bA3D7J9kQ-1] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1595571612379, current=UP, previous=DOWN]
2020-07-24 14:20:12.379  INFO 9200 --- [o-z3bA3D7J9kQ-1] o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed [config.client.version, config.info]
2020-07-24 14:20:12.389  INFO 9200 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/localhost:config-client:3355 - registration status: 204
2020-07-24 14:25:12.385  INFO 9200 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

这就完成了修改配置后不用手动刷新每一个项目,只需要向配置中心发一个POST请求,其他从这个配置中心读取的配置都会自动更新。

标签云

ajax AOP Bootstrap cdn Chevereto CSS Docker Editormd GC Github Hexo IDEA JavaScript jsDeliver JS樱花特效 JVM Linux Live2D markdown Maven MyBatis MyBatis-plus MySQL Navicat Oracle Pictures QQ Sakura SEO Spring Boot Spring Cloud Spring Cloud Alibaba SpringMVC Thymeleaf Vue Web WebSocket Wechat Social WordPress Yoast SEO 代理 分页 图床 小幸运 通信原理

Spring Cloud Bus消息总线
Spring Cloud Bus消息总线