{{announcement.body}}
{{announcement.title}}

Spring Cloud Config Client, Part 2

DZone 's Guide to

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.

· Cloud Zone ·
Free Resource

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  getMyrole() :

Java




x
24


 
1
@org.springframework.web.bind.annotation.RestController
2
public class RestAPIController {
3
 
          
4
    @Value("${role.name:Config server issue}")
5
    private String roleName;
6
 
          
7
 
          
8
    @GetMapping("/check-role-name")
9
    public String getMyRole() {
10
        return this.roleName;
11
    }
12
 
          
13
}
14
 
          



The GET API will return property value from the Spring config server. Use the   @Value annotation to read the value. 
Java




x


 
1
@Value("${role.name:Config server issue}")
2
 private String roleName;



Set the property specific to the client application in the “application.properties” or “bootstrap.properties” file inside folder "src/main/resources."
Java




xxxxxxxxxx
1


 
1
spring.application.name=client-config
2
 spring.profiles.active=development
3
spring.cloud.config.uri=http://localhost:8888


  • “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.
Please refer to the file at the following git location.

Config client development is complete and now we can build and run the application.  

Command to build: 

Java






Command to run:

Java






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.

Further Reading

Spring Cloud Config Server — Programmer Gate

How to Use the Spring Config Server

Topics:
spring config server ,spring ,java ,cloud ,spring config client ,spring cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}