Spring Cloud Netflix Eureka Server
Spring Cloud Netflix Eureka Server is like a phone directory for your microservices. Just like how you would look up someone’s phone number in a directory, Eureka Server helps your microservices find each other in a distributed system.
Here’s a simpler explanation:
- Phone Directory Analogy: Imagine you have a phone directory where everyone in your neighborhood lists their phone number and address. This directory is like Eureka Server.
- Registration: Each house (microservice) in the neighborhood registers itself with the phone directory when it’s set up. It tells the directory its address (network location) and what services it offers.
- Discovery: When someone (another microservice) needs a particular service, they consult the phone directory (Eureka Server) to find out which houses (microservices) offer that service and where they are located.
- Load Balancing: If multiple houses offer the same service, the directory helps distribute the requests evenly among them, like a traffic officer directing cars to different lanes to balance the load.
- Fault Tolerance: If a house (microservice) becomes unavailable for some reason, the directory knows about it and can direct requests to other available houses offering the same service.
So, in easy language, Spring Cloud Netflix Eureka Server helps microservices in your application find each other, distribute requests evenly, and handle failures gracefully, just like a helpful phone directory for your neighborhood.
Let’s consider a simple e-commerce application consisting of multiple microservices:
- Product Service: Manages product information such as name, description, and price.
- Order Service: Handles order creation, order status, and order details.
- User Service: Manages user accounts and authentication.
- Payment Service: Deals with payment processing.
- Notification Service: Sends notifications to users about their orders.
Now, let’s see how Spring Cloud Netflix Eureka Server fits into this scenario:
1. Product Service
The Product Service registers itself with the Eureka Server upon startup. It provides endpoints for fetching product information.
2. Order Service
The Order Service also registers itself with Eureka Server. When a user places an order, the Order Service communicates with the Product Service to fetch product details.
3. User Service
The User Service handles user authentication and registration. It communicates with the Order Service for placing orders and with the Notification Service for sending notifications.
4. Payment Service
The Payment Service interacts with the Order Service to process payments for orders placed by users.
5. Notification Service
The Notification Service registers itself with Eureka Server. It listens for events from other services, such as order placement or payment confirmation, and sends notifications to users accordingly.
Real-time Example:
- User places an order: The frontend application sends a request to the Order Service to place an order.
- Order Service communicates with other services: The Order Service fetches product details from the Product Service, processes payment using the Payment Service, and updates the order status.
- Notification Service sends a notification: Once the order is confirmed, the Notification Service sends a notification to the user about the order confirmation.
Benefits of Eureka Server:
- Service Discovery: Each microservice can easily find and communicate with other services without hardcoding their locations.
- Load Balancing: If there are multiple instances of a service, Eureka helps distribute incoming requests evenly among them.
- Fault Tolerance: If a service instance becomes unavailable, Eureka automatically redirects requests to other available instances.
In this real-time example, Spring Cloud Netflix Eureka Server facilitates the seamless communication and coordination between microservices in the e-commerce application, enabling features like service discovery, load balancing, and fault tolerance.
Setting up Eureka Server
Make sure you have add spring-cloud-starter-netflix-eureka-server
while creating spring eureka project, must add dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- First, let’s create a Spring Boot application that acts as the Eureka server.
use @EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2. Create Application.yml (or use application.properties)
register-with-eureka: false
- This property tells the Eureka client whether it should register itself with the Eureka server or not.
- When set to
false
, it means that the application running this configuration will not register itself with the Eureka server. This is useful for standalone applications that do not need to be discovered by other services. - In some scenarios, you might have certain microservices that don’t need to be registered with Eureka, such as admin services or batch jobs. For these cases, you can disable registration to reduce unnecessary network traffic and overhead.
fetch-registry: false
- This property tells the Eureka client whether it should fetch the service registry from the Eureka server.
- When set to
false
, it means that the application running this configuration will not fetch the service registry from the Eureka server. This is useful for standalone applications that do not need to discover other services. - In scenarios where a microservice does not need to discover other services (for example, if it only provides functionality and doesn’t consume services from other microservices), you can disable fetching the registry to optimize performance and reduce startup time.
# application.yml for Eureka Server
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
4.Accessing Eureka Dashboard
Open your browser and go to http://localhost:8761/
Now you can register your microservices with this.
Creating Eureka Client
Make sure you have addspring-cloud-starter-netflix-eureka-client
dependencies in your pom.xml
Now, let’s create a Spring Boot application(order service, payment service) that registers itself with the Eureka server as a client.
- Use @EnableDiscoveryClient — (this is not required from Spring Cloud 2020.0.0 (Ilford))
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
2.Use below in application. Properties
server.port=8080
spring.application.name=eureka-client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
or create application.yml
server:
port: 8080
spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
In this application.yml
configuration:
server.port: 8080
specifies that the application will run on port 8080.spring.application.name: eureka-client
sets the name of the Spring Boot application to "eureka-client".eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
tells the Eureka client where to find the Eureka Server. The Eureka Server URL is set tohttp://localhost:8761/eureka/
.- This is the default URL used by Eureka Server for service registration and discovery.
This configuration allows the Eureka client to register itself with the Eureka Server running on localhost
port 8761
and to discover other services registered with the same Eureka Server.
Note:- Starting from Spring Cloud 2020.0.0 (Ilford), @EnableEurekaClient
annotation is not required if you have the spring-cloud-starter-netflix-eureka-client
dependency on your classpath. The Eureka client functionality is automatically enabled when the dependency is present.
Here’s how you can use Eureka client without explicitly using @EnableEurekaClient
:
- Dependency Configuration: Ensure that you have the
spring-cloud-starter-netflix-eureka-client
dependency in yourpom.xml
orbuild.gradle
file. - Eureka Configuration: In your
application.properties
orapplication.yml
file, configure the Eureka client properties, including the URL of the Eureka server.