SpringCloud (第九篇)整合消息总线 Bus

前面两篇文章我们聊了Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。
Spring Cloud Bus的一个功能就是让这个过程变得简单,当远程Git仓库的配置更改后,只需要向其中的一个微服务实例发送一个Post请求,通过消息组件通知其它微服务实例重新拉去配置文件即可。

一、 SpringCloud Bus简介

Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,
也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。Spring Cloud Bus可选的消息代理组件包括RabbitMQ,AMQP和Kafka等。
整体实现架构图:


image.png

架构图分析

架构图的简单分析:
⑴Eureka-Server服务
        这个服务主要是用来服务的注册和发现的,它是一个注册中心,我们会将Config Server,Config Client这些服务都注册进去。
⑵ Config Server服务
        这个是一个分布式配置中心Config Server,通过它我们可以从远处Git仓库读取我们需要的配置文件,所以,
Config Client  服务可以通过连接它来获取自己需要的配置信息。
⑶ Config Client服务
        这个服务是ConfigClient客户端,它需要从ConfigServer服务端获取自己的配置文件信息。
⑷ 消息总线
           可以理解为一个消息代理,它可以将分布式的节点都连接起来,也可以完成各个应用程序节点间的相互通信, 这里我们主要用来广播配置文件的更改,
  并且消息总线的可选消息代理组件包括RabbitMQ,AMQP和Kafka等,这 里我们选中的是RabbitMQ组件。
⑸/bus/refresh
         这个是用来刷新配置的请求,当远程仓库的配置文件修改后,我们不需要重新启动各个子节点,只需向某一个 子节点发送一个Post请求即可,
消息总线会自动通知其它各个节点进行配置文件的刷新。

二、 SpringCloud Bus 使用

1. 准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,已在本机上安装。安装教程:https://blog.csdn.net/qq_41307443/article/details/80865409

image.png

2. 改造 cloud_13_config_client_high 模块

复制子模块 cloud_13_config_client_high 该名称 cloud_14_config_client_bus ,修改子模块的pom,并引入父模块。


image.png

在父模块 引入 子模块:


image.png

3. 在 子模块的pom中添加依赖

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

完整依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 服务注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--config-config 的服务依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- 消息总线 Bus -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

4. 修改配置文件

修改 bootstrap.yml ,修改如下:

server:
  port: 9105

## spring cloud eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:9090/eureka/

# spring config
spring:
  application:
    ####注册中心应用名称
    name:  config-client


  cloud:
      config:
        #  github 上的 配置文件前缀名称
        name: config-client
        label: master
        profile: dev
        ####读取config-server注册地址
        discovery:
          #指定server端的name,也就是server
          service-id: config-server
          #开启Config服务发现支持
          enabled: true

# 新添加 Bus 配置
      bus:
        enabled: true
        trace:
          enabled: true
# 新添加 RabbitMQ
  rabbitmq:
    host: 192.168.1.107
    port: 5672
    username: root
    password: 123456
management:
  endpoint:
  endpoints:
    web:
      exposure:
        include: bus-refresh

注意:这里我一开始使用了http的端口 15672,发生了上述异常。后面改成了 5672 可以成功连接。映射2个RabbitMQ有2个端口:5672端口是客户端和RabbitMQ及通信的接口,15672端口为管理界面访问web界面的端口。

5. 修改启动类

在启动类上添加注解

@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class CloudClientBusApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudClientBusApplication.class, args);
    }
}

6. 控制类读取配置文件

@RefreshScope
@RestController
public class ConfigClientController {

    // 果获取不到冒号前的配置,则使用冒号后作为默认值
   //@Value("${yaosy:yaosy default}")
    @Value("${yaosy}")
    private String yaosy;

    @Value("${github}")
    private String github;

    @GetMapping("/yaosy")
    public String getYaosy(){
        return yaosy ;
    }
}

注意:注解 @RefreshScope //配置文件自动刷新

7. 启动服务并测试

依次启动eureka-cloud_eureka_01、 cloud_12_config_server_high, cloud_14_config_client_bus,端口为:9106。
启动成功后会自动添加RabbitMQ队列,如图:


image.png

在浏览器中访问:http://127.0.0.1:9106/yaosy 页面显示:

image.png

进入 github 将yaosy的值改为 : “yaosy version 2.0.0”,即改变配置文件 yaosy 的值。如果是传统的做法,需要重启cloud_14_config_client_bus服务,才能达到配置文件的更新。

image.png

然而通过Bus,我们只需要发送post请求:http://127.0.0.1:9106/actuator/bus-refresh,你会发现cloud_14_config_client_bus会重新读取配置文件。
在这里我使用 postman 进行post请求发送,http://127.0.0.1:9106/actuator/bus-refresh .

image.png

请求发送后 cloud_14_config_client_bus 进行重新读取配置文件。
image.png

刷新浏览器页面:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容