Microservices: Access Properties From Spring Cloud Config Server

DZone 's Guide to

Microservices: Access Properties From Spring Cloud Config Server

Learn how to write a microservice that can be used to access centralized properties files on a Spring Cloud config server.

· Microservices Zone ·
Free Resource

In my previous microservice series, I discussed how to configure a config server step by step. Here I will show you how to access those centralized properties files from the Config server.

To start with, we have to write a small microservice which has a config property in the Config server, and use that property in this microservice.

Step 1: Create a project template from https://start.spring.io/. While creating the template, choose the following modules:

  1. Config Client
  2. Actuator
  3. Spring-Web
  4. Jersey 

Then hit the Generate Project button to download the template.

Image title

Step 2: Import the Template project as Maven project in Eclipse.
Step 3: Now change the application.properties file to bootstrap.properties and place the following properties there:


Property Name





A unique name(Service ID) for the service we will create a properties file in configserver based on this ServiceID



It determines in which port this service will be running.



It is the URL of config server please note that if config server has a ServiceID we can change it to http://{serviceID}:{port}  If there are multiple configServers behind a Load balancer we can give the public ip of load balancer


Step 4: The pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <description>Demo project for Spring Boot</description>

        <relativePath/> <!-- lookup parent from repository -->







Step 5: Now create EmployeeSearchService.properties file under {user.home}/CentralRepo.

It is the filesystem from where configServer reads the properties file.

Please note that we provide the unique serviceID value as  EmployeeSearchService in spring.application.name property so we will create a properties file name EmployeeSearchService.properties ({serviceID}-{profile{.properties) considering the profile value as default.

cd CentralRepo
touch EmployeeSearchService.properties
vi EmployeeSearchService.properties
Hit insert
Write name=Shamik Mitra
Hit esc and wq!

Step 6: Now create a package called com.example.EmployeeSearchService.controller and create a RestController called GreetController .

package com.example.EmployeeSearchService.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

* @author Shamik Mitra
public class GreetController {

    private String personName;

    public String greet(){

        return "hello " + personName;


Please notice the annotation @Value("${name}"). This name property value actually fetches from the EmployeeSearchService.properties which we had created earlier. EmployeeSerachService find the properties file from the central server and fetch that name property and cached in local memory so the Central server has to be up before the EmployeeSearchService. Once EmployeeSearchService is up and running it can consult local cache so in the meantime if ConfigServer goes down that will not create such problem.

I use the @RefreshScope annotation so that if any property value changes in configServer that can be propagated to EmployeeSearchService without restarting the application for that we need to hit the
http://localhost:8080/refresh endpoint which is the Actuator endpoint; by doing this latest property value will be reflected in EmployeeSearchService we will test that in a later section.

Step 7: Now run the ConfigServer first, then EmployeeSearchServiceApplication as a Java Project.

package com.example.EmployeeSearchService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class EmployeeSearchServiceApplication {

public static void main(String[] args) {
SpringApplication.run(EmployeeSearchServiceApplication.class, args);

Once both are up and running, please hit the following URL in the browser: http://localhost:8080/greeting.

Output : Hello Shamik Mitra.

Now try to change the value of the name property:

cd CentralRepo
touch EmployeeSearchService.properties
vi EmployeeSearchService.properties
Hit insert
Edit name=Samir Mitra
Hit esc and wq!

Now hit the URL again (http://localhost:8080/greeting). We see it shows the old value.

Output : Hello Shamik Mitra.

Now we hit the actuator endpoint refresh (http://localhost:8080/refresh) and hit http://localhost:8080/greeting.

Output : Hello Samir Mitra.

About URL: To check the properties values for a microservice in the Central Config server, we have to call the REST endpoint of the Config server.We can build the config server endpoint URL in this manner: http://{Config server URL}:{Port}/{ServiceID}/{Profile} So if we want to see all the properties value in config server for the MicroServiceEmployeeSearchService, the URL should be http://localhost:9090/EmployeeSearchService/default.

microservice ,config server ,spring cloud ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}