Friday, June 30, 2017

java microservices tutorial:Access spring cloud config server

microservices architecture java : Access  Config Server 

In previous microservices  series I have discussed How to configure a config server step by step. Here I will show you how to access those centralized properties file from spring cloud config server.

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

Step 1: Create a project template from while creating 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.

microservice architecture diagram
Step 2 : Import the Template project as Maven project in Eclipse.
Step 3 : Now change the file to and place 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="" xmlns:xsi=""


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

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







Step 5 : Now create file under {user.home}/CentralRepo
It is the filesystem from where configServer reads the properties file.
Please note that as we provide the unique serviceID value as  EmployeeSerachService in property so we will create a properties file name ({serviceID}-{profile{.properties) considering the profile value as default.

  1. cd CentralRepo
  2. touch
  3. vi
  4. Hit insert
  5. Write name=Shamik Mitra
  6. Hit esc and wq!

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

package com.example.EmployeeSerachService.controller;

import org.springframework.beans.factory.annotation.Value;
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 fetch from the 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 EmployeeSerachService. Once EmployeeSerachService 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 EmployeeSerachService  without restarting the application for that we need to hit
http://localhost:8080/refresh endpoint which is the Actuator endpoint , by doing this latest property value will be reflected in  EmployeeSerachService  we will test that in later section.

Step 7: Now run the ConfigServer first then EmployeeSerachServiceApplication as Java Project

package com.example.EmployeeSerachService;

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

public class EmployeeSerachServiceApplication {

    public static void main(String[] args) {, args);

Once both are up and running

Please hit the following url in browser  http://localhost:8080/greeting

Output : Hello Shamik Mitra.

Now try to change the value of name property
  1. cd CentralRepo
  2. touch
  3. vi
  4. Hit insert
  5. Edit name=Samir Mitra
  6. Hit esc and wq!

Now hit the URL again
We see it shows old value
Output : Hello Shamik Mitra.

Now if we hit the actuator endpoint refresh

Output : Hello Samir Mitra.

About URL : To check the properties values for a Microservice in 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 MicroService EmployeeSerachService.
The url should be