springcloud系列29——Spring Cloud Config配置属性刷新之手动刷新

前言

在以往的应用中,如果要更改应用的配置,要让配置生效必须重启应用程序。在Spring Cloud Config中我们可以刷新配置属性而不用重启应用。

这1节说明在Spring Cloud Config如何手动刷新配置属性。

手动刷新配置属性

1.需要引入spring boot-actuator依赖(用到/refresh端点)

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

2.在Bean上增加@RefreshScope注解

比如:

@Controller
@RequestMapping("/user")
@RefreshScope
public class UserController {
    @Value("${my.custom.message}")
    private String customeMessage;
    
    @GetMapping("/customMsg")
    @ResponseBody
    public String getCustomeMessage() {
        return this.customeMessage;
    }
}

Ok,准备工作完毕!

先来看看my.custom.message原来的配置

现在我们修改git仓库中配置文件的my.custom.message的配置项。

我们将value修改为:

my.custom.message=Hello,This message is changed!!!

现在通过/refresh刷新配置属性。

返回结果表示my.custom.message配置有更新。

现在在访问/user/customeMsg

可以看到,已经使用了刚刚更新的配置。

注意

1.默认spring-boot-acturator开启了安全认证,这里作为演示将其关闭(management.security.enabled=false)。

2.如果有很多个微服务,使用手动刷新配置的方式就不太合适了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值