Objective

A lot of our microservice configuration now resides in the yaml configuration files within the application. In this episode, we will make this configuration environment-specific (12-factor-app), and externalize this configuration by serving it from a configuration service. We use spring-cloud-config server and client to achieve this.

At the end of this episode, based on the environment in which you run your application (eg: local, QA, cloud), the application will pull its configuration (eg: database connection properties) from a configuration service, which in turn gets all the configuration from a git repository.

Technologies introduced

  1. Spring Cloud Config

Create a repository to hold your configuration

  • Here I have gone for a single repository to hold the configuration of all my services. We could have gone for “one repo per application” policy also.
  • Here is my repository:
https://github.com/microxs-demo/application-configuration.git
  • In this repository, I have configuration yaml files for each of my spring applications.
  • The different environments (eg: development, QA, Prod) are represented as spring profiles in each of these yaml files.

Create a configuration service

  • Next, we create a configuration service that serves the configuration from the above git repository
  • It is a spring boot application with its main class annotated with
    @EnableConfigServer
  • Note that the config-service application depends on
    org.springframework.cloud:spring-cloud-config-server:1.2.2.RELEASE
  • Specify the git repository from which the config-service should serve configuration
    spring.cloud.config.server.git.uri: https://github.com/microxs-demo/application-configuration.git
  • Run your config-service

Verify the configurations served by config-service

  • In your browser, go to:
    http://localhost:8888/catalog-service/development
  • The configuration for catalog-service for the profile – “development” will be served in JSON format

Update your services to use configuration from config-service

We will now update our catalog-service to use configuration from the externalized configuration server – config-service.

  • Add dependency to
    org.springframework.cloud:spring-cloud-starter-config:1.2.2.RELEASE
  • Set the config-service URI for the application to fetch its configuration from. Update the configuration file catalog-service.yml
spring:
  profiles: development
  cloud:
    config:
      uri: http://${MICROXS_COMMERCE_CONFIG_SERVICE_HOST:localhost}:${MICROXS_COMMERCE_CONFIG_SERVICE_PORT:8888}

Run your service choosing the profile (or environment) you want

SPRING_PROFILES_ACTIVE=development gradle bootRun

Your logs should indicate the profile you are running, show where it is pulling configuration from:

Fetching config from server at: http://localhost:8888

Located environment: name=catalog-service, profiles=[development], label=master, version=d7ca69a6470a4e72826e5bd59619ca1c667eb503, state=null

Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='configClient'], MapPropertySource [name='https://github.com/microxs-demo/application-configuration.git/catalog-service.yml#development'], MapPropertySource [name='https://github.com/microxs-demo/application-configuration.git/catalog-service.yml']]]

The following profiles are active: development

You will also notice that your mongodb connection properties are coming from whatever you defined for that profile in the configuration repository.

Source Code

Tag “step5” in repository: application-configuration

git clone https://github.com/microxs-demo/application-configuration 
cd application-configuration 
git checkout tags/step5

Tag “step5” in repository config-service

git clone https://github.com/microxs-demo/config-service 
cd config-service 
git checkout tags/step5

Tag “step5” in repository catalog-service

git clone https://github.com/microxs-demo/catalog-service 
cd catalog-service 
git checkout tags/step5

Video

Detailed explanation in video

Advertisements