Microservices

Implementing Microservice Registry with Eureka

In the article, we will discuss how to implement a microservice registry with Eureka. The microservice registry is a database of the instances of the microservices. It stores its locations. The instances of the service are registered with the registry service on startup and deregistered automatically at the time of shutdown. Netflix provides a registry service server that is Eureka. Spring Boot provides integration with the Netflix API, so we can easily implement a microservice registry using Netflix’s Eureka server.

In the previous article, we have discussed how to discover a microservices and also explained microservices discovery patterns such as the client-side discovery and server-side discovery patterns.

The client service, or external routers, make a query to find the available instances of a service. The registry server provides all of the available instances of the requested service. Take a look at the following diagram, which shows Service Registry and discovery with Eureka:

Microservice Registry with Eureka

As you can see, all services register with the Eureka server to make themselves available. In the following section, we’ll look at how to register services with Eureka.

Microservice Registry with Eureka

We can use the Eureka registry server with Spring Cloud because it integrates with Netflix. First, add the following Maven dependency into your application’s pom.xml file:

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

Let’s create a service, the Account Service, and register it with the Eureka server. The application class of the Account Service in Spring Boot will look as follows:

package com.dineshonjava.bookshop.accountservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class AccountServiceApplication {
	public static void main(String[] args) {
		SpringApplication.run(AccountServiceApplication.class, args);	
	}
}

As you can see in the preceding code, the @EnableEurekaClient annotation activates the Netflix EurekaClient implementation. This Account Service registers itself with the Eureka server, which makes it available. Take a look at the following YML configuration file for this Account Service application:

spring:
	application:
		name: account-service

server:
	port: 1111

eureka:
	client:
		service-url:
		default-zone: ${EUREKA_URI:http://localhost:8761/eureka}
		instance:
		prefer-ip-address: true

As you can see, the service name is account-service, and it will run on port 1111. This service registers itself with the Eureka server running on http://localhost:8761/eureka.

In the following section, we will implement the Eureka server with Spring Boot.

Implementing the Eureka Discovery server

It is very simple to implement the Eureka server application using Spring Boot; just use the following code:

package com.dineshonjava.bookshop.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

As you can see in the preceding code, the Eureka server is a small Spring Boot application. The @EnableEurekaServer annotation provides Netflix’s Eureka registry server. Take a look at the following Eureka server application configuration file for more information:

server:
	port: 8761

eureka:
	instance:
		hostname: localhost
	client:
		registerWithEureka: false
		fetchRegistry: false
	serviceUrl:
		defaultZone:			http://${eureka.instance.hostname}:${server.port}/eureka/

This server application uses port 8761.

Now, let’s take a look at the following screenshot:

Account service registered with Eureka server

As you can see, the ACCOUNT-SERVICE has now been successfully registered with the Eureka server.

Previous
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago