Implementing a configuration client

Configuration clients are regular Spring Boot applications. All you need to do is provide the server configuration URI to read the centralized configuration, as follows:

spring:
application:
name: configuration-demo
cloud:
config:
uri: http://localhost:9000

The following code snippet shows a REST endpoint reading a centralized configuration and serving the read value as its own response:

@RestController
@RefreshScope
public class ConfigurationDemoController {

@Value("${configuration.dynamicValue}")
private String dynamicValue;

@GetMapping(path = "/dynamic-value")
public ResponseEntity<String> readDynamicValue() {
return new ResponseEntity<>(this.dynamicValue, HttpStatus.OK);
}
}

The following screenshot shows the configuration file stored in the Git repository:

Configuration file stored in a Git repository

Once you execute a request against the preceding endpoint, it will produce the following output:

$ curl http://localhost:8080/dynamic-value 
Old Dynamic Value

Change the value of the configuration variable in the file stored in Git, as shown in the following screenshot:

The configuration file with the change applied

If you hit the endpoint, you will retrieve the same output as before. In order to reload the configuration, you will need to reload the configuration variables by hitting the /refresh endpoint by using a POST request, as shown in the following code:

$ curl -X POST http://localhost:8080/actuator/refresh
["config.client.version","configuration.dynamicValue"]

After reloading the configuration, the endpoint will serve a response using the new provided value, as you can see in the following output:

$ curl http://localhost:8080/dynamic-value
New Dynamic Value
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.252