<p class="wp-block-paragraph">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.</p>



<p class="wp-block-paragraph">In the <a href="https://dineshonjava.com/microservice-discovery-patterns-and-registry/"><strong>previous article</strong></a>, we have discussed how to discover a microservices and also explained microservices discovery patterns such as the <strong>client-side discovery</strong> and <strong>server-side discovery </strong>patterns.</p>



<p class="wp-block-paragraph">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: </p>



<div class="wp-block-image"><figure class="aligncenter"><img src="https://dineshonjava.com/wp-content/uploads/2019/07/discovery-1.png" class="wp-image-4479"/><figcaption>Microservice Registry with Eureka</figcaption></figure></div>



<p class="wp-block-paragraph"> As you can see, all services register with the Eureka server to make themselves available. In the following section, we&#8217;ll look at how to register services with Eureka. </p>



<h2 class="wp-block-heading">Microservice Registry with Eureka</h2>



<p class="wp-block-paragraph">We can use the Eureka registry server with Spring Cloud because it integrates with Netflix. First, add the following Maven dependency into your application&#8217;s <strong>pom.xml</strong> file:</p>



<pre class="wp-block-code"><code><;dependency>
	<;groupId>org.springframework.cloud<;/groupId>
	<;artifactId>spring-cloud-starter-eureka-server<;/artifactId>
<;/dependency></code></pre>



<p class="wp-block-paragraph">Let&#8217;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:</p>



<pre class="wp-block-code"><code>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);	
	}
}</code></pre>



<p class="wp-block-paragraph">As you can see in the preceding code, the <strong>@EnableEurekaClient</strong> annotation activates the Netflix <strong>EurekaClient </strong>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:</p>



<pre class="wp-block-code"><code>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</code></pre>



<p class="wp-block-paragraph">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. </p>



<p class="wp-block-paragraph">In the following section, we will implement the Eureka server with Spring Boot.</p>



<h3 class="wp-block-heading">Implementing the Eureka Discovery server</h3>



<p class="wp-block-paragraph">It is very simple to implement the Eureka server application using Spring Boot; just use the following code:</p>



<pre class="wp-block-code"><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);
	}
}</code></pre>



<p class="wp-block-paragraph">As you can see in the preceding code, the Eureka server is a small Spring Boot application. The <strong>@EnableEurekaServer</strong> annotation provides Netflix&#8217;s Eureka registry server. Take a look at the following Eureka server application configuration file for more information:</p>



<pre class="wp-block-code"><code>server:
	port: 8761

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



<p class="wp-block-paragraph">This server application uses port 8761.</p>



<p class="wp-block-paragraph">Now, let&#8217;s take a look at the following screenshot:</p>



<div class="wp-block-image"><figure class="aligncenter"><img src="https://dineshonjava.com/wp-content/uploads/2019/07/eureka.png" class="wp-image-4480"/><figcaption>Account service registered with Eureka server</figcaption></figure></div>



<p class="wp-block-paragraph">As you can see, the ACCOUNT-SERVICE has now been successfully registered with the Eureka server.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/microservice-discovery-patterns-and-registry/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4478'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…