Eureka & API Gateway (MSA)
Eureka & API Gateway (MSA)
Eureka & API Gateway (MSA)
What is Eureka?
• It allows microservices to register themselves and discover other services for inter-service
communication.
1. Service Registration: Each microservice registers itself with Eureka by providing metadata
(like hostname, port, etc.).
2. Service Discovery: Microservices use Eureka to discover the addresses of other services they
need to communicate with.
3. Heartbeat Monitoring: Eureka monitors the health of each registered service through
periodic heartbeat messages.
4. Fault Tolerance: If a service does not send a heartbeat for a set period, Eureka removes it
from the registry.
1. Eureka Server: The central registry where all services are registered.
2. Eureka Clients:
o These are the microservices that register with the Eureka Server.
groovy
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
o You can view all registered services on the Eureka Dashboard (available at
http://localhost:8761).
• Multiple Eureka Servers (e.g., in different regions or zones like us-east-1c, us-east-1d)
replicate data with each other.
• Application Clients register with one of the Eureka Servers and periodically fetch the registry
to communicate with other services.
• Services communicate with each other using the addresses retrieved from the Eureka
registry.
Server Application:
Client Application:
API Gateway in Microservices Architecture
What is an API Gateway?
• An API Gateway acts as a single entry point for all client requests in a microservices system.
• Instead of having clients interact with multiple services directly, the API Gateway routes,
manages, and aggregates requests to the appropriate backend services.
• It simplifies the system for both the client and the developers, making it easier to manage
and scale.
o The client only interacts with one endpoint (API Gateway), rather than multiple
endpoints for each service.
o This makes communication between the frontend (web app, mobile app) and
backend services less complex.
2. Routing Requests:
o Example:
3. Aggregation:
o If the client request requires data from multiple services, the gateway collects
responses from these services and returns a combined response to the client.
o Example: A product detail page might need product information, reviews, and user
details.
4. Transformation:
o The gateway can transform requests and responses (e.g., converting data formats or
adding/removing headers) to ensure smooth communication between clients and
services.
o The API Gateway forwards the request to the correct service (e.g., user accounts,
orders).
o After receiving the responses from services, it sends the final response back to the
client.
1. Path-Based Routing:
o Example:
2. Method-Based Routing:
o Routes requests based on the HTTP method (GET, POST, PUT, DELETE).
o Example:
3. Header-Based Routing:
o Example:
4. Parameter-Based Routing:
o Example:
▪ /search?type=product → Product search service
• Simplifies Client Interaction: Clients only need to know about one endpoint instead of
multiple.
• Centralized Authentication & Authorization: The gateway can enforce security policies (like
authentication) at a single point.
• Fault Handling & Caching: The gateway can manage timeouts, retries, and cache responses
to improve performance.
• Service Versioning: Supports multiple versions of APIs and routes traffic accordingly.
• The client (web app or mobile app) must directly interact with each service, which can be
complex.
2. The API Gateway determines which service should handle each request.
3. If a request needs data from multiple services (like user and product details), the gateway
gathers the data and sends a combined response to the client.
What is Zuul?
• Zuul is an API Gateway developed by Netflix as part of its microservices ecosystem.
• It acts as a reverse proxy and routing layer, directing client requests to the appropriate
backend services.
• Zuul simplifies communication between external clients (like web apps or mobile apps) and
multiple microservices.
1. Routing:
o Routes requests to the correct service based on paths, methods, headers, or query
parameters.
2. Load Balancing:
o Works with Ribbon (another Netflix library) to distribute incoming traffic across
multiple instances of a service.
5. Fault Tolerance:
o Works with Hystrix (Netflix’s circuit breaker) to provide fault tolerance by handling
failed requests gracefully.