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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Using Kong Ingress Controller with Spring Boot Services
  • Component Tests for Spring Cloud Microservices
  • Migration of Microservice Applications From WebLogic to Openshift

Trending

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Memory Leak Due to Time-Taking finalize() Method
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • Navigating Double and Triple Extortion Tactics
  1. DZone
  2. Coding
  3. Frameworks
  4. Microservices With Spring Boot - Part 5 - Using Eureka Naming Server

Microservices With Spring Boot - Part 5 - Using Eureka Naming Server

In this final part of our microservices architecture series, we will learn to enable a Eureka Naming Server and allow the microservices to communicate with it.

By 
Ranga Karanam user avatar
Ranga Karanam
DZone Core CORE ·
Jan. 29, 18 · Tutorial
Likes (32)
Comment
Save
Tweet
Share
36.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this series, let's learn the basics of microservices and microservices architectures. We will also start looking at a basic implementation of a microservice with Spring Boot. We will create a couple of microservices and get them to talk to each other using Eureka Naming Server and Ribbon for Client Side Load Balancing.

This is part 5 of this series. In this part, we will focus on enabling Eureka Naming Server and have the microservices communicate with it.

Microservices with Spring Boot

  • Part 1 - Getting Started with Microservices Architecture
  • Part 2 - Creating Forex Microservice
  • Part 3 - Creating Currency Conversion Microservice
  • Part 4 - Using Ribbon for Load Balancing
  • Current Part - Part 5 - Using Eureka Naming Server


You will learn:

  • What is the need for a Naming Server?
  • What is Eureka?
  • How does a Naming Server enable location transparency between microservices?

Microservices Overview

In Parts 2 and 3, we created two microservices and established communication between them.

In Part 4, we used Ribbon to distribute load between the two instances of Forex Service. However, we are hardcoding the URLs of both instances of the Forex Service in CCS. That means that every time there is a new instance of FS, we would need to change the configuration of CCS. That's not cool.

In this part, we will use Eureka Naming Server to fix this problem.

Tools you will need:

  • Maven 3.0+ is your build tool
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Complete Maven Project With Code Examples

Our GitHub repository has all the code examples.

Bootstrapping Eureka Naming Server With Spring Initializr

Creating a Eureka Naming Server with Spring Initializr is a cake walk. Spring Initializr is a great tool for bootstrapping your Spring Boot projects. You can create a wide variety of projects using Spring Initializr.

The following steps have to be done for a web services project:

  • Launch Spring Initializr and choose the following:
    • Choose com.in28minutes.springboot.microservice.eureka.naming.server as Group
    • Choose spring-boot-microservice-eureka-naming-server as Artifact
    • Choose following dependencies
  • Click Generate Project.
  • Import the project into Eclipse. File -> Import -> Existing Maven Project.
  • Do not forget to choose Eureka in the dependencies.

Enabling Eureka

EnableEurekaServer in SpringBootMicroserviceEurekaNamingServerApplication.

@SpringBootApplication
@EnableEurekaServer
public class SpringBootMicroserviceEurekaNamingServerApplication {

Configure the application name and port for the Eureka Server:

/spring-boot-microservice-eureka-naming-server/src/main/resources/application.properties

spring.application.name=netflix-eureka-naming-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false


Launching Eureka Naming Server

Launch SpringBootMicroserviceEurekaNamingServerApplication as a Java application.

You can launch Eureka at http://localhost:8761

You will see that there are no instances connected to Eureka yet:

Connect FS and CCS Microservices With Eureka

Make these changes on both the microservices

Add to pom.xml:

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

Configure Eureka URL in application.properties:

eureka.client.service-url.default-zone=http://localhost:8761/eureka

Restart all the instances of CCS and FS. You will see that the CCS and FS microservices are registered with Eureka Naming Server. That's cool!

This screenshot shows how to launch an additional instance of Forex Service on 8081.

You will see that one instance of CCS and two instances of FS microservices are registered with Eureka Naming Server.

Routing Ribbon Requests Through Eureka

All that you would need to do is to remove this configuration. Remove this configuration from application.properties:

forex-service.ribbon.listOfServers=localhost:8000,localhost:8001

Restart the CCS instance.

Eureka in Action

Currently, we have the following services up and running:

  • Currency Conversion Microservice (CCS) on 8100
  • Two instances of Forex Microservice on 8000 and 8001
  • Eureka Server launched

Now you will see that the requests to CCS will be distributed between the two instances of the Forex Microservice by Ribbon through Eureka.

Request 1

GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8000,
}


Request 2

GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8001,
}


You can see that the port numbers in the two responses are different.

Exercise: Launch another instance of Forex Service on 8002. You will see that load gets automatically routed to it as well.

Cool! That's awesome, isn't it?

Summary

We have now created two microservices and established communication between them.

We are using Ribbon to distribute load between the two instances of Forex Service and Eureka as the naming server. When we launch new instances of Forex Service, you will see that load is automatically distributed to them.

The idea behind this series of five articles was to give the flavor of Spring Boot and Spring Cloud with microservices.

There is a lot more ground to cover with microservices. Until next time, cheers!

The complete code example for this project can be found in the GitHub repository.

Spring Framework microservice Spring Boot

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Using Kong Ingress Controller with Spring Boot Services
  • Component Tests for Spring Cloud Microservices
  • Migration of Microservice Applications From WebLogic to Openshift

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!