Chapter 6. Service routing with Spring Cloud and Zuul
In a microservices architecture, the chapter explains that a service gateway is used to centralize cross-cutting concerns such as security, logging, and request tracking instead of forcing every service team to implement them separately. This approach reduces inconsistency, avoids hard dependencies across services, and makes it easier to enforce policy in one place. Zuul, integrated through Spring Cloud, serves as that gateway by acting as a reverse proxy between clients and downstream services, routing all calls through a single entry point.
The chapter then shows how to set up Zuul in a Spring Boot application, connect it to Eureka for service discovery, and configure routes so requests can be mapped automatically or manually. It covers several routing styles: using Eureka service IDs directly, creating custom route mappings, ignoring specific Eureka-generated routes, adding a common prefix to routes, and pointing Zuul to static URLs for services not managed by Eureka. It also highlights how route configuration can be refreshed dynamically from externalized configuration without restarting the gateway, and how timeouts must be managed carefully through Hystrix and Ribbon to keep the gateway responsive.
Much of the chapter focuses on Zuul filters, which let you apply shared behavior before, during, and after request routing. A pre-filter generates or preserves correlation IDs so requests can be traced across services, while supporting classes store request data in thread-local context and propagate headers through outgoing calls. A post-filter sends the correlation ID back to the client in the response. Finally, the chapter demonstrates a route filter that can dynamically redirect traffic for A/B testing by consulting a separate routing service and forwarding some requests to an alternate service instance, showing how Zuul can do far more than static proxying.
Figure 6.1. Without a services gateway, the service client will call distinct endpoints for each service.
Figure 6.2. The service gateway sits between the service client and the corresponding service instances. All service calls (both internal-facing and external) should flow through the service gateway.
Figure 6.3. Zuul will use the organizationservice application name to map requests to organization service instances.
Figure 6.11. The pre-, route, and post filters form a pipeline in which a client request flows through. As a request comes into Zuul, these filters can manipulate the incoming request.
Figure 6.12. Zuul filters provide centralized tracking of service calls, logging, and dynamic routing. Zuul filters allows you to enforce custom rules and policies against microservice calls.
Figure 6.13. A set of common classes are used so that the correlation ID can be propagated to downstream service calls.
Summary
- Spring Cloud makes it trivial to build a services gateway.
- The Zuul services gateway integrates with Netflix’s Eureka server and can automatically map services registered with Eureka to a Zuul route.
- Zuul can prefix all routes being managed, so you can easily prefix your routes with something like /api.
- Using Zuul, you can manually define route mappings. These route mappings are manually defined in the applications configuration files.
- By using Spring Cloud Config server, you can dynamically reload the route mappings without having to restart the Zuul server.
- You can customize Zuul’s Hystrix and Ribbon timeouts at global and individual service levels.
- Zuul allows you to implement custom business logic through Zuul filters. Zuul has three types of filters: pre-, post, and routing Zuul filters.
- Zuul pre-filters can be used to generate a correlation ID that can be injected into every service flowing through Zuul.
- A Zuul post filter can inject a correlation ID into every HTTP service response back to a service client.
- A custom Zuul route filter can perform dynamic routing based on a Eureka service ID to do A/B testing between different versions of the same service.
FAQ
What is a services gateway in a microservices architecture?
A services gateway is an intermediary that sits between service clients and microservices. Clients call a single URL, and the gateway routes the request to the appropriate downstream service instance while also acting as a central policy enforcement point.Why use a services gateway instead of calling microservices directly?
It centralizes cross-cutting concerns like security, logging, and tracking so each service team does not have to implement them separately. It also avoids hard dependencies and inconsistent behavior across services.What is Spring Cloud Zuul?
Zuul is Netflix’s open source services gateway, and Spring Cloud integrates with it to make gateway setup easier. It provides route mapping, filtering, and integration with service discovery like Eureka.How do you enable Zuul in a Spring Boot application?
You add the Spring Cloud Zuul dependency and annotate the main application class with@EnableZuulProxy. That is enough to make the app run as a Zuul proxy server.How does Zuul route requests automatically with Eureka?
If no routes are explicitly configured, Zuul can use Eureka service IDs to automatically map incoming requests to downstream services. For example, a request path like/organizationservice/** can be routed to the service registered with that Eureka ID.How can you define custom routes in Zuul?
You can manually define routes inapplication.yml using the zuul.routes configuration. This lets you map a friendly path like /organization/** to a service such as organizationservice.What does the ignored-services setting do in Zuul?
It excludes specified Eureka service IDs from automatic route registration. You can use it to hide auto-generated routes or disable all Eureka-based mappings by setting it to *.How does Zuul support correlation IDs for tracking requests?
Zuul can use a pre-filter to check for atmx-correlation-id header and generate one if it is missing. A post filter can then add that same correlation ID back to the response headers so the client can track the request end to end.