Spring Cloud Bus 解决配置文件实时更新与git仓库同步

Spring Cloud Bus

实现不重启服务的情况下,及时更新配置文件。

在上一篇博文之后,测试,会发现,当我们修改配置文件的属性值时,项目中并不能及时更新

如果想在不重启微服务的情况下更新配置该如何实现呢? 可以使用Spring Cloud Bus来实现配置的自动更新

需要注意的是Spring Cloud Bus底层是基于RabbitMQ实现的,默认使用本地的消息队列服务,所以需要提前

启动本地RabbitMQ服务(安装RabbitMQ以后才有)

Spring Cloud Bus作用:将git仓库的配置文件更新,在不重启系统的情况下实现及时同步到各个微服务。

一丶简介

​ Spring Cloud Bus是用轻量的消息代理将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。也就是消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。 Spring Cloud Bus可选的消息代理有RabbitMQ和Kafka。

​ 使用bus之后,流程如下:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Cy1wfyI-1586227766252)(C:\Users\86132\AppData\Roaming\Typora\typora-user-images\image-20200405175643136.png)]

二丶Spring Cloud Bus应用

启动RabbitMQ通过修改码云中的配置文件后发送Post请求实现及时更新用户微服务中的配置项

需求:在码云的git仓库中修改user-dev.yml配置文件,实现不重启user-service的情况下可以及时更新配置文件。

####2.1启动RabbitMQ;

2.2修改配置中心config-server
  • 添加依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

  • 修改配置中心config-server(添加依赖);

    server:
      port: 12000
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/goheima/heima-config.git
      # 配置rabbitmq信息;如果是都与默认值一致则不需要配置
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:10086/eureka
    management:
      endpoints:
        web:
          exposure:
            # 暴露触发消息总线的地址
            include: bus-refresh
    
2.3修改服务提供工程user-service
  • 添加依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-bus</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  • bootstrap.xml中添加tabbitmq配置:

     # 配置rabbitmq信息;如果是都与默认值一致则不需要配置其余配置和上篇博文一致
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    
  • controller中添加注解:@RefreshScope 刷新配置

2.4测试

在Git仓库中修改配置文件user-dev.yml的test.name内容,第四步:使用Postman或者RESTClient工具发送POST方式请求访问地址http://127.0.0.1:12000/actuator/bus-refresh

说明

1、Postman或者RESTClient是一个可以模拟浏览器发送各种请求(POST、GET、PUT、DELETE等)的工具

2、请求地址http://127.0.0.1:12000/actuator/bus-refresh中 /actuator是固定的,/bus-refresh对应的是配置中心confifig-server中的application.yml文件的配置项include的内容

3、请求http://127.0.0.1:12000/actuator/bus-refresh地址的作用是访问配置中心的消息总线服务,消息总线

服务接收到请求后会向消息队列中发送消息,各个微服务会监听消息队列。当微服务接收到队列中的消息后,

http://127.0.0.1:12000/actuator/bus-refresh地址的作用是访问配置中心的消息总线服务,消息总线

服务接收到请求后会向消息队列中发送消息,各个微服务会监听消息队列。当微服务接收到队列中的消息后,

会重新从配置中心获取最新的配置信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值