Microservice Registry Discovery using Netflix Eureka

In this article, we will discuss how to do Microservice Registry Discovery using Netflix Eureka. We will see an example of a Microservice and also will register it with Netflix Eureka and then will discover a Microservice using the Eureka server.

Table of Content

  1. What is Eureka
  2. Why we use Service Discovery
  3. Eureka Registry
  4. Making microservice to act as a Eureka Server
  5. Make your Application as a client of Eureka Server

Microservice Registry Discovery using Netflix Eureka Component

What is Eureka

Eureka is a Component of Netflix which is integrated with spring boot to create microservices architecture.
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.

Why we use Service Discovery

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’s see the following diagram:

MicroService Registry Discovery using Netflix Eureka Component

As you can see in the above diagram, this is how services register itself with Eureka server to make available to other services.

Eureka Registering

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.

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.

Making microservice to act as a Eureka Server

To implement Eureka server to act as the service registry, You need to add a spring-cloud-started-eureka-server dependency in your pom file:

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

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 registerWithEureka false. Let’s see the following diagram:

yaml

After adding this dependency to the application YAML file. Then you need to enable the Eureka server in a Sprtingboot main application class using @EnableEurekaServer 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.

package com.dineshonjava;

@SuppressWarnings("unused")
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryMicroserviceServerApplication {

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

You can verify it by using URL http://localhost:1111 to view the Eureka Dashboard. Let’s see the following diagram about the Eureka Dashboard:

eureka dashboard

Make your Application as a client of Eureka Server:

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.

1. Add below dependency in the pom file

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

2. Add the URL of the Eureka Service on which Eureka server is available and where this Microservice will register itself in the application.yaml file.

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

3. Add the annotation @EnableDiscoveryClient in the main application file where the @SpringBootApplication annotation is used.

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.

eureka-dashboard

 

Previous
Next

9 Comments

  1. Dinesh Rajput August 19, 2018
  2. Anonymous August 20, 2018
  3. Fahim August 20, 2018
  4. Dinesh August 20, 2018
  5. Fahim August 20, 2018
  6. Nasir August 20, 2018
  7. Reliablepharmacy August 22, 2018
  8. Andykaufmantony August 22, 2018
  9. Rajwant August 25, 2018