DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • IDE Changing as Fast as Cloud Native
  • Understanding the Role of ERP Systems in Modern Software Development
  • Stack in Data Structures

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • IDE Changing as Fast as Cloud Native
  • Understanding the Role of ERP Systems in Modern Software Development
  • Stack in Data Structures
  1. DZone
  2. Data Engineering
  3. Data
  4. Microservices Communication: Eureka Client

Microservices Communication: Eureka Client

Eureka! No more need to store service URLs in code properties and update them with every change. Service discovery with Eureka will solve the problem for us.

Shamik Mitra user avatar by
Shamik Mitra
·
Jul. 17, 17 · Tutorial
Like (10)
Save
Tweet
Share
43.06K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous Java microservices tutorial example, I created a Eureka Server. In this tutorial, I will show you how microservice communication happens. In this tutorial, I will create a Eureka client that communicates with a Eureka server — a microservices service registry.

If you haven't gone through the previous articles, please click here.

Before diving into the code, let me tell you more about the about Eureka's client/server communication.

Eureka Client/Server Communication

Talk to the Eureka server: At first, while the Eureka client is bootstrapped, it tries to talk with the Eureka server in the same Zone. So, suppose a Eureka client instance is in the Asia Zone —  it tries to connect to the Asia Zone's Eureka server. If it is not available, then it fails over to another Zone.

It determines its targets using the eureka.client.serviceUrl.defaultZone property, so we need to give a URL of the same Zone's Eureka server. And for failing over to another Zone, the Eureka server should be peer connected.

Register: Next, the Eureka client/microservices share the instance information with the Eureka server and registers themselves with the {service-id } (spring.application.name).

Heartbeat: After registration is successful, after every 30 seconds, the Eureka client sends a heartbeat to the Eureka server to renew its leases. So, if after 90 seconds the Eureka server does not get any heartbeat from the Eureka client, it unregisters the Eureka client instance from the service registry and sends the updated registry to all peers and Eureka clients.

Fetching service registry: Eureka clients fetch the service registry from the Eureka server so it can discover other services and communicate with them. After every 30 seconds, this service registry information gets updated by receiving delta updates from the Eureka server. Please note that the Eureka server also caches the delta updates every 3 minutes, so the Eureka client can receive the same delta instances multiple times. After receiving delta updates, it again tries to compare its local registry with the server registry to check that delta is successfully applied. If there are any mismatches, it pulls all the registries again.

Unregister: When the client instances are going to shut down, they send a cancel signal to the Eureka client so the Eureka server unregisters it from its registry.

Synchronization: Be aware that there may be some time lag as the Eureka server and client manage the cache.

microservices communication

Coding Time:

We will make the EmployeeSearchService a Eureka client. To do that, first, we need to add the discovery module for Spring Boot in the Maven pom.xml

<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 that the Employee Search application is registered in the Eureka server with the service id EmployeeSearchService.

spring cloud tutorial

microservice

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • IDE Changing as Fast as Cloud Native
  • Understanding the Role of ERP Systems in Modern Software Development
  • Stack in Data Structures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: