【Spring Cloud】Spring Cloud Config 配置统一管理(二) 配置文件的热更新

说明

在之前的配置同一管理中,我们每次修改配置文件之后,都需要重新启动客户端才会去拉取最新的配置文件,现在,我们需要去实现配置文件的热更新。

配置

第一步:引入依赖


    org.springframework.boot
    spring-boot-starter-actuator

第二步:在配置文件中增加刷新接口 refresh

management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info  

第三步:在需要修改配置文件的控制器中加入注解 @RefreshScope
-> 示例

package top.yuyufeng.demo.action;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuyufeng
 * @date 2018/12/26.
 */
@RestController
@RefreshScope
public class IndexController {
    @Value("${my-name}")
    private String myName;

    @RequestMapping(value = "/")
    public String index() {
        return "name:" + myName;
    }
}

使用

项目启动后,再修改配置文件,push到git仓库
post请求地址 http://127.0.0.1:8080/actuator/refresh 即可

【Spring Cloud】Spring Cloud Config 配置统一管理(二) 配置文件的热更新_第1张图片

你可能感兴趣的:([Spring,Cloud])