SpringCloud Config(配置中心) 配置服务自动刷新

概述

配置中心负责统一保存微服务的配置文件(yml或properties文件),配置文件一般是从git库(或其它后端)中拉取。

微服务启动时,会从配置中心拉取配置文件。如果配置中心有对应的配置文件,则会覆盖服务本地的配置文件中对应的属性。

当git库有新的提交时,通过webhook通知配置中心。webhook会带上修改的内容,配置中心找到此次修改的配置文件,通过Spring Cloud Bus通知对应的服务刷新配置。

架构

springboot:2.3.3
spring cloud: Hoxton.SR8
在这里插入图片描述

Config Server端配置

依赖


<!-- config-server依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
 
<!-- springcloud-bus依赖实现配置自动更新,rabbitmq -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- webhook通知配置中心刷新的接口 -->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-monitor</artifactId>
 </dependency>

配置文件

spring:
  cloud:
    config:
      server:
        git:
          uri: git@192.168.1.33:root/leve-config-repository.git
          search-paths: config
          timeout: 10
          ignore-local-ssh-settings: true
          private-key: |
            -----BEGIN RSA PRIVATE KEY-----
            xx
            -----END RSA PRIVATE KEY-----
  rabbitmq:
    host: 192.168.1.57,192.168.1.58

启动类

启动类增加注解 @EnableConfigServer。

至此Config-Server端已配置完毕。

Config Client端配置

添加依赖

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

配置文件

spring:          			
  cloud:
    config:
      profile: dev                                   #项目配置文件选择
      label: dev                                     #git仓库的分支
      discovery:                                     #通过注册中心发现配置中心
        enabled: true
    bus:
      enabled: true
      id: ${spring.application.name}:${spring.profiles.active}:${random.value}
rabbitmq:    					        #本地环境不需要配置mq
    host: 192.168.1.57,192.168.1.58

记住配置bus.id,否则webhoob发送的事件会匹配不上服务。

代码

可以使用@RefreshScope 或 @ConfigurationProperties注解,获取配置文件中的属性并支持动态刷新。如:

@Component
@ConfigurationProperties("test.auth.client")
@Getter
@Setter
public class AuthClientProp {

    /**
     * 授权id
     */
    private String id;
    /**
     *  授权密钥
     */
    private String secret;

}

@RefreshScope 和 @ConfigurationProperties的区别

Client端已经配置完毕。

配置webhook

在这里插入图片描述
上面是gitlab配置webhook的例子。
当push到dev分支时,gitlab调用配置中心的monitor接口通知更新。

至此,所有配置完成。

补充说明

  1. 如果使用的是蛇型命名,需要为ConfigServicePropertySourceLocator设置自定义Mapper的RestTemplate.

  2. 集成了Spring Cloud Bus后,不能使用stream3.0的写法来接收其它的mq消息,只能使用经典写法,否则接收不到mq消息。(springcloud 2020已解决这个问题)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值