在config的client端需要更新变量的对应的controller加入注解
@RefreshScope
package com.lyq.controller; 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; /** * Created by 云强 on 2017/11/21. */ @RestController // 在执行refresh时会刷新bean中变量值 @RefreshScope public class ConfigTestController { @Value("${foo}") String foo; @RequestMapping(value = "/config/hi") public String hi(){ return foo; } }
在配置文件里加入配置
management.security.enabled=false
spring.application.name=service-demo #关闭thymeleaf的缓存,不然在开发过程中修改页面不会立刻生效需要重启,生产可配置为true。 spring.thymeleaf.cache=false #注册eureka服务 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ server.port=8081 #config客户端配置 spring.cloud.config.label=master spring.cloud.config.profile=dev #spring.cloud.config.uri=http://localhost:8888/ spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server #springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置,以post请求的方式来访问http://localhost:8081/refresh 就会更新修改后的配置文件 management.security.enabled=false
配置成功,启动程序,通过postman调用refresh来刷新配置
http://localhost:8081/refresh