Spring Cloud 如何动态刷新 Git 仓库配置?

有时候在配置中心有些参数是需要修改的,这时候如何不重启而达到实时生效的效果呢?

本文基于以下讲解:

  • Spring Cloud Greenwich.SR3
  • Spring Boot 2.1.7.RELEASE
  • 基于 Git 的配置中心仓库
添加 actuator 依赖

在引用配置中心的项目中添加以下 actuator 依赖:

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

spring-boot-starter-actuator:这个模块的 /actuator/refresh (POST请求)端点可以刷新配置,更多的使用参考 Spring Boot 系列文章。

暴露 refresh 端点

actuator 默认是不暴露 refresh 端点的,需要我们主动暴露,在引用配置中心的项目中添加以下配置:

management:
  endpoints:
    web:
      exposure:
        include: refresh,info,health
添加刷新范围

引用了配置中心的项目,在需要刷新的 Bean 上添加 @RefreshScope 注解。

示例1:

@RefreshScope
@RestController
public class TestController {

	@Value("${username}")
	private String username;
	
...

}

示例2:

@RefreshScope
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {

...

}

当配置更改时,标有 @RefreshScopeBean 将得到特殊处理来生效配置,不然改了配置不会刷新的。@RefreshScope 的原理可以参考这篇文章:Spring Cloud @RefreshScope 原理是什么?,很详细。

手动刷新配置

修改配置后,我们可以通过 post 到 /actuator/refresh 即可手动刷新配置。

如下图所示:

如果参数有变更,刷新成功的话,会返回一个含有参数名的变更数组。

自动刷新配置

如果你使用了 Gitlab 或者 Github 仓库,可以配置 Webhooks 来做到自动更新,当参数变更时,能做到自动通知。

Gitlab配置如下图所示:

Github也差不多的,可以配置一个 URL(用于变更通知)和一个 Secret Token(用于请求验证)。

但这种方式仅限于单台,如果有多台或者多个系统,该如何通知,恐怕需要加一个公共接口来绕一圈了。

如果需要请求头认证的,可以使用这种方式:

https://user:password@ip:port/xxxx

扩展问题

如果使用配置中心项目少的情况,我们是可以通过上面的方式进行配置动态刷新,如果项目比较复杂的情况呢?

上面的方式肯定都是行不通的,Spring Cloud Bus 消息总线可以解决配置修改的真正的动态刷新,请看下回分解。关注微信公众号:Java技术栈,第一时间推送。在公众号后台回复:cloud,还能获取栈长整理的 Spring Cloud 系列教程,都是实战干货。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
配置动态刷新_SpringCloud配置中心动态加载(刷新)配置文件可以通过Spring Cloud Config实现。具体实现步骤如下: 1. 在pom.xml文件中引入Spring Cloud Config依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ``` 2. 在启动类中添加@EnableConfigServer注解,开启配置中心服务: ``` @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 3. 在配置文件中添加配置中心相关配置: ``` spring: cloud: config: server: git: uri: https://github.com/{git_username}/{git_repository}.git search-paths: '{config_files_path}' username: {git_username} password: {git_password} ``` 其中,uri为Git仓库地址,search-paths为配置文件路径,username和password为Git仓库的用户名和密码。 4. 在需要动态加载配置的应用程序中,添加@RefreshScope注解,表示该类中的配置可以被动态刷新: ``` @RestController @RefreshScope public class ConfigController { @Value("${config_name}") private String configValue; @GetMapping("/config") public String getConfig() { return configValue; } } ``` 5. 在需要动态刷新配置的时候,向应用程序发送POST请求,请求路径为/actuator/refresh: ``` curl -X POST http://localhost:8080/actuator/refresh ``` 以上就是配置动态刷新_SpringCloud配置中心动态加载(刷新)配置文件的实现步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Java技术栈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值