Spring Cloud Gateway (6) Customize the Global Filter
Introduction to the
In the previous five analyses, I have a general understanding of the Filter component of Spring Cloud Gateway. Today, I will practice writing a global filter with statistical request return time
Train of thought to sort out
From reading the official documentation and from reading the previous source code, it is generally known that GlobalFilter needs to inherit from GlobalFilter and Ordered
- GlobalFilter: You need to rewrite the Filter method to implement your own processing logic in it, which is largely exchange manipulation, value assignment, and so on
- Ordered: The getOrder method needs to be overridden to determine the position of the custom filter in the chain, Ordered by numeric size
User-defined duration statistics filter
Here is a simple reference to the writing method of GatewayMetricsFilter. After the trigger of filter chain is completed, no matter failure or success, statistics will be conducted. The filter order is simply after WRITE_RESPONSE_FILTER_ORDER (the order defined with it is subtracted by one). The code looks like this:
/** * Count the duration of a request in the filter chain **@author lw1243925457
*/
public class DurationStatisticsFilter implements GlobalFilter.Ordered {
private static final Log log = LogFactory.getLog(DurationStatisticsFilter.class);
private static final String START_STAMP = "startStamp";
/** * Request response time statistics * 1. Write the timestamp of the request to Exchange * 2. When the filter chain is complete, whether it succeeds or fails, it is considered complete@param exchange the current server exchange
* @param chain provides a way to delegate to the next filter
* @return mono
*/
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.debug("duration statistics filter");
exchange.getAttributes().put(START_STAMP, System.currentTimeMillis());
return chain.filter(exchange)
.doFinally(f -> printDurationTime(exchange));
}
private void printDurationTime(ServerWebExchange exchange) {
long startStamp = exchange.getAttribute(START_STAMP);
long endStamp = System.currentTimeMillis();
log.debug("duration filter time : " + (endStamp - startStamp) + " ms");
}
/** * this is just after NettyWriteResponseFilter@return order
*/
@Override
public int getOrder(a) {
return WRITE_RESPONSE_FILTER_ORDER - 1; }}Copy the code
The main function is configured to run
To configure the bean in the main function, the code looks like this:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p -> p
.path("/")
.filters(f -> f
.addRequestParameter("test"."test")
.addResponseHeader("return"."return")
.retry(retryConsumer)
)
.uri("http://localhost:8082/"))
.build();
}
@Bean
public GlobalFilter durationStatisticsFilter(a) {
return newDurationStatisticsFilter(); }}Copy the code
Pretty simple, that’s about it, run it, browser access, and you’ll see the following output, over
o.s.c.g.sample.DurationStatisticsFilter : duration statistics filter
o.s.c.g.sample.DurationStatisticsFilter : duration filter time : 349 ms
Copy the code
Filter Records related analysis
- Spring Cloud Gateway (a) code pull and run examples
- Spring Cloud Gateway (2) an Http request flow parsing
- Spring Cloud Gateway Filter chain
- Spring Cloud Gateway(4) Request retry mechanism
- Spring Cloud Gateway(v) Traffic limiting
- Spring Cloud Gateway (6) Customize the Global Filter