在上一篇文章中,我們瞭解了 Spring Cloud Gateway 作爲網關所具備的基礎功能:路由。本篇我們將關注它的另一個功能:過濾器。
Spring Cloud Gateway 已經內置了很多實用的過濾器,但並不能完全滿足我們的需求。本文我們就來實現自定義過濾器。雖然現在 Spring Cloud Gateway 的文檔還不完善,但是我們依舊可以照貓畫虎來定製自己的過濾器。
Filter 的作用
其實前邊在介紹 Zuul 的的時候已經介紹過 Zuul 的 Filter 的作用了,同作爲網關服務,Spring Cloud Gateway 的 Filter 作用也類似。
這裏就簡單用兩張圖來解釋一下吧。
當使用微服務構建整個 API 服務時,一般有許多不同的應用在運行,如上圖所示的mst-user-service
、mst-good-service
和mst-order-service
,這些服務都需要對客戶端的請求的進行 Authentication。最簡單粗暴的方法就是像上圖一樣,爲每個微服務應用都實現一套用於校驗的過濾器或攔截器。
對於這樣的問題,更好的做法是通過前置的網關服務來完成這些非業務性質的校驗,就像下圖
Filter 的生命週期
Spring Cloud Gateway 的 Filter 的生命週期不像 Zuul 的那麼豐富,它只有兩個:“pre” 和 “post”。
“pre”和 “post” 分別會在請求被執行前調用和被執行後調用,和 Zuul Filter 或 Spring Interceptor 中相關生命週期類似,但在形式上有些不一樣。
Zuul 的 Filter 是通過filterType()
方法來指定,一個 Filter 只能對應一種類型,要麼是 “pre” 要麼是“post”。Spring Interceptor 是通過重寫HandlerInterceptor
中的三個方法來實現的。而 Spring Cloud Gateway 基於 Project Reactor 和 WebFlux,採用響應式編程風格,打開它的 Filter 的接口GatewayFilter
你會發現它只有一個方法filter
。
僅通過這一個方法,怎麼來區分是 “pre” 還是 “post” 呢?我們下邊就通過自定義過濾器來看看。
自定義過濾器
現在假設我們要統計某個服務的響應時間,我們可以在代碼中
long beginTime = System.currentTimeMillis();
// do something...
long elapsed = System.currentTimeMillis() - beginTime;
log.info("elapsed: {}ms", elapsed);
自定義過濾器需要實現GatewayFilter
和Ordered
。其中GatewayFilter
中的這個方法就是用來實現你的自定義的邏輯的每次都要這麼寫是不是很煩?Spring 告訴我們有個東西叫 AOP。但是我們是微服務啊,在每個服務裏都寫也很煩。這時候就該網關的過濾器登臺表演了。
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
好了,讓我們來擼代碼吧而Ordered
中的int getOrder()
方法是來給過濾器設定優先級別的,值越大則優先級越低。
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
public class ElapsedFilter implements GatewayFilter, Ordered {
private static final Log log = LogFactory.getLog(GatewayFilter.class);
private static final String ELAPSED_TIME_BEGIN = "elapsedTimeBegin";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
exchange.getAttributes().put(ELAPSED_TIME_BEGIN, System.currentTimeMillis());
return chain.filter(exchange).then(
Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(ELAPSED_TIME_BEGIN);
if (startTime != null) {
log.info(exchange.getRequest().getURI().getRawPath() + ": " + (System.currentTimeMillis() - startTime) + "ms");
}
})
);
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
我們在請求剛剛到達時,往ServerWebExchange
中放入了一個屬性elapsedTimeBegin
,屬性值爲當時的毫秒級時間戳。然後在請求執行結束後,又從中取出我們之前放進去的那個時間戳,與當前時間的差值即爲該請求的耗時。因爲這是與業務無關的日誌所以將Ordered
設爲Integer.MAX_VALUE
以降低優先級。
現在再來看我們之前的問題:怎麼來區分是 “pre” 還是 “post” 呢?其實就是chain.filter(exchange)
之前的就是 “pre” 部分,之後的也就是then
裏邊的是 “post” 部分。
創建好 Filter 之後我們將它添加到我們的 Filter Chain 裏邊
@Bean
public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
// @formatter:off
return builder.routes()
.route(r -> r.path("/fluent/customer/**")
.filters(f -> f.stripPrefix(2)
.filter(new ElapsedFilter())
.addResponseHeader("X-Response-Default-Foo", "Default-Bar"))
.uri("lb://CONSUMER")
.order(0)
.id("fluent_customer_service")
)
.build();
// @formatter:on
}
現在再嘗試訪問 http://localhost:10000/customer/hello/windmt 即可在控制檯裏看到請求路徑與對應的耗時
2018-05-08 16:07:04.197 INFO 83726 --- [ctor-http-nio-4] o.s.cloud.gateway.filter.GatewayFilter : /hello/windmt: 40ms
實際在使用 Spring Cloud 的過程中,我們會使用 Sleuth+Zipkin 來進行耗時分析。
自定義全局過濾器
前邊講了自定義的過濾器,那個過濾器只是局部的,如果我們有多個路由就需要一個一個來配置,並不能通過像下面這樣來實現全局有效(也未在 Fluent Java API 中找到能設置 defaultFilters 的方法)
@Bean
public ElapsedFilter elapsedFilter(){
return new ElapsedFilter();
}
這在我們要全局統一處理某些業務的時候就顯得比較麻煩,比如像最開始我們說的要做身份校驗,有沒有簡單的方法呢?這時候就該全局過濾器出場了。
有了前邊的基礎,我們創建全局過濾器就簡單多了。只需要把實現的接口GatewayFilter
換成GlobalFilter
,就完事大吉了。比如下面的 Demo 就是從請求參數中獲取token
字段,如果能獲取到就 pass,獲取不到就直接返回401
錯誤,雖然簡單,但足以說明問題了。
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
public class TokenFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String token = exchange.getRequest().getQueryParams().getFirst("token");
if (token == null || token.isEmpty()) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -100;
}
}
然後在 Spring Config 中配置這個 Bean
@Bean
public TokenFilter tokenFilter(){
return new TokenFilter();
}
重啓應用就能看到效果了
2018-05-08 20:41:06.528 DEBUG 87751 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://localhost:10000/customer/hello/windmt?token=1000] to Route{id='service_customer', uri=lb://CONSUMER, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$334/1871259950@2aa090be, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory$$Lambda$337/577037372@22e84be7, order=1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGatewayFilterFactory$$Lambda$339/1061806694@1715f608, order=2}]}
2018-05-08 20:41:06.530 DEBUG 87751 --- [ctor-http-nio-2] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=com.windmt.filter.TokenFilter@309028af}, order=-100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@70e889e9}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory$$Lambda$337/577037372@22e84be7, order=1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGatewayFilterFactory$$Lambda$339/1061806694@1715f608, order=2}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@51351f28}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@724c5cbe}, order=10100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@418c020b}, order=2147483637}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@15f2eda3}, order=2147483646}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@70101687}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@21618fa7}, order=2147483647}]
官方說,未來的版本將對這個接口作出一些調整:
This interface and usage are subject to change in future milestones.
from Spring Cloud Gateway - Global Filters
自定義過濾器工廠
如果你還對上一篇關於路由的文章有印象,你應該還得我們在配置中有這麼一段
filters:
- StripPrefix=1
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
StripPrefix
、AddResponseHeader
這兩個實際上是兩個過濾器工廠(GatewayFilterFactory),用這種配置的方式更靈活方便。
我們就將之前的那個ElapsedFilter
改造一下,讓它能接收一個boolean
類型的參數,來決定是否將請求參數也打印出來。
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
public class ElapsedGatewayFilterFactory extends AbstractGatewayFilterFactory<ElapsedGatewayFilterFactory.Config> {
private static final Log log = LogFactory.getLog(GatewayFilter.class);
private static final String ELAPSED_TIME_BEGIN = "elapsedTimeBegin";
private static final String KEY = "withParams";
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList(KEY);
}
public ElapsedGatewayFilterFactory() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
exchange.getAttributes().put(ELAPSED_TIME_BEGIN, System.currentTimeMillis());
return chain.filter(exchange).then(
Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(ELAPSED_TIME_BEGIN);
if (startTime != null) {
StringBuilder sb = new StringBuilder(exchange.getRequest().getURI().getRawPath())
.append(": ")
.append(System.currentTimeMillis() - startTime)
.append("ms");
if (config.isWithParams()) {
sb.append(" params:").append(exchange.getRequest().getQueryParams());
}
log.info(sb.toString());
}
})
);
};
}
public static class Config {
private boolean withParams;
public boolean isWithParams() {
return withParams;
}
public void setWithParams(boolean withParams) {
this.withParams = withParams;
}
}
}
過濾器工廠的頂級接口是GatewayFilterFactory
,我們可以直接繼承它的兩個抽象類來簡化開發AbstractGatewayFilterFactory
和AbstractNameValueGatewayFilterFactory
,這兩個抽象類的區別就是前者接收一個參數(像StripPrefix
和我們創建的這種),後者接收兩個參數(像AddResponseHeader
)。
GatewayFilter apply(Config config)
方法內部實際上是創建了一個GatewayFilter
的匿名類,具體實現和之前的幾乎一樣,就不解釋了。
靜態內部類Config
就是爲了接收那個boolean
類型的參數服務的,裏邊的變量名可以隨意寫,但是要重寫List<String> shortcutFieldOrder()
這個方法。
這裏注意一下,一定要調用一下父類的構造器把Config
類型傳過去,否則會報ClassCastException
public ElapsedGatewayFilterFactory() {
super(Config.class);
}
工廠類我們有了,再把它註冊到 Spring 當中
@Bean
public ElapsedGatewayFilterFactory elapsedGatewayFilterFactory() {
return new ElapsedGatewayFilterFactory();
}
然後添加配置(主要改動在第 8 行)
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
default-filters:
- Elapsed=true
routes:
- id: service_customer
uri: lb://CONSUMER
order: 0
predicates:
- Path=/customer/**
filters:
- StripPrefix=1
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
然後我們再次訪問 http://localhost:10000/customer/hello/windmt?token=1000 即可在控制檯看到以下內容
2018-05-08 16:53:02.030 INFO 84423 --- [ctor-http-nio-1] o.s.cloud.gateway.filter.GatewayFilter : /hello/windmt: 656ms params:{token=[1000]}
總結
本文主要介紹了 Spring Cloud Gateway 的過濾器,我們實現了自定義局部過濾器、自定義全局過濾器和自定義過濾器工廠,相信大家對 Spring Cloud Gateway 的過濾器有了一定的瞭解。之後我們將繼續在過濾器的基礎上研究 如何使用 Spring Cloud Gateway 實現限流和 fallback。
示例代碼可以從 Github 獲取:https://github.com/zhaoyibo/spring-cloud-study
參考
spring-cloud/spring-cloud-gateway
Spring Cloud Gateway
- 本文作者: Yibo
- 本文鏈接: https://windmt.com/2018/05/08/spring-cloud-14-spring-cloud-gateway-filter/
- 版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明出處!