Coding image00

Published on September 16th, 2016 | by Guest


Spring Micro Services in Java

This post is intended by java development experts to make you understand about the concept of Spring Micro Services and how you can use them in your java project. Read further to know more about the subject.

Technology: Micro-service as the name suggests the application can be split into multiple services, each service will register into service registry implementers like Eureka, Consul etc…

Service Producer and Client will connect to Service Registry using Service Name which is provided by Application. Dynamically any no. of services can be added to Registry, client can be connect to service using this Registry means service and client will work as independent components, they don’t depend on each other.

Eg: Online Shopping Service will have various modules like Order, cart, products, billing, etc…

Each module we can develop as a separate entity, and we can provide the fault tolerance for each of the service.


Here Account Service, Product Catalog, Cart Server, Order Server are the individual Services, all are depends on Service Registry (we are using Eureka) for Connecting to other Services.


Web Service look for Account Service Entry in Service Registry using Application Name provided for Service. is property used for defining the Application Name in Service Registry.

Below is the snapshot if we attach services to Service Registry(in our case Eureka Service Registry)


Starting Service Registry

If you have some Java RMI working background, Java RMI maintains RMI Registry for finding various different Process information. Same kind of Registry is need for Micro Services.

Eureka is one of the popular Registry Server, and Spring has developed wrappers around it, Spring Cloud is the framework which will supports Eureka Server and they made some annotations so that Eureka server will start and running easily.

Adding Spring Cloud Dependencies

Spring Cloud Framework comes with starter dependencies and we can specify it as the Parent,


Adding Spring Cloud Dependency:


Spring Cloud supports Eureka and Consul as the Registration servers.

Adding Eureka Server Dependency:


Adding Consul Server Dependency:


We can use Spring Boot framework for starting server.

@EnableEurekaServer is the annotation which will bootstrap the server using default Configurations.

EurekaInstanceConfigBean is the Bean, it is used to customize the Configuration properties for Eureka Eureka by default runs on port 8761, if we want to customize the port number, we can make an entry in either yml).

Eg: server.port: 1111

The Spring Boot Main class will look like this:

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

If we want to customize the properties. By default spring Boot will look for file in classpath.

If we want to load the properties from other than then we need to add either using System.setProperty or we can add this property in Environmental Property.

Running ServiceRegistry in 1111 Port

We need to add property using System class.

public class ServiceRegistry {
    public static void main(String[] args) {
        System.setProperty("", "service-registry");, args);

Create a new and add the below content:


Configuring Discovery Service

The above application may act as either server or client, for configuring it as server and don’t register as client we need to add the below property in file.

eureka.instance.hostname =localhost
eureka.instance.client.registerWithEureka =false
eureka.instance.client.fetchRegistry =false

Attaching Client to Eureka Server

@EnableDiscoveryClient: It is annotation provided by the spring cloud for Contacting with Eureka Registry Server. For discovery to work we need to set property to true. This annotation will set to true by default.

And our client need to know the Eureka Server URL.

Configuring Registry URL in client application:

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ =accounts-service

This name is used to register with Eureka Server Registry.

One more important property needed in client application for dev mode:


Indicates how often (in seconds) the eureka client needs to send heartbeats to eureka server to indicate that it is still alive. If the heartbeats are not received for the period specified in leaseExpirationDurationInSeconds, eureka server will remove the instance from its view, there by disallowing traffic to this instance.

eureka.instance.leaseExpirationDurationInSeconds =30;

Indicates the time in seconds that the eureka server waits since it received the last heartbeat before it can remove this instance from its view and there by disallowing traffic to this instance. Setting this value too long could mean that the traffic could be routed to the instance even though the instance is not alive. Setting this value too small could mean, the instance may be taken out of traffic because of temporary network glitches.This value to be set to atleast higher than the value specified in leaseRenewalIntervalInSeconds.

Eg: suppose if the Eureka Client application is providing the rest services then using we can execute services using http://<service-name>/<relative-path>

If service name account-service and this application is providing /accounts endpoint then the using service registry we can execute the endpoint as


It will execute the account service endpoint and it will provide results.

Client application for Rest Services

We can use the above name and we can write a wrapper for rest service.We can use the Spring provided RestTemplate for Contacting this service, as we know that same service can be deployed in same host and same port more than once, if we use @LoadBalanced annotation for RestTemplate injected instance it will use instance based on load.

You can refer the sample source code application:

Please follow README file for running the application.


Large applications can be split into independent Micro services and each can be contact using Service Registry, Service Registry will also provide fault tolerance and high availability of the applications. We can dynamically register the service and client application, so that no dependency on each other. We can write a common gate way application which will contact the micro services to provide the result for security purposes. Spring Cloud is the framework which provides the easily running the Service Registry application on top of Spring Boot.

Java development company experts have explained the concept and use of Spring Micro Services. If there is anything you have not understand or any point missed by the experts, tell in comments.

Like this post? Share with your friends.
Share on Facebook5Tweet about this on TwitterShare on Google+6Email this to someone

Tags: , , , ,

About the Author


Contribution of guest authors towards Techno FAQ blog

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top ↑
  • Latest posts

  • Advertisement

  • Browse by category

  • Recent comments

  • Advertisement

  • Subscribe to updates

    You can get the latest posts from Techno FAQ delivered to you via Email or RSS.

    Enter your email address:

  • Subscribe to our RSS feed
  • Forum activity

  • Find us on Facebook

  • Latest tweets

  • Advertisement