Creating the configuration consumer Spring Cloud Config client

Let's create a Spring Boot application that connects with the Spring Config Server to take advantage of reading external property sources from the central configuration server. So, for the client project, we have to add the following Maven configuration for the spring-cloud-starter-config and spring-boot-starter-web modules:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Next, let's create a client class that is a simple REST controller with one GET method mapping:

package com.dineshonjava.cloudconfigclient; 
 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RestController; 
 
@RestController 
public class ConfigClientController { 
    
   @Value("${user.role}") 
    private String role; 
    
   @GetMapping("/profile/{name}") 
   public String getActiveProfile(@PathVariable String name){ 
         return "Hello "+name+"! active profile name is "+role; 
   } 
} 

As you can see, the REST controller class has one request handler method and one role property. This property is annotated with the @Value annotation to populate the value of ${user.role}. It will be fetched from our Cloud Config Server hosted at http://localhost:8888/. But it must be placed in a resource file named bootstrap.properties, because this file will be loaded very early while the application starts. Let's see the configuration of the bootstrap.properties file:

spring.application.name=config-client 
spring.profiles.active=dev 
spring.cloud.config.uri=http://localhost:8888 

As you can see, we have set the application name, and also put the active profile and the connection details for the Spring Cloud server application.

Suppose your Spring Cloud Config Server application is configured with security. Then you also have to give username and password to access the Config Server application. Let's see the bootstrap.properties file with the security configuration:

spring.application.name=config-client 
spring.profiles.active=dev 
spring.cloud.config.uri=http://localhost:8888 
spring.cloud.config.username=root 
spring.cloud.config.password=s3cr3t 

Now, I have added the security configuration to access the Config Server. Let's run the client application and see the output of the REST service:

package com.dineshonjava.cloudconfigclient; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
public class CloudConfigClientApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(CloudConfigClientApplication.class, args); 
   } 
} 

Let's call the http://localhost:8080/profile/Dinesh REST service:

As you can see in the preceding screenshot, the role of the user is fetched from the Spring Cloud Config Server.

..................Content has been hidden....................

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