Spring Cloud Config Client
The Spring Cloud Config Client is a component of the Spring Cloud framework that enables applications to access configuration properties stored in a centralized configuration server. It allows for the externalization of configurations from the application code, promoting a more maintainable and flexible architecture.
Spring Cloud Config Server is where the configurations for multiple applications are stored.
During startup, the Config Client contacts the Config Server to obtain the application-specific configuration properties. The client application uses the application name, profile and label information to fetch the relevant configuration.
To set up a Spring Cloud Config Client:
i) Add below dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
ii) Configure the client: In application.properties or application.yml, specify the Config Server’s location, application name, profile and label.
spring.cloud.config.profile=jdbc
spring.cloud.config.label=master
spring.cloud.config.name=spring-config-server-demo
spring.config.import=optional:configserver:http://localhost:8080
iii) Access the configured properties in app as usual
package com.example.springconfigclientdemo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpringConfigClientController {
@Value("${greeting}")
String greeting;
@GetMapping("/greet")
public String greet() {
return greeting;
}
}
For complete source code: https://github.com/MMahendravarman/Springboot_Examples
Leave a Reply