microservices
  1. microservices-setting-up-spring-cloud-config-server

Setting up Spring Cloud Config Server

Spring Cloud Config Server is a central place for managing external configuration of microservices. It runs as a standalone server, and microservices can fetch configuration from the server at runtime as per their environment.

Syntax

The syntax for setting up Spring Cloud Config Server is as follows:

  1. Create a new Spring Boot project with spring-cloud-config-server dependency.

  2. Add the following code to the application.properties file:

# The git repository containing configuration files
spring.cloud.config.server.git.uri=<repository_url>

# The branch to checkout for configuration files
spring.cloud.config.server.git.default-label=<branch_name>

# The folder within the repository to use for configuration files
spring.cloud.config.server.git.search-paths=<folder_path>
  1. Use @EnableConfigServer annotation on the SpringBootApplication class of your project.

Example

Consider the following Spring Boot project structure:

- my-config-server
  |- src
  |   |- main
  |       |- java
  |           |- com.example.ConfigServerApplication.java
  |       |- resources
  |           |- application.properties
  |- pom.xml

To set up the Spring Cloud Config Server, follow these steps:

  1. Create a new Spring Boot project with spring-cloud-config-server dependency:
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
</dependencies>
  1. Add the following code to the application.properties file:
spring.cloud.config.server.git.uri=https://github.com/my-org/my-config-repo.git
spring.cloud.config.server.git.default-label=main
spring.cloud.config.server.git.search-paths=config-repo
  1. Use @EnableConfigServer annotation on the SpringBootApplication class of your project:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

Explanation

In the example above, we have created a new Spring Boot project for the config server and added the spring-cloud-config-server dependency to the pom.xml file. Then we have added the configuration details in the application.properties file, which tells the server where to fetch the configuration files from. Finally, we have used @EnableConfigServer annotation on the SpringBootApplication class to enable the Spring Cloud Config Server.

Use

Spring Cloud Config Server is used to manage external configurations of microservices. Once the config server is set up, microservices can fetch configuration from the server at runtime by specifying the server endpoint in their respective application.properties file.

Important Points

  • Spring Cloud Config Server is a central place for managing external configuration of microservices.
  • Config server runs as a standalone server.
  • Microservices can fetch configuration from the server at runtime by specifying the server endpoint in their respective application.properties file.
  • Configuration files can be stored either on a local file system or in a remote Git repository.

Summary

Spring Cloud Config Server is a standalone server for managing external configuration of microservices. It runs as a separate service, and microservices can fetch configuration from the server at runtime. The configuration files can be stored either on a local file system or in a remote Git repository. Once the server is set up, microservices can fetch configurations dynamically as per their environment.

Published on: