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.
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.
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:
As you can see in the above diagram, this is how services register itself with Eureka server to make available to other services.
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.
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:
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:
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.
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…