Eureka is a RESTful service (also known as Eureka Server
) that is used for the discovery of other services in a microservices architecture. It follows a client - server model. The Eureka Server maintains a registry of all the available services. Each service instance (client) periodically sends a heartbeat to the Eureka Server to indicate that it is still alive. If the server does not receive a heartbeat for a certain period, it removes the service instance from the registry.
Zookeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. In the context of service discovery, Zookeeper uses a hierarchical namespace (similar to a file system) where service instances can register themselves as nodes. When a service needs to discover another service, it queries Zookeeper to find the relevant nodes.
Eureka is designed with a focus on simplicity and resilience. It has a peer - to - peer architecture where multiple Eureka Servers can be deployed in a cluster. If one server fails, the other servers can still function and provide the service registry. It also has a self - protection mechanism that prevents the removal of service instances from the registry during network partitions.
Zookeeper is designed to provide high - availability and strong consistency. It uses the Zab (Zookeeper Atomic Broadcast) protocol to ensure that all the nodes in the cluster have the same view of the data. It provides a leader - follower model where one node acts as the leader and the others as followers. The leader is responsible for all the write operations, and the followers replicate the data.
First, add the necessary dependencies to your pom.xml
for a Spring Boot project:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
To configure the Eureka Server, create a main class with the @EnableEurekaServer
annotation:
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);
}
}
To register a service as an Eureka client, add the @EnableEurekaClient
annotation to your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.client.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
Add the necessary dependencies to your pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
To register a service with Zookeeper, add the following configuration to your application.properties
:
spring.cloud.zookeeper.connect-string=localhost:2181
spring.application.name=my - service
And annotate your main application class with @EnableDiscoveryClient
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MyZookeeperServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyZookeeperServiceApplication.class, args);
}
}
A large e - commerce application uses Eureka for service discovery. The application has hundreds of microservices, and Eureka’s simplicity and resilience have allowed for easy management of the service registry. The self - protection mechanism has proven useful during network partitions, preventing the accidental removal of critical service instances.
A distributed analytics system uses Zookeeper for service discovery. Since the system requires strong consistency in the configuration and service discovery, Zookeeper’s ability to provide such consistency has been beneficial. The hierarchical namespace also allows for easy organization of the different service instances.
Both Eureka and Zookeeper are powerful tools for service discovery in Spring Cloud. Eureka is a great choice when simplicity, resilience, and low latency are the main requirements. On the other hand, Zookeeper is more suitable when strong consistency is needed. When architecting a Java application, developers should carefully consider the specific requirements of their application and choose the appropriate service discovery option accordingly.