Guided Exercise - Spring Microservices - Proxy Pattern | (Extra - Spring Cloud Gateway & Netflix Zuul) #167
akash-coded
started this conversation in
Tasks
Replies: 6 comments 1 reply
|
Please find the proxy implementation part-1 -- Edited -- |
0 replies
0 replies
|
Hi @akash-coded Part 1 - |
1 reply
|
Hi @akash-coded |
0 replies
|
Hi @akash-coded |
0 replies
|
Please find the proxy pattern implementation part-2 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Part-1
Scenario:
Imagine an e-commerce application with the following microservices:
ProductService: Manages product information.OrderService: Manages customer orders.InventoryService: Keeps track of the stock/inventory for each product.Now, the
OrderServiceneeds to fetch product details fromProductServiceand update inventory in theInventoryServiceafter each order.We will use a proxy to isolate direct calls between these services, to decouple them and ensure clean separation of concerns.
1. Concepts:
Proxy Pattern: In the context of microservices, a proxy is an intermediate that acts as both a server and a client for the purpose of making requests on behalf of other clients. It can be used for load balancing, monitoring, logging, security, etc.
Spring Boot Microservices: Microservices are smaller, independent applications that work together. Spring Boot makes it easy to create stand-alone, production-grade applications.
2. Product Service
Repository Structure:
application.properties:
Product.java:
ProductRepository.java:
ProductService.java & ProductController.java:
Provide basic CRUD for managing products.
3. Inventory Service
Follow a similar structure and CRUD for managing inventory.
4. Order Service
Here's where it gets interesting. This service will act as a client for the other two services, either directly or via a proxy.
Direct Calls without Proxy:
Using Proxy:
A proxy can be introduced here to avoid direct calls. This proxy can add additional features like logging, security, etc.
The
OrderControllerwill now useProductServiceProxyinstead of direct calls.5. Best Practices & Additional Tips:
Spring Cloud GatewayorNetflix Zuulas a proxy gateway.Spring Cloud Circuit Breakerfor handling failures.Wrap Up:
By introducing the proxy pattern in the context of microservices, we can achieve separation of concerns, cleaner code, and additional features like logging, security, and more. The above exercise should provide a good starting point for TCS engineers with 2-8 years of experience in Spring Boot.
Part-2
We'll dive into using both Spring Cloud Gateway and Netflix Zuul to further explore the proxy pattern, especially in the context of microservices. We'll also integrate different tasks around the benefits and limitations of the proxy pattern.
7. Introduction to Spring Cloud Gateway & Netflix Zuul
Both Spring Cloud Gateway and Netflix Zuul provide API gateway capabilities. While Zuul was the initial solution provided by Netflix, Spring Cloud Gateway offers a more modern approach and is often considered the successor.
Zuul:
Spring Cloud Gateway:
8. Setting Up Netflix Zuul
Dependencies:
Include the following in the
pom.xml:application.properties:
Here, we're setting up routes that will forward requests to the respective microservices.
Main Application Class:
9. Setting Up Spring Cloud Gateway
Dependencies:
Include these in the
pom.xml:application.properties:
10. Tasks & Scenarios
Task 1 - Third-Party Consumers via Gateway:
Consider you want to expose the
ProductServiceto third-party vendors but with rate-limiting.RequestRateLimiterfilter.Task 2 - Service Aggregation:
Third-party vendors need aggregated data from both
ProductServiceandInventoryService. Create an endpoint in the Gateway that fetches data from both services and returns a combined response.Task 3 - Custom Filters:
Task 4 - Error Handling & Fallback:
Introduce a mechanism in the Gateway to handle errors gracefully, providing default responses if a service is unavailable.
Task 5 - Drawbacks & Limitations:
InventoryServiceis launched. Implement versioning in Gateway and discuss the challenges of managing multiple versions.Detailed Implementation Guidelines for the Tasks
Alright, let's dive deeper into each task, providing a detailed step-by-step guide.
Task 1 - Third-Party Consumers via Gateway
Objective: Expose the
ProductServiceto third-party vendors but limit the rate at which they can access it.Zuul Rate Limiting:
a. Add the required dependencies:
b. In
application.properties:This will limit the
ProductServiceto 10 requests per minute.Spring Cloud Gateway Rate Limiting:
a. Add Redis as a backend for rate limiting:
b. In
application.properties:c. Configure rate limiter in the route definition:
Task 2 - Service Aggregation
Objective: Create a unified API endpoint in the Gateway that fetches data from both
ProductServiceandInventoryServiceand then aggregates the responses.Using Zuul:
Zuul doesn’t inherently support aggregation, so you need to manually combine data.
a. Create an API endpoint
/aggregate-data.b. Within this endpoint, make parallel calls to both
ProductServiceandInventoryService.c. Combine the responses and send it back to the client.
Using Spring Cloud Gateway:
a. Define a new route with a custom URI:
b. In the handler, make WebFlux calls to both services and aggregate using
zipormerge.Task 3 - Custom Filters
Objective: Log details of incoming requests before forwarding.
Zuul Custom Filter:
a. Create a class extending
ZuulFilter.b. Override methods and in
run()method, log the required details.c. Register this filter as a bean.
Spring Cloud Gateway Custom Filter:
a. Implement
GlobalFilterinterface.b. Override the
filtermethod to log details.c. Register as a bean.
Task 4 - Error Handling & Fallback
Objective: Provide a default response if a service is down.
Zuul Fallback:
a. Implement
FallbackProvider.b. Return a
ClientHttpResponsefrom thefallbackResponsemethod.Spring Cloud Gateway Fallback:
a. Add a
SetStatusfilter to the route.b. Configure it to check the status of the downstream service.
c. If the service is down, set an appropriate fallback response.
Task 5 - Drawbacks & Limitations
Objective: Implement unified authentication at the gateway level while keeping authorization at the microservice level. Understand the challenges.
Unified Authentication:
a. Implement an authentication mechanism in the gateway (like JWT token verification).
b. Once authenticated, forward the request to the microservice.
Service-level Authorization:
a. In each microservice, check for the presence of certain roles or permissions before processing the request.
Challenges:
Wrap Up:
Spring Cloud Gateway and Netflix Zuul bring a plethora of benefits to the table. They act as a perfect proxy layer in front of your microservices, providing features like routing, load balancing, and security out of the box. When implemented correctly, they can abstract away many complexities and provide a unified front to consumers, both internal and third-party.
However, as with all tools, they come with their own challenges and learning curves. Proper design considerations are essential to avoid creating a monolithic gateway that becomes a bottleneck or a single point of failure.
All reactions