Spring Cloud Config

eidher

eidher

Posted on February 15, 2023

Spring Cloud Config

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.

Source: https://docs.spring.io/spring-cloud-config/docs/current/reference/html/

Spring Cloud Config Server

Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the @EnableConfigServer annotation. Consequently, the following application is a config server:

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}
Enter fullscreen mode Exit fullscreen mode

Source: https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_spring_cloud_config_server

The config server application needs this dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

The default implementation of the server storage backend uses git, but you can also use SVN, file system, CredHub, AWS (Secrets Manager, Parameter Store, or S3), JDBC, Redis, or Vault. For git, you must create a repository containing YAML and properties files in the master branch. Then, define it in the application.properties file of the Config Server application:

spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///C:/Users/home/Desktop/yourProject/git-repo
Enter fullscreen mode Exit fullscreen mode

If you added a config-client.properties file, for instance, in the git repository, you can run the config server application and access http://localhost:8888/config-client/default to view the properties (default is the profile).

Spring Cloud Config Client

Spring Boot applications can use the Spring Config Server. The config client applications need this dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Then, add in the application.properties file of the Config client applications this property:

spring.config.import=optional:configserver:http://localhost:8888
Enter fullscreen mode Exit fullscreen mode

Additionaly, if you are using different profiles you must define the profile and it will use the corresponding properties file. Add this property: spring.cloud.config.profile=profile (where profile is dev, qa, etc)

Finally, you can use the @ConfigurationProperties or @Value annotations to access the properties from the config server.

For more information see: https://www.baeldung.com/spring-cloud-configuration

💖 💪 🙅 🚩
eidher
eidher

Posted on February 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related