SpringCloud Bus动态刷新中心化配置

说明

上一篇我们介绍了配置中心实战,并留下了一个配置修改后如何实现自动刷新的问题,下面就实际操作一下,首先是手动刷新单个微服务,其次是利用消息总线实现全部刷新。

手动动态刷新

动态刷新是当远程配置文件修改后,客户端在不重启的情况下获取的配置能够实时刷新。

1.引入actuator监控

修改pom.xml添加以下依赖:

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

2.暴露监控端口

修改bootstrap.yml添加以下配置:

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

3.添加@RefreshScope注解

要实现动态刷新,我们需要在项目的 ConfigClientController中加上@RefreshScope注解,表示要刷新。

@RefreshScope
@RestController
public class ConfigClientController {
    
    
    @Value("${msg}")
    private String msg;

    @GetMapping("/configInfo")
    public String say(){
    
    
        return msg;
    }
}

4.测试

  • 启动注册中心、配置中心服务端、配置中心客户端
  • 修改配置仓库配置

在这里插入图片描述

  • 手动执行刷新请求

打开windows命令行工具,执行以下请求:

curl -X POST "http://localhost:3355/actuator/refresh"

  • 浏览器访问地址:http://localhost:3355/configInfo,读取到仓库配置即表示成功。

在这里插入图片描述

利用BUS自动刷新

虽然手动刷新也可以实现某个微服务配置的动态刷新,但是其有一个致命的弊端:当微服务有很多的时候,你总不能挨个的去执行手动刷新请求,因此诞生了利用Spring Cloud Bus利用RabbitMQ的发布订阅模式来实现自动刷新。

1.Spring Cloud Bus介绍


Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。Spring Clud Bus目前支持RabbitMQ和Kafka。

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

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

2.搭建环境

2.1 安装erlang

因为rabbitmq服务器是由erlang语言编写的,所以需要先安装erlang环境

直接下载安装即可:http://erlang.org/download/otp_win64_21.3.exe

2.2 安装rabbitmq

直接下载安装:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe

软件安装完毕后,进入RabbitMQ安装目录下的sbin目录打开cmd窗口执行:

rabbitmq-plugins enable rabbitmq_management

在这里插入图片描述

  • 启动

可以到【开始】中找到启动程序点击即可启动:
在这里插入图片描述

启动成功后访问:http://localhost:15672/

在这里插入图片描述

用户名/密码:guest/guest

  • 可能启动失败的原因

我碰到无法启动的情况是因为我本机之前安装过ActiveMQ,这个是有冲突的。先关闭ActiveMQ就好了。

3.新增3366客户端

为了演示多客户端配置自动刷新,仿照3355客户端复制一个3366客户端,过程省略。

4.配置中心服务端

  • 修改pom.xml添加如下 依赖:
<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 添加配置
server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/indexman/config-server.git #GitHub上面的git仓库名字
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
#rabbitmq相关配置
rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'

5.配置中心客户端

  • 添加依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 添加rabbitmq配置
server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: order #配置文件名称
      profile: dev #读取后缀名称
      uri: http://localhost:3344 #配置中心地址
  #rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

6.测试

6.1 动态刷新全局广播

  • 依次启动eureka、配置中心服务端、3355和3366客户端

  • 修改远程配置
    在这里插入图片描述

  • 发送全局通知命令

  • 访问3355和3366客户端,发现配置已更新
    在这里插入图片描述

6.2 动态刷新定点通知

如果你不想通知全部微服务,可以向某个微服务单独发送通知。

公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

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

我们这里以刷新运行在3355端口上的config-client为例:

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

总结

一张图总结Spring Cloud Bus动态刷新机制:
在这里插入图片描述

项目代码

https://gitee.com/indexman/cloudstudy

おすすめ

転載: blog.csdn.net/IndexMan/article/details/119078461