• Spring Cloud教程 第十二弹 Spring Cloud Config整合Spring Cloud Bus实现配置动态刷新


    Spring Cloud Config整合Spring Cloud Bus

    1、回顾

    在上一弹:Spring Cloud教程 第十一弹 Spring Cloud Config连接git和数据库 中介绍了spring cloud config的基本使用,但是配置无法动态刷新,也就是说如果我更改了git或数据库中的配置,项目必须重新启动才能使新配置生效。

    注意:

    • 更新配置后,调用config server的HTTP接口是可以获取到新配置的,这是因为config server会实时请求git或数据库,配置当然是最新的,但是config client是感知不到的

    下面介绍两种不重启项目便可以刷新配置的方式:

    1. 手动刷新
    2. 整合Spring Cloud Bus实现动态刷新

    2、手动刷新

    步骤如下。

    1. config client项目的pom.xml中引入spring-boot-starter-actuator依赖,如下所示:
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    1. config client配置端点,如下所示:
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 在需要动态刷新的Bean上打@RefreshScope,如下所示:
    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 DynamicRefreshController {
        @Value("${stuName:\"\"}")
        private String stuName;
    
        @GetMapping("/config")
        public void init(){
            System.out.println("stuName="+stuName);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 当修改git、数据库配置时,POST请求config client路径/actuator/refresh,即可手动刷新

    3、Spring Cloud Bus动态刷新配置

    Spring Cloud Bus 实现配置的动态刷新步骤:

    1. 当git中的配置发生改变后,向config server发送一个 POST请求,请求路径为/actuator/bus-refresh
    2. config server接收到请求后,会将该请求转发给服务总线Spring Cloud Bus
    3. Spring Cloud Bus接到消息后,生成Topic然后会通知给所有config client
    4. config client接收到通知,请求config server拉取最新配置

    Spring Cloud Bus需要引入RabbitMQ或Kafka作为消息传输的媒介。

    与手动刷新不同,动态刷新的改造主要在config server项目中。

    步骤如下:

    1. config server项目中的pom.xml引入依赖,如下所示:
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-bus-amqpartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. config server配置端点,如下所示:
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 安装并运行RabbitMQ
      参考文章:RabbitMQ入门教程
    2. config server配置RabbitMQ
    spring:
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5.config client引入依赖:

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-bus-amqpartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    1. config client配置RabbitMQ
    spring:
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    bus-refresh端点原理:

    • /actuator/bus-refresh端点清除RefreshScope缓存并且重新绑定@ConfigurationProperties
  • 相关阅读:
    Linux基础指令(一)
    深度学习图像分类算法研究与实现 - 卷积神经网络图像分类 计算机竞赛
    可观测性-Event-埋点数据模型
    String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的?
    stable diffusion 模型和lora融合
    智能运维|AIRIOT智慧光伏管理解决方案
    人工智能安全-2-非平衡数据处理(2)
    ubuntu源码安装aria2
    springboot actuator:开放全部(部分)端点、端点映射、端点保护
    Unity中使用WebP类型文件
  • 原文地址:https://blog.csdn.net/xl_1803/article/details/128140353