Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microservices - Central Config Server Layer

DZone's Guide to

Microservices - Central Config Server Layer

Learn how to set up the central config server layer in a Spring Boot microservice, for accessing and updating properties files in a microservice architecture.

· Microservices Zone ·
Free Resource

Containerized Microservices require new monitoring. See why a new APM approach is needed to even see containerized applications.

Mainly, in microservices, all the modules are deployed at various locations on different servers. Each module has its properties file in which various properties like database configurations, environment-specific URLs, and application-specific properties are stored. So if we have these properties files stored along with each module separately at the same location of the module, it will lead to different issues. If we need to update just a username for a database or change some part of the URL in the property file, we need to first locate where the module is stored, then go to that server, change the property for that module, and rebuild the whole module. In microservices, there will be a huge number of modules, and managing properties for each module with the traditional approach will be very difficult. So, in microservices, a new layer has been introduced to manage all things related to properties files, known as the Central Config Server.

Overview

The central config server is mainly responsible for providing properties files to each microservice connected to the discovery server. At the time of starting, it will get the properties files from the path specified in the application.yml file and store it inside the memory; this path can be a physical path on the system or a Git repository. Whenever a microservice starts, it will ask the central config server for its property file, and at that time, the central config server will provide the related property file, which was in memory, to the microservice. Each microservice has the configuration added to the central config server in its bootstrap.yml, so whenever it is starting, it first talks to the central config server via discovery and gets the property file. Whenever a change to the property file is required, we just need to go to the path mentioned in the central config server, then update the property file and restart the central config server and the core module whose property file is changed. You will not even require any kind of build for the core module to get the updated properties.

Implementation

First, you need to create a simple Spring Boot app with the following configurations added in the main file. You just need to add the annotation @EnableConfigServer, which will tell that it is playing the role of config server.

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class CentralConfigServerApplication {
 public static void main(String[] args) {
  SpringApplication.run(CentralConfigServerApplication.class, args);
 }
}

Then, in the application.yml file, you needs to specify the path for the properties files. It can be a system path or a Git repository. Following are the two ways to add the path:

spring:
    profiles:
        active: native
    cloud:
        config:
            server:
                native:
                    searchLocations: file:./properties,classpath:config/


-----------------------------------------------------------------

spring:
    profiles:
        active: native
    cloud:
        config:
            server:
      git:
                uri: <git repo url>   

Then, to integrate the central config server in each microservice module, you need to add the following configurations in the bootstrap.yml so that while starting the application, it will connect to the central config server via discovery and get the property file by its application.

spring:
  application:
    name: samplemodule
  cloud:
    config:
      enabled: true
      discovery:
        enabled: true
        serviceId: central-config-server

The most important thing to remember is that you need to name the property file using the respective module's application, e.g. samplemodule.properties, and put in the specified location.

References:

https://spring.io/guides/gs/centralized-configuration/

https://cloud.spring.io/spring-cloud-config/

Automatically manage containers and microservices with better control and performance using Instana APM. Try it for yourself today.

Topics:
microservices ,tutorial ,spring boot

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}