版权声明:本文为博主原创文章,转载请注明出处。作者:杨雄进 https://blog.csdn.net/makyan/article/details/88792556
9.9.手动刷新Config配置
当云端(我们使用gitee)我们的配置文件内容发生变化时,我们项目的客户端却没有发生变化,这怎么办呢?
1、创建一个项目:从futurecloud-config-client-eureka复制一份,得到futurecloud-config-client-eureka-refresh项目
引入依赖:
<!--引入安全认证依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
后面我们使用post请求时,需要登录eureka
2、修改controller 类,增加注解@RefreshScope //开启更新功能
package com.futurecloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 当有请求/fresh节点的时候,会重新请求一次ConfigServer去拉取最新的配置文件
* 请求/fresh需要有几点要求:
* 1.加actuator的依赖
* 2.SpringCloud1.5以上需要设置 management.security.enabled=false
* 这个Controller的作用是查看from这个key的值
* @RefreshScope是为了可以动态刷新这个Controller的Bean
*/
@RestController
@RefreshScope //开启更新功能
public class ConfigClientEurekaRefreshController {
/**
* from 这个属性值,是配置在gitee上的配置文件中的属性
*/
@Value("${from}")
private String fromValue;
/**
* 返回配置文件中的值
* @return
*/
@GetMapping("/return/from")
@ResponseBody
public String returnFromValue(){
return fromValue;
}
}
3 测试
(1) 依次启动项目
futurecloud-service
futurecloud-config-server-eureka
futurecloud-config-client-eureka-refresh
(2) 访问http://localhost:7005/return/from,获得结果
dev-environment76567
(3) 修改Git仓库中futurecloud-config的文件内容为:
dev-environment0000000000
(4) 重新访问http://localhost:7005/return/from,获得结果依然是:
dev-environment76567
(5) 发送post请求到http://localhost:7005/actuator/refresh,结果返回
(6) 再次访问http://localhost:7005/return/from,返回结果为:
dev-environment0000000000
说明配置已经刷新