<p>In this article, we will discuss how to do Microservice Registry Discovery using Netflix Eureka. We will see an <a href="https://dineshonjava.com/microservices-with-spring-boot/">example of a Microservice</a> and also will register it with Netflix Eureka and then will discover a Microservice using the Eureka server.</p>
<h2>Table of Content</h2>
<ol>
<li>What is Eureka</li>
<li>Why we use Service Discovery</li>
<li>Eureka Registry</li>
<li>Making microservice to act as a Eureka Server</li>
<li>Make your Application as a client of Eureka Server</li>
</ol>
<div align="center"></div>
<h2>Microservice Registry Discovery using Netflix Eureka Component</h2>
<h3>What is Eureka</h3>
<p>Eureka is a Component of Netflix which is integrated with spring boot to create microservices architecture.<br />
In this post, we will discuss the Eureka service registry/ discovery with the help of annotation. How simple annotation help us to configure a service register and help to discover the server.</p>
<h3>Why we use Service Discovery</h3>
<p>Suppose we have many microservices which are distributed on the different server. Where instances of the service dynamically change due to some property changes like, IP Address, a name of the instance, and failures which is common nowadays. Then in this situation service discovery help us to automatically identify the server by communicating with each other. Let&#8217;s see the following diagram:</p>
<p><img class="aligncenter wp-image-4204 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/08/discovery.png" alt="MicroService Registry Discovery using Netflix Eureka Component" width="603" height="294" /></p>
<p>As you can see in the above diagram, this is how services register itself with Eureka server to make available to other services.</p>
<h3>Eureka Registering</h3>
<p>First of all, Microservices add dependency of Eureka Client in the pom.xml file. And when run the application, Microservices provide metadata about itself to the Eureka Server like Hostname, the port on which service is available, Health indicator URL if you have added actuator in the classpath.</p>
<p>Now Eureka communicate with each other with the help of Heartbeat protocol to check the instance is available or not. If an instance is failed then eureka server remove that instance from the configuration table.</p>
<h3>Making microservice to act as a Eureka Server</h3>
<p>To implement Eureka server to act as the service registry, You need to add a <em><strong>spring-cloud-started-eureka-server</strong></em> dependency in your pom file:</p>
<pre class=""><;dependency>; 
	<;groupId>;org.springframework.cloud<;/groupId>; 
	<;artifactId>;spring-cloud-starter-eureka-server<;/artifactId>; 
<;/dependency>;</pre>
<p>And you need to add property information in the YAML file like port information, Make this application as a eureka server, not Eureka client by using <em><strong>registerWithEureka</strong> false</em>. Let&#8217;s see the following diagram:</p>
<p><img class="size-full wp-image-4205 aligncenter" src="https://dineshonjava.com/wp-content/uploads/2018/08/yaml.png" alt="yaml" width="423" height="204" /></p>
<p>After adding this dependency to the application YAML file. Then you need to enable the Eureka server in a Sprtingboot main application class using <em><strong>@EnableEurekaServer</strong></em> annotation. Which is basically check your classpath weather the jar is available or not if available then it will make your application as a Eureka server.</p>
<pre class="highlight">package com.dineshonjava; 
 
@SuppressWarnings("unused") 
@SpringBootApplication 
@EnableEurekaServer 
public class DiscoveryMicroserviceServerApplication { 
 
	public static void main(String[] args) { 
		SpringApplication.run(DiscoveryMicroserviceServerApplication.class, args); 
	} 
} 
 
</pre>
<p>You can verify it by using URL <strong>http://localhost:1111</strong> to view the Eureka Dashboard. Let&#8217;s see the following diagram about the Eureka Dashboard:</p>
<p><img class="aligncenter size-full wp-image-4206" src="https://dineshonjava.com/wp-content/uploads/2018/08/eureka.png" alt="eureka dashboard" width="1414" height="571" /></p>
<h3>Make your Application as a client of Eureka Server:</h3>
<p>Now Add below dependency in your client application to make your microservice to register with Eureka Server which Continuous communicating with each other to give information about available services in the microservice architecture application.</p>
<p>1. Add below dependency in the pom file</p>
<pre class="highlight"><;dependency>; 
	 <;groupId>;org.springframework.cloud<;/groupId>; 
	 <;artifactId>;spring-cloud-starter-eureka<;/artifactId>; 
 <;/dependency>; 
</pre>
<p>2. Add the URL of the Eureka Service on which Eureka server is available and where this Microservice will register itself in the <strong><em>application.</em>yaml</strong> file.</p>
<pre class="highlight"><em>eureka:</em> 
<em> client:</em> 
<em> serviceUrl:</em> 
<em> </em>defaultZone<em>: http://localhost:1111/eureka/</em> 
</pre>
<p>3. Add the annotation <strong>@EnableDiscoveryClient</strong> in the main application file where the <strong>@SpringBootApplication</strong> annotation is used.</p>
<p>Now Run the Eureka Server application first then run the client application. It will take maximum 30 sec to register client application with Eureka server. Refresh the Eureka server application to reflect the Eureka client application in the dashboard.</p>
<p><img class="aligncenter size-full wp-image-4207" src="https://dineshonjava.com/wp-content/uploads/2018/08/eureka-dashboard.png" alt="eureka-dashboard" width="1410" height="360" /></p>
<p> ;</p>
<div align="center">
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/mastering-spring-boot-2-0-and-spring-cloud/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/software-architecture-patterns-and-designs/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4203'});
});
</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…