Spring Boot Config Client Example
This tutorial shows how to create a Spring Boot Config Client that will its read application configuration from a Spring Boot Config Server that externally keeps the application configuration wtih local & dev profiles.
To configure a Config Server locally, view this page
https://www.techblogss.com/microservices/spring-cloud-config-serverStep 1) Create pom.xml for config client project
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2.1.6.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>central-repo</id> <name>Central Repository</name> <url>https://repo1.maven.org/maven2</url> </repository> </repositories>
Step 2) Create ConfigClient class that will launch config client, AppConfiguration class that holds configuration & MessageRestController
package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.cloud.context.config.annotation.RefreshScope; @SpringBootApplication public class ConfigClient { public static void main(String[] args) { SpringApplication.run(ConfigClient.class, args); } }
package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; @RefreshScope public class AppConfiguration { @Value("${message:Hello default}") private String message; String getMessage() { return this.message; } }
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MessageRestController { @Autowired private AppConfiguration configuration; @RequestMapping("/message") String getMessage() { return configuration.getMessage(); } }
Step 3) Create application.properties, bootstrap.yml, bootstrap-local.yml, bootstrap-dev.yml files and keep in src/main/resources folder
application.properties
management.endpoints.web.exposure.include=*
bootstrap.yml (to load the configuration from config server.)
spring: application: name: config-server
bootstrap-local.yml
spring: cloud: config: uri: http://localhost:8888 logging: file: C:/Users/bisht/configuration-server.log level: ROOT: 'INFO'
bootstrap-dev.yml
spring: cloud: config: uri: http://localhost:8888 logging: file: C:/Users/bisht/configuration-server.log level: ROOT: 'INFO'
Step 4) Launch ConfigClient application using dev/local profile
To Run this Spring Boot application using dev profile, use following command →
java -Dspring.profiles.active=dev -jar demo-0.0.1-SNAPSHOT.jar
To Run this Spring Boot application using local profile, use following command →
java -Dspring.profiles.active=local -jar demo-0.0.1-SNAPSHOT.jar
Step 5) Validating Config Client
Open any browser and launch http://localhost:8080/message. You will see below details displayed for dev & local profiles in the broswer.
Step 6) Updating and refreshing configuration.
Now update application-local.properties in config server and run below command to refresh the properties in client config. Observe the property that was updated without restarting config-server or config-client application.
C:\Users\bisht>curl -X POST http://localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json" ["config.client.version","message"]
References :
Spring Cloud Config