Standup Config Server
Click here to expand...
Dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>Application code
@EnableConfigServer @SpringBootApplication public class ConfigurationServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigurationServiceApplication.class, args); } }Config Repository Setup (Filesystem)
- On the file system, create a new directory and run
git initin it- Create a new file
a-bootiful-client.propertiesand add the followingmessage = This is from config serverTell Config Server Where the Config Repository Is
server.port=8888 spring.cloud.config.server.git.uri=${HOME}/Desktop/config
Standup Config Client
Click here to expand...
Dependencies
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>App Code
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; @RefreshScope @RestController class DefaultController { @Value("${message:Hello default}") private String message; @RequestMapping("/message") String getMessage() { return this.message; } }App Properties
spring.application.name=a-bootiful-client spring.config.import=optional:configserver:http://localhost:8888/ # to enable the /refresh endpoint, to demonstrate dynamic configuration changes management.endpoints.web.exposure.include=*
Test Dynamic Config Changes
Click here to expand...
- check message: http://localhost:8080/message
- change message property in
a-bootiful-client.propertiesand commit- verify config-server picked up changes http://localhost:8888/a-bootiful-client/default
- refresh config-client via hitting POST endpoint:
curl localhost:8080/actuator/refresh -d {}-H “Content-Type: application/json”- check updated message: http://localhost:8080/message