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 https://start.spring.io/ 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 application.properties file to bootstrap.properties and place following properties there.

spring.application.name=EmployeeSerachService
server.port=8080
spring.cloud.config.uri=http://localhost:9090



Property Name
Value
Description
spring.application.name
EmployeeSerachService
A unique name(Service ID) for the service we will create a properties file in configserver based on this ServiceID

server.port
8080
It determines in which port this service will be running.
spring.cloud.config.uri
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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>EmployeeSerachService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>



Step 5 : Now create EmployeeSerachService.properties 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 spring.application.name property so we will create a properties file name
EmployeeSerachService.properties ({serviceID}-{profile{.properties) considering the profile value as default.

  1. cd CentralRepo
  2. touch EmployeeSerachService.properties
  3. vi EmployeeSerachService.properties
  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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Shamik Mitra
*
*/
@RefreshScope
@RestController
public class GreetController {
   
    @Value("${name}")
    private String personName;
   
    @RequestMapping("/greeting")
    public String greet(){
       
        return "hello " + personName;
    }

}



Please notice the annotation @Value("${name}") this name property value actually fetch from the EmployeeSerachService.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 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;

@SpringBootApplication
public class EmployeeSerachServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeSerachServiceApplication.class, 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 EmployeeSerachService.properties
  3. vi EmployeeSerachService.properties
  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
http://localhost:9090/EmployeeSerachService/default.