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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Microservices With JHipster
  • A New Era Of Spring Cloud
  • Building Mancala Game in Microservices Using Spring Boot (Part 3: Web Client Microservice Implementation Using Vaadin)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  1. DZone
  2. Coding
  3. Frameworks
  4. Microservices: Service Registration and Discovery

Microservices: Service Registration and Discovery

See an example of a microservices application developed with Vaadin and Spring Boot to learn about implementing service registration and discovery.

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
May. 07, 18 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
47.6K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous article of this series, we explored a demo application designed with a microservices architecture and implemented with Spring Boot and Vaadin Framework 8. The demo application is available on GitHub and you can follow the instructions there to run it by yourself.

In this article, we’ll explore the concept of service registration and discovery, a mechanism that enables microservices to consume other microservices without knowing their exact location (usually a URL).

Why Do We Need This?

Without this, the services’ locations would be coupled to each other, causing a hard to maintain system. The basic problem is evident: How do services know the IP and port of a service they need to consume? In a simple architecture, a static configuration works pretty well. Each service is deployed at the same location and it rarely changes. But this is not the case in microservices architectures.

One of the objectives of microservices is to enable the development teams to deploy and scale their applications independently. As more services and instances are added to the system, service locations start to change frequently: partly because of manual or automatic scaling, partly because of continuous delivery processes. Instances will start to go up and down frequently as scaling and deployment happen. Clearly, a static configuration of the location of services is not a viable solution. A dynamic configuration is needed.

How Does It Work?

The dynamic configuration containing the location of available services is held in a discovery service. Each microservice only needs to know about the location of the discovery service and will use it to get the location of other microservices. In the demo application, the discovery service is implemented in the discovery-server application.

Let’s take two of the microservices in the demo application as an example: The admin-application (a web Vaadin application) and the biz-application (a Spring Data REST web service). The admin-application needs the biz-application for reading and writing data. The following diagram shows the communication flow:

1. The biz-application starts up and tells the discovery-server it is available.

2. The admin-application starts up and asks the discovery-server for the location of the biz-application.

3. The discovery-server looks-up in its internal database and returns the location of the biz-application.

4. The admin-application can now make direct requests to the biz-application.

What happens when more instances of the biz-application are added? The process is shown in the following diagram:

Notice how each biz-application instance registers itself (1 and 2) and how the discovery-server returns all available locations to the admin-application (4) which in turn selects one of them to make requests (5). Which instance to select is done through a client-side load balancer with fault tolerance features. This article focuses on service registration and discovery. Load balancing and fault tolerance are covered in a later article of this series.

Implementing a Discovery Server With Eureka

There are many implementations suitable for service discovery. SmartStack, Zookeeper, Etcd, Consul, NSQ, Serf, and Doozer, are all good examples. The demo application uses the battle-proven Eureka by Netflix.

Spring Cloud offers excellent integration with Eureka (and with Netflix OSS in general) through Spring Cloud Netflix. Thanks to Spring Boot’s autoconfiguration features, implementing a discovery service with Eureka is extremely easy.

Use the Spring Initializr to create a new Spring Boot application named discovery-server and include the Eureka Server and Actuator (optional) dependencies:

Make sure to use version 2.0.0 (a milestone version at the time of writing) of Spring Boot. Click Generate Project, extract the generated file, and import the Maven project into your favorite IDE.

Open up the DiscoveryServerApplication class and annotate it with @EnableEurekaServer:

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
   ...
}

Remove the application.properties file and create an application.yml file with the following content:

server.port: 7001
spring.application.name: discovery-server

eureka:
 instance:
   hostname: localhost
   leaseRenewalIntervalInSeconds: 2
   leaseExpirationDurationInSeconds: 2
 server:
   evictionIntervalTimerInMs: 1000
   response-cache-update-interval-ms: 1000
 client:
   registerWithEureka: false
   fetchRegistry: false

This file configures the application to use port 7001 and the name discovery-server. It also contains the configuration for Eureka. Notice the last two lines. They are needed to avoid the discovery-server from registering with itself.

The rest of the configuration is not intended to serve a production system. It’s adjusted to handle only a few instances of each microservice and to allow you to quickly see changes while trying the application. In production deployments, you should tune it according to your specific requirements. See Eureka’s documentation for more information on this.

Compile and package the application using Maven and run it as a standard Java program using the command line (or your IDE):

cd discovery-server
mvn package
java -jar target/discovery-server-0.0.1-SNAPSHOT.jar

Go to http://localhost:7001 to see the dashboard included with Eureka. At this point, no instances are listed yet, but we’ll get to that in the next articles of this series.

microservice Web Service Discovery (law) Spring Framework application Spring Cloud Spring Boot

Published at DZone with permission of Alejandro Duarte. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Microservices With JHipster
  • A New Era Of Spring Cloud
  • Building Mancala Game in Microservices Using Spring Boot (Part 3: Web Client Microservice Implementation Using Vaadin)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

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!