1. Introduction to Eureka
Eureka is a service discovery solution open-sourced by Netflix, forming part of the Netflix OSS suite. It is commonly used in microservice architectures, with its core function being service registration and discovery.
When a microservice starts up, it registers its network address (e.g., IP and port) with a Eureka server, known as the Eureka Server (service registry). Other services (clients), upon startup, retrieve the list of running services from the Eureka Server, enabling inter-service communication.
Eureka consists of two primary components:
- Eureka Server: The provider of service registration functionality. It offers a dashboard displaying currently registered service information.
- Eureka Client: Both service consumers and providers. When a service starts, the client registers the service's information with the Eureka Server. Service consumers can then discover service providers through the Eureka Server.
2. Key Features of Eureka
- AP System: Accoridng to the CAP theorem, Eureka is a classic AP (Availability, Partition Tolerance) system. It guarantees high availability and partition tolerance but does not ensure consistency—at any given point, some instance information might be out of sync.
- Self-Preservation Mode: To handle network anmoalies, the Eureka Server can enter a self-preservation mode. In this mode, even if heartbeats from certain microservices are not received, those instances are not immediately removed from the service list.
- REST-based Inter-service Communication: The Eureka Server provides REST APIs, facilitating communication between services written in different languages.
3. Usage Guide
3.1 Setting Up a Eureka Server
- Add Dependency: If using Spring Boot, add the
spring-cloud-starter-netflix-eureka-serverdependency to yourbuild.gradleorpom.xml.
<!-- Dependency in pom.xml -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- Enable Eureka Server: Annotate your Spring Boot application's main class with
@EnableEurekaServer.
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- Configure Application Properties: Set Eureka Server-related configurations in
application.propertiesorapplication.yml.
# application.yml
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.2 Setting Up a Eureka Client
- Add Dependency: Include the
spring-cloud-starter-netflix-eureka-clientdependency in the client service'sbuild.gradleorpom.xml.
<!-- Dependency in pom.xml -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Enable Eureka Client: Annotate your Spring Boot application's main class with
@EnableEurekaClientor@EnableDiscoveryClient.
@SpringBootApplication
@EnableEurekaClient // or @EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
- Configure Application Properties: Configure Eureka Client settings in
application.propertiesorapplication.yml, such as the Eureka Server URL for registration.
# application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3.3 High Availability Configuration for Eureka Server
In a production environment, the Eureka Server is often deployed as a high-availability cluster. The basic approach is to set up multiple Eureka Server instances that register with each other as services (i.e., they act as clients to one another). This ensures that if one instance fails, others can continue providing service registration and discovery functionality.
The above steps cover the fundamental concepts and setup for Eureka Server and Client. Detailed configuration and usage may vary based on specific project requiremants.