Spring Cloud(12)——Spring Cloud Bus 消息总线

705 阅读5分钟

Spring Cloud(11)——Spring Cloud Config 分布式配置中心中,我们为微服务提供了集中化的尾部配置支持,但是还不够完善,比如不能实现自动刷新功能,这个要依赖Spring Cloud Bus 配合 Config来实现。

1、Bus 消息总线概述

什么是总线

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

什么是Spring Cloud Bus

Spring Cloud Bus 是用来将分布式系统的节点与轻量级消息系统连接起来的框架,它整合了java的事件处理机制和消息中间件功能。

Spring Cloud Bus 目前支持 RabbitMQ 和 Kafka。

Spring Cloud Bus 能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当做微服务的通信通道。

两种设计思想

1.利用消息总线触发一个客户端 /bus/refresh ,而刷新所有的 Config 客户端的配置,如下图:

在这里插入图片描述

2、利用消息总线触发一个服务端 Config Server 的 /bus/refresh ,而刷新所有的 Config 客户端的配置,如下:

img

第二种设计思想更合适,第一种不合适的原因为:

  • 打破了微服务的职责单一性,因为微服务本身是业务模块,他不应该承担配置刷新职责。
  • 破坏了微服务各节点的对等性。
  • 有一定的局限性,例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。

接下来的动态刷新全局广播就用第二种设计架构。

2、RabbitMQ环境配置

1、安装Erlang

由于RabbitMQ是基于erlang的,所以,在正式安装RabbitMQ之前,需要先安装一下erlang。

  1. 官网下载地址:www.erlang.org/downloads 不建议下最新版本

  2. 安装:Windows 64位选择:==OTP 23.2 Windows 64-bit Binary File (248)== ,安装时只需注意安装路径,其他next即可

  3. 配置环境变量

在这里插入图片描述

配置完上面的之后 找到系统变量中的PATH 点击编辑:

新建输入==%ERLANG_HOME%\bin==即可。

  1. 测试是否安装成功

    cmd输入 erl -version 显示版本号就说明安装成功

在这里插入图片描述

安装成功!

2、安装RabbitMQ

  1. 官网下载地址:www.rabbitmq.com/download.ht… 在这里插入图片描述

  2. 安装:安装时只需注意安装地址,其他next即可

添加可视化插件

进入RabbitMQ安装目录下的sbin目录:

在这里插入图片描述

输入命令:rabbitmq-plugins enable rabbitmq_management

在这里插入图片描述

访问地址查看是否安装成功:

输入请求:http://localhost:15672/

在这里插入图片描述

默认账号密码:guest guest

输入后点击Login:

在这里插入图片描述

RabbitMQ环境配置完成!

3、Spring Cloud Bus 动态刷新全局广播

搭建环境

Spring Cloud(11)——Spring Cloud Config 分布式配置中心中,我们搭建了Config 客户端cloud-config-client-3355,以3355为模板,搭建3366

1、创建cloud-config-client-3366模块

2、导入pom依赖

<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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>


    <dependency>
        <groupId>com.cheng.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

</dependencies>

3、编写bootstrap.yml配置文件

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: main  #分支名称
      name: application #配置文件名称
      profile: dev  #读取后缀名称    上面三个拼接起来就是  main分支下的application-dev.yml配置文件
      uri: http://localhost:3344  #config服务端地址  http://config-3344.com:3344/main/application-dev.yml

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

#暴露监控端口
management:
  endopints:
    web:
      exposure:
        include: "*"

4、创建主启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3366.class,args);
    }
}

5、编写业务类

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo; //获取配置信息

    @Value("${server.port}")
    private String serverPort;

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

给cloud-config-center-3344 config 服务端添加消息总线支持

该模块在Spring Cloud(11)——Spring Cloud Config 分布式配置中心已创建。

1、pom中添加依赖

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

2、yml配置文件中添加下面配置

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

添加完的yml文件如下:

server:
  port: 3344

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Wpengcheng/springcloud-config.git  #github仓库地址
          #搜索目录
          search-paths:
            - springcloud-config
      #读取分支
      label: main
#rabbitmq相关配置      
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest


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


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

给cloud-config-client-3355 客户端添加消息总线支持

1、pom中添加依赖

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

2、yml配置文件中添加下面配置

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

给cloud-config-client-3366 客户端添加消息总线支持

1、pom中添加依赖

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

2、yml配置文件中添加下面配置

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

测试

1、启动cloud-eureka-server7001

2、启动cloud-config-center-3344 config服务端

3、启动cloud-config-client-3355 和 cloud-config-client-3355 config客户端

修改github上的配置文件的信息,

然后发送post请求:curl -X POST "http://localhost:3344/actuator/bus-refresh"

这里的请求是发给config 服务端的,只需发送一次,全局都会生效。

4、Spring Cloud Bus 动态刷新定点通知

指定具体某一个实实例生效,而不是全部。

公式:http://localhost:3344/actuator/bus-refresh/{destination}

3344为配置中心的端口号

bus/refresh请求不再发送到具体的服务实例上,而是发给config server 并通过destination参数类型指定需要更新配置的服务或实例。

比如我们只通知服务名为config-client,端口为3355的实例刷新配置,

只要发送 curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

destination参数为 服务名:端口号