Coding

Published on September 16th, 2016 | by Guest

0

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.

image00

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.

image02

Web Service look for Account Service Entry in Service Registry using Application Name provided for Service.

spring.application.name 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)

image01

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,

<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>_Brixton_.RELEASE</version>
</parent>

Adding Spring Cloud Dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>

Spring Cloud supports Eureka and Consul as the Registration servers.

Adding Eureka Server Dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

Adding Consul Server Dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
</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 Server.eg: Eureka by default runs on port 8761, if we want to customize the port number, we can make an entry in either application.properties(or yml).

Eg: server.port: 1111

The Spring Boot Main class will look like this:

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistry {
    public static void main(String[] args) {
        SpringApplication.run(ServiceRegistry.class, args);
    }
}

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

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

Running ServiceRegistry in 1111 Port

We need to add spring.config.name property using System class.

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistry {
    public static void main(String[] args) {
        System.setProperty("spring.config.name", "service-registry");
        SpringApplication.run(ServiceRegistry.class, args);
    }
}

Create a new service-registry.properties and add the below content:

server.port=1111

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 service-registry.properties 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 spring.cloud.discovery.enabled 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/
spring.application.name =accounts-service

This name is used to register with Eureka Server Registry.

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

eureka.instance.leaseRenewalIntervalInSeconds=5

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

http://account-service/accounts

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:

https://github.com/paulc4/microservices-demo.git

Please follow README file for running the application.

Conclusion

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 Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn3Share on Reddit0Share on Tumblr0Share on VKEmail this to someone

Tags: , , , ,


About the Author

Guest

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 ↑
  • Software We Love

  • 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

  • Support us

    If you find our content useful, you can support our activities by making a small donation.

    Bitcoin: 18ykfpxGUymmLoyuZ7hNHXg6zoQmEgsrvJ

    Ethereum: 0xcD9A6f858A235f9001DA612F43e12ff29CD1b53D

    PayPal (except India):

    Instamojo ( payments):

  • Advertisement