springcloud-configclient手动刷新
1.依赖
<!--引入consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--引入健康检查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.配置文件
application.properties
server.port=9991 spring.application.name=configclient spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.service-name=${spring.application.name} management.endpoints.web.exposure.include=* name=${name}
bootstrap.properties
spring.cloud.config.name=sem spring.cloud.config.profile=pro spring.cloud.config.uri=http://127.0.0.1:9999/ spring.cloud.config.label=
3.启动注解
@SpringBootApplication public class ConfigclientApplication { public static void main(String[] args) { SpringApplication.run(ConfigclientApplication.class, args); } }
4.controller
@RestController @RefreshScope public class TestController { @Value("${name}") private String name; @RequestMapping("/test") public String hello() { return name; } }
5.刷新
SVN配置文件修改提交后,configclient需要post发送刷新请求 URL:http://127.0.0.1:9991/actuator/refresh,再次访问带有@RefreshScope注解的controller配置文件修改后的值才能被重新加载。