-
Notifications
You must be signed in to change notification settings - Fork 59
Zuul Filters
This page will cover several topics about Zuul:
-
Pre filter
- Invocation: Invoked before routing the request to the target service.
- Uses:
- Request authentication.
- Request authorization.
- Make sure all the requests are in the consistent format.
- Log debug info.
-
Post filter
- Invocation: Invoked after the target service has been triggered and a response is being sent back to the client.
- Uses:
- Make sure all the response are in the consistent format.
- Log debug info.
- Collect statistics and metrics.
- Stream the response from the target service to the client.
-
Route filter
- Invocation: Invoked when the request is routing to the target service.
- Uses:
- Define a custom routing logic.
- A/B test.
-
Error filter
- Invocation: Invoke when an error occurs during the execution of other filters.
To define a custom filter, you need to create a class and implement the ZuulFilter
interface.
YourFilter.java
@Component
public class YourFilter implements ZuulFilter{
@Override
public String filterType() {
/* details */
}
@Override
public int filterOrder() {
/* details */
}
@Override
public boolean shouldFilter() {
/* details */
}
@Override
public Object run() {
/* details */
}
}
For the ZuulFilter
interface, there are 4 methods need to be overridden:
Topic | Method | Description |
---|---|---|
Type | public String filterType() |
Define the filter type. |
Execution Order | public int filterOrder() |
Define the order of execution across multiple filters. |
Criteria | public boolean shouldFilter() |
Define the filter will be invoked or not. |
Action | public Object run() |
The action to be executed if the criteria is met. |
The filterType()
method is to classify the type of the filter. There are several types and the corresponding string for the return value:
Type | Return Value |
---|---|
Pre filter | pre |
Post filter | post |
Route filter | route |
Error filter | error |
Static response filter | static |
The filterOrder()
method is to define the order of execution across multiple filters. The return value should be int
and there are several rules applies to it:
- The filter with the smaller filterOrder will be executed first.
- Filters may have the same filterOrder if precedence is not important for a filter.
- filterOrders do not need to be sequential.
The shouldFilter()
method is to define the filter will be invoked or not. Returning true
will be invoked and return false
to avoid to be invoked.
The run()
method is the core method of a filter class and it defines the behavior of the filter if the criteria is met. The return value is optional and it can be null
if nothing needs to be returned.
- Overview
- Getting Started
-
Technical Essentials
- Autowired
- SpringData JPA
- Configuration File Auto-loading
- Configuration Encryption
- Service Discovery with Eureka
- Resiliency Patterns with Hystrix
- Configure Hystrix
- Service Gateway with Zuul
- Zuul Filters
- Protect Service with Spring Security and OAuth2
- Use JWT as Access Token
- Store Clients and Users' Credentials to DB
- Integrate with Message Queue (Kafka)
- Integrate with Redis
- Tune Logging
- Log Aggregation
- Send Trace to Zipkin
- Build Runnable Jar
- Core Application Logic
- Components