What is Netflix Eureka?
Netflix Eureka is a service discovery server, designed to keep track of microservices in a distributed system. It acts as a registry, where each microservice registers itself and also queries the registry to discover other services. This setup makes it possible to dynamically scale services and handle failure scenarios with minimal disruption.
Key Features of Eureka:
- Service Registration: Microservices register with the Eureka server, providing details like IP addresses, ports, and health checks.
- Service Discovery: Other services can query the Eureka server to get the location of a registered service.
- Fault Tolerance: Eureka automatically deregisters unhealthy services and replicates registry data to other instances, providing resiliency.
- Load Balancing: Eureka works with Spring Cloud LoadBalancer or Netflix Ribbon to distribute traffic across multiple instances of a service.
How Service Discovery Works?
Here’s a high-level overview of how service discovery using Eureka works in a Spring Boot microservices architecture:
- Service Registration: Microservices register themselves with Eureka by providing their instance details (host, port, health status).
- Heartbeat (Service down/up): Each microservice sends periodic heartbeats to the Eureka server to indicate it is still available. If heartbeats stop, Eureka marks the service as unavailable.
- Service Discovery: Microservices or API Gateways query Eureka to discover the location of other services by their registered names.
Development Steps
- Setting Up Eureka Server in Spring Boot
- Setting Up Eureka Client (Employee-Service, Department-Service,etc)
👉1. Setting Up the Eureka Server in Spring Boot: Below are the steps to setting up a Eureka Server.
- Create Spring boot project as Microservice (service-registry)
- Import in IntelliJ Idea as a module of the parent folder.
- Add @EnableEurekaServer annotation in Main class
- Disable Eureka Server as Eureka Client (Configure in Application.properties file)
- Launch Eureka Server in the browser (UI)
Step 1: Create a Spring boot project as Microservice (service-registry).
Step 2: Import in IntelliJ Idea as a module of the parent folder. Import system same for all microservices. After creating any microservice, unzip it and place it in the parent directory. Then open IDE and import it by following below.
Step 3: Add @EnableEurekaServer annotation in the Main class.
Step 4: Disable Eureka Server as Eureka Client (Configure in Application.properties file): By default Eureka Server also a Eureka Client. Therefore, it requires disabling by configuring in the Application.properties file.
Step 5: Launch Eureka Server in the browser (UI) : No instance/service added yet.
👉2. Setting Up Eureka Client (Employee-Service, Department-Service,etc)
- Register Employee-Service Microservice as Eureka Client
- Run Employee-Service and check the instance in Eureka Server (Browser UI)
- And same way Register, Run, and check Department-Service in the Eureka Server as a Eureka Client.
✈ Register Employee-Service Microservice as an Eureka Client by following the below 3 steps.
Steps 1: Add below maven dependency
- Eureka Discovery Client
- Dependency Manager [Add, if not exist yet]
- Properties [Add, if not exist yet]
Step 2: Add the Eureka server URL in the Application.properties file of Employee-Service microservice.
Step 3: Give the application name. This name would be used as the ID of Employee-Service Microservice. This ID will be used instead of the host/port of Employee-Service during communication among microservices or API Gateway.
spring.application.name=EMPLOYEE-SERVICE
✈ Run the Employee-Service and verify its instance in the Eureka Server dashboard through your browser.
Employee Service's instance added in Eureka Server.
✈ Department-Service : Same add Department-Service as Eureka Client and check in Eureka UI.
Done 👍
0 Comments