Netflix Eureka: The Tool That Services Play “Hide and Seek” With at the Top Level
Netflix Eureka is the equivalent of a magic wand for service discovery in a microservices architecture. That’s how Eureka ensures all those services can find each other and communicate, with solutions that might bring you as much joy as eating chocolate cake. In this article, we will discuss the important aspects of Netflix Eureka, how it works, and a sample application using it. Are you ready to enter into this mysterious world of services?
What is Netflix Eureka?
Netflix Eureka is an open-source service discovery tool developed at Netflix. Summary: “We know how to do this stuff.” In a microservices architecture, it is used for the dynamic location and management of different services. In distributed systems running on Amazon Web Services, Eureka is like a savior — saving you from rivaling services.
Key Features
- Service Registration and Discovery: Services register with the Eureka server and can be discovered by other services. Think of it as Facebook, but without the drama.
- Health Checks: Services are checked on regularly for good health and kept up-to-date. No sick services are allowed!
- High Availability: High availability is provided with multiple Eureka server setups. No more “I didn’t know” excuse; everyone is prepared around here!
- Dynamic Configuration: The services are dynamically configured and updated. New update? It’s child’s play with Eureka!
Architecture of Eureka
Eureka has two main superpowers:
- Eureka Server: the central superhero where services register and get discovered
- Eureka Client: the other superhero, letting services register with the Eureka server and discover other services. The whole of the two behave like a Batman and Robin.
Sample Application
Let’s now build a sample application which is playful and also supports Eureka Server and Eureka Client.
1. Setup Eureka Server
First let’s develop the Eureka Server. We can build a eureka server very easily using Spring Boot.
Add necessary dependencies to our pom.xml
file:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Next, create our main class and define it as a Eureka Server:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Configure the application.yml
file:
server:
port: 2424
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
After these steps, we can start the Eureka Server. Now we’re ready to register our services. The Eureka Server is like a town square where services shout, “I’m here!”
2. Setting Up Eureka Client
Now, let’s create a Eureka Client that will register with the Eureka Server. We’ll again use Spring Boot for this.
Add the necessary dependencies to our pom.xml
file:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
Create our main class and define it as a Eureka Client:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
Configure the application.yml
file:
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: http://localhost:2424/eureka/
After these steps, we can start the Eureka Client and see it registered on the Eureka Server. The Eureka Client shouts “I’m here!” as it registers with the Eureka Server.
Advanced Topics
We have learned about the basics of Eureka. Let’s dive a little deeper.
High Availability
Eureka is also capable of high availability and therefore can be used across large distributed systems. Many Eureka servers can be established so that the services are always at hand. It balances the load among the services, making sure that the fastest and the most appropriate service is always operational.
eureka:
instance:
hostname: eureka-peer1
client:
serviceUrl:
defaultZone: http://eureka-peer2:2424/eureka/
Health Checks And Restart
Eureka checks the health of services at an interval. When a service is not functioning, Eureka detects it and notifies the other services. This is to ensure that your system remains healthy and running at all times.
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// Define your health check here
boolean healthy = checkHealth();
if (healthy) {
return Health.up().build();
} else {
return Health.down().withDetail("Error", "Something went wrong").build();
}
}
private boolean checkHealth() {
// Define your health check logic here
return true;
}
}
Conclusion
Netflix Eureka is an extremely powerful, enjoyable tool for dynamically discovering and managing services in distributed systems. We have discussed the main features of Eureka and how to use them with a simple example here. Eureka ensures that service discovery is easy, reliable, and interoperable — that is the place where it makes the job more enjoyable. It is similar to how GPS would guide you to the right service!
References
- Netflix Eureka GitHub
- Spring Cloud Netflix Documentation
- Baeldung Eureka Guide