Spring Cloud Config Client, Part 2
The second part of this two-part tutorial demonstrates how to set up and configure the Spring Cloud config client with dependencies.
Join the DZone community and get the full member experience.Join For Free
This article will talk about the development of the config client. Please go through the previous article before reading ahead. We will create a config client and use properties stored in the Spring config server. Please make sure spring config server is up and running.To create the config client application, use https://start.spring.io/ to generate codebase. Select “config client” and “REST repositories” dependencies to generate a project. Basically, we will create REST service and try to access configuration stored in config server. A sample pom.xml file can be referred to here for dependencies.
You may also enjoy: Introduction to Spring Cloud: Config (Part 1)
Import the generated project into the editor and create the
RestAPIController Java class to define the API.
Add annotation the
@RestController and create the GET API
The GET API will return property value from the Spring config server. Use the
@Valueannotation to read the value.
Set the property specific to the client application in the “application.properties” or “bootstrap.properties” file inside folder "src/main/resources."
- “spring.application.name” of application is used to pick a property file from the config server.
- “spring.cloud.config.uri” is Spring config server URL.
- “spring.profiles.active” tells which environment profile to use.
Config client development is complete and now we can build and run the application.
Command to build:
Command to run:
The client application should be up now. Test the API using URL; this should return value from the "client-config-development.properties" file.You can also use " @RefreshScope" and “spring-boot-starter-actuator” to support manual refreshing config values.
Finally, here is the Git code link.
Spring Cloud Config Server — Programmer Gate
How to Use the Spring Config Server
Opinions expressed by DZone contributors are their own.