microservices architecture java: Eureka Server
In the previous microservices articles, we learn how to configure and access Config Server this tutorial we learn what is Spring Cloud Eureka server ?
Why microservices service registry is needed?
microservices communication : Eureka server/client communication?
How to configure Eureka server using Spring Cloud?
What is Eureka Server?
Eureka server is nothing but a service discovery pattern implementation, where every Microservice is registered and client Microservice looks up Eureka server to get a dependent Microservice to get the job done.
Eureka Server is a Netflix OSS product and Spring cloud offers the declarative way to register and invoke service by Java annotation.
Why Eureka server/service registry is needed in microservices?
To understand why Eureka server needed in microservices architecture, lets start from simple
Understanding How one service calls another service REST endpoint for communication.
Say we need to call Employee payroll service to get payroll information for an Employee. The payroll service which is deployed on localhost 8080 port and by passing employee primary key we got that information so we just call following
Where localhost/127.0.0.1 is hostname/IP address and payroll is payroll service context, 1 is the Employee primary key.
But this is only possible when beforehand you know the hostname/IP addresses then you can configure your URL. So here Hostname or IP address is a constraint or a pain point.
If IP address of a server/container is fixed then you can use that approach but
what happens when your IP addresses and hostname are unpredictable?
Nowadays on a Cloud platform, it is obvious that all the servers or containers use dynamic IPs for autoscaling. And the interesting thing is that in Microservice architecture the key principle is your service can autoscaled as per load so Cloud platforms are ideal for Microservices.
What I try to say here is we can not predict the IP addresses of container/server beforehand so putting dependent services IP addresses in the config file is not a solution we need a more sophisticated technique to identify service, and Eureka server stepped in here.
Eureka server/client communication:
Every Microservice register itself in Eureka server when bootstrapped. Generally using the {ServiceId} it register into Eureka server or it can use hostname or any public IP(If those are Fixed). After registering, every 30 seconds it pings Eureka server to notify that the service itself is available. If Eureka server not getting any ping from a service for a quite long time this service is unregistered from Eureka server automatically and Eureka Server notifies the new state of the registry to all other services. I will write this mechanism elaborately in next Article.
Now one question may pop up our mind is then What is Eureka server itself?
Eureka server is nothing but an another Microservice which treats itself as a Eureka client.
What I mean by that is Eureka server has to be highly available as every service communicate it to discover other services. So it is recommended that it should not be a single point of failure.To overcome it we need multiple Eureka server instance runs behind a load balancer. Now when there are multiple Eureka servers so each server needs to have synchronized registry details so that every server knows about the current state of every Microservices register in Eureka server registry.
Eureka server communicates its peer server as a client and clone the updated state of registry so Eureka server acts itself as Client we can perform this by just configuring
eureka.client.serviceUrl.defaultZone property.
Eureka server works in two modes.
Standalone : In local, we configure stand-alone mode where we have only one Eureka server i.e localhost and same cloning property from itself.
Clustered : where we have multiple Eureka server each cloning it states from its peer.
Eureka server can have properties file and communicate with config server as other Microservices do.
Diagram of Eureka server Communication:
Here I will show you a standalone setup of Eureka server.
Step 1: Create a project template from https://start.spring.io/ while creating the template choose the following modules
- Config Client.
- Actuator
- Eureka Server
Step 2: open the downloaded code template and import it as Maven project in your IDE. The pom.xml looks like
<?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>EmployeeEurekaServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EmployeeEurekaServer</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.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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 3: Now open the EmployeeEurekaServerApplication.java and put the annotation @EnableEurekaServer on top of the class. By doing so this application now acts as Service Registry all other Microservices can register themselves by using this service URL we will discuss it in next article.
package com.example.EmployeeEurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EmployeeEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeEurekaServerApplication.class, args);
}
}
Step 4:Now change the application.properties to bootstrap.properties as it may consult with config server if it has any properties defined in config server. In bootstrap.properties file write the following lines
spring.application.name=EmployeeEurekaServer
eureka.client.serviceUrl.defaultZone:http://localhost:9091/eureka/
server.port=9091
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Name
|
Description
|
spring.application.name
|
Unique name for Eureka server service
|
eureka.client.serviceUrl.defaultZone
|
It consult with other Eureka server for sync the service registry as it is standalone mode I give the local server address.
|
server.port
|
In which port server will be bound.
|
eureka.client.register-with-eureka
|
This determines is this server register itself as client as I said earlier Eureka server is also act as client so that it can sync the registry.The value false means prevent itself act as a client.
|
eureka.client.fetch-registry
|
Not register itself in service registry
|
Now our Eureka server setup is completed. We will now run this project as java application . Now hit the following url
http://localhost:9091 you will see the following dashboard
Post a Comment