microservices communication: Eureka Client
In my previous java microservices tutorial example I have created a Eureka Server . In this tutorial I will show you how microservices communication happens. In this tutorial, I will create a Eureka client which communicates with Eureka server Aka microservices service registry.
Before deep dive into the code let me tell you the about the Eureka client/server Communication in a detailed manner.
Eureka Client/Server Communication:
Talk to Eureka Server: At first, while the Eureka client is bootstrapped it tries to talks with the Eureka server which is in same Zone so suppose a Eureka client instance is in Asia Zone so it tries to connect Asia Zones Eureka server if it is not available then it fails over to another Zone.
It determines using eureka.client.serviceUrl.defaultZone property, so we need to give a URL of the same zone Eureka server. And for failover to another Zone Eureka server should be peer connected.
Register : Next Eureka client/microservices shares the instances information with Eureka server and register itself with the {service-id } (spring.application.name).
HeartBeat : After registering is successful after every 30 seconds Eureka client sends heartbeat to Eureka server to renew its leases. So if till 90 seconds if Eureka server not getting any heartbeat from Eureka client it unregisters the Eureka client instance from Service registry and sends the updated registry to all peers and Eureka clients.
Fetching Service Registry : Eureka clients fetch service registry from Eureka server so it can discover other service and communicates with them. After every 30 seconds this service registry information getting updated by receiving delta updates from Eureka server, Please note that Eureka server also caches the delta updates still 3 minutes so Eureka client can receive same delta instances multiple times. After receiving delta updates it again tries to compare its local registry with server registry to check delta is applied successfully or not if there are any mismatches it pulls all the registry again.
Unregister : when the Client instances are going to shut down it sends a cancel signal to Eureka Server so Eureka server unregisters it from its registry.
Synchronization : As Eureka server manages cache and also clients, it may takes some time to get reflected the original status to peers. It’s called Time lag.
Coding Time:
We will make the EmployeeSearchService as Eureka client to do that first we need to add discovery Module of Spring boot in Maven pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Now add the @EnableDiscoveryClient annotation on top of EmployeeSerachServiceApplication.java
package com.example.EmployeeSerachService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EmployeeSearchServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeSearchServiceApplication.class, args);
}
}
After that Run the Eureka server first then EmployeeSearchApplication you will see Employee SearchApplication is registered in Eureka server with service id EmployeeSearchService.