全局過濾器作用于所有的路由,不需要單獨配置,我們可以用它來實現很多統一化處理的業務需求,比如權限認證,ip訪問限制等等。
接口定義類:org.springframework.cloud.gateway.filter.globalfilter
1
2
3
|
public interface globalfilter { mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain); } |
gateway自帶的globalfilter實現類有很多,如下圖:
有轉發,路由,負載等相關的globalfilter,感興趣的可以自己去看下源碼,了解下。
我們自己如何定義globalfilter來實現我們自己的業務邏輯?
給出一個官方文檔上的案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@configuration public class exampleconfiguration { private logger log = loggerfactory.getlogger(exampleconfiguration. class ); @bean @order (- 1 ) public globalfilter a() { return (exchange, chain) -> { log.info( "first pre filter" ); return chain.filter(exchange).then(mono.fromrunnable(() -> { log.info( "third post filter" ); })); }; } @bean @order ( 0 ) public globalfilter b() { return (exchange, chain) -> { log.info( "second pre filter" ); return chain.filter(exchange).then(mono.fromrunnable(() -> { log.info( "second post filter" ); })); }; } @bean @order ( 1 ) public globalfilter c() { return (exchange, chain) -> { log.info( "third pre filter" ); return chain.filter(exchange).then(mono.fromrunnable(() -> { log.info( "first post filter" ); })); }; } } |
上面定義了3個globalfilter,通過@order來指定執行的順序,數字越小,優先級越高。下面就是輸出的日志,從日志就可以看出執行的順序:
2018-10-14 12:08:52.406 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : first pre filter
2018-10-14 12:08:52.406 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : second pre filter
2018-10-14 12:08:52.407 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : third pre filter
2018-10-14 12:08:52.437 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : first post filter
2018-10-14 12:08:52.438 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : second post filter
2018-10-14 12:08:52.438 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : third post filter
當globalfilter的邏輯比較多時,我還是推薦大家單獨寫一個globalfilter來處理,比如我們要實現對ip的訪問限制,不在ip白名單中就不讓調用的需求。
單獨定義只需要實現globalfilter, ordered這兩個接口就可以了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@component public class ipcheckfilter implements globalfilter, ordered { @override public int getorder() { return 0 ; } @override public mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain) { httpheaders headers = exchange.getrequest().getheaders(); // 此處寫死了,演示用,實際中需要采取配置的方式 if (getip(headers).equals( "127.0.0.1" )) { serverhttpresponse response = exchange.getresponse(); responsedata data = new responsedata(); data.setcode( 401 ); data.setmessage( "非法請求" ); byte [] datas = jsonutils.tojson(data).getbytes(standardcharsets.utf_8); databuffer buffer = response.bufferfactory().wrap(datas); response.setstatuscode(httpstatus.unauthorized); response.getheaders().add( "content-type" , "application/json;charset=utf-8" ); return response.writewith(mono.just(buffer)); } return chain.filter(exchange); } // 這邊從請求頭中獲取用戶的實際ip,根據nginx轉發的請求頭獲取 private string getip(httpheaders headers) { return "127.0.0.1" ; } } |
過濾的使用沒什么好講的,都比較簡單,作用卻很大,可以處理很多需求,上面講的ip認證攔截只是冰山一角,更多的功能需要我們自己基于過濾器去實現。
比如我想做a/b測試,那么就得在路由轉發層面做文章,前面我們有貼一個圖片,圖片中有很多默認的全局過濾器,其中有一個loadbalancerclientfilter是負責選擇路由服務的負載過濾器,里面會通過loadbalancer去選擇轉發的服務,然后傳遞到下面的路由nettyroutingfilter過濾器去執行,那么我們就可以基于這個機制來實現。
filter中往下一個filter中傳遞數據實用下面的方式:
1
|
exchange.getattributes().put(gateway_request_url_attr, requesturl); |
獲取方直接獲取:
1
|
uri requesturl = exchange.getrequiredattribute(gateway_request_url_attr); |
如果我想改變路由的話,就可以這樣做:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@component public class debugfilter implements globalfilter, ordered { @override public int getorder() { return 10101 ; } @override public mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain) { try { exchange.getattributes().put(gateway_request_url_attr, new uri( "http://192.168.31.245:8081/house/hello2" )); } catch (urisyntaxexception e) { e.printstacktrace(); } return chain.filter(exchange); } } |
loadbalancerclientfilter的order是10100,我們這邊比它大1,這樣就能在它執行完之后來替換要路由的地址了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000018402335