Refresh client configuration content manually (spring cloud config)

Time:2019-12-2

Refresh client configuration content manually

Add dependency to client project

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

Client project modification profile

Add management. Endpoints. Web. Exposure. Include = refresh, health, info

spring.application.name=spring-cloud-config-client
server.port=9006
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#Settings do not need to be registered in consumer
spring.cloud.consul.discovery.register=false
#Exposed access points shown
management.endpoints.web.exposure.include=refresh,health,info

Client program adds support for refreshing annotation

Add @ refreshscope annotation on the class using configuration center:

@RestController
//Refresh trigger address / Actor / refresh
@RefreshScope
public class ConfigTestController {

    //Configuration information is read by @ value annotation, and configuration items are read by ${configuration items}
    @Value("${bluersw.config}")
    private String configBluersw;

    @RequestMapping("/ConfigTest")
    public String ConfigTest(){
        return this.configBluersw;
    }
}

Test refresh effect

Change the configuration content in Git warehouse to Test-5 (bluersw. Config = Test-5), start the client program (spring cloud config client), refresh the client page 127.0.0.1:9006 / configtest, find out whether the display content is Test-3, and then execute:

curl -X POST http://127.0.0.1:9006/actuat/refresh

Refresh the page 127.0.0.1:9006/configtest again. The content of the page is displayed as Test-5, indicating that the configuration information in the client program has read the latest value.