全局過濾器作用于所有的路由,不需要單獨(dú)配置,我們可以用它來實(shí)現(xiàn)很多統(tǒng)一化處理的業(yè)務(wù)需求,比如權(quán)限認(rèn)證,ip訪問限制等等。
接口定義類:org.springframework.cloud.gateway.filter.globalfilter
1
2
3
|
public interface globalfilter { mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain); } |
gateway自帶的globalfilter實(shí)現(xiàn)類有很多,如下圖:
有轉(zhuǎn)發(fā),路由,負(fù)載等相關(guān)的globalfilter,感興趣的可以自己去看下源碼,了解下。
我們自己如何定義globalfilter來實(shí)現(xiàn)我們自己的業(yè)務(wù)邏輯?
給出一個(gè)官方文檔上的案例:
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個(gè)globalfilter,通過@order來指定執(zhí)行的順序,數(shù)字越小,優(yōu)先級(jí)越高。下面就是輸出的日志,從日志就可以看出執(zhí)行的順序:
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
當(dāng)globalfilter的邏輯比較多時(shí),我還是推薦大家單獨(dú)寫一個(gè)globalfilter來處理,比如我們要實(shí)現(xiàn)對ip的訪問限制,不在ip白名單中就不讓調(diào)用的需求。
單獨(dú)定義只需要實(shí)現(xiàn)globalfilter, ordered這兩個(gè)接口就可以了。
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(); // 此處寫死了,演示用,實(shí)際中需要采取配置的方式 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); } // 這邊從請求頭中獲取用戶的實(shí)際ip,根據(jù)nginx轉(zhuǎn)發(fā)的請求頭獲取 private string getip(httpheaders headers) { return "127.0.0.1" ; } } |
過濾的使用沒什么好講的,都比較簡單,作用卻很大,可以處理很多需求,上面講的ip認(rèn)證攔截只是冰山一角,更多的功能需要我們自己基于過濾器去實(shí)現(xiàn)。
比如我想做a/b測試,那么就得在路由轉(zhuǎn)發(fā)層面做文章,前面我們有貼一個(gè)圖片,圖片中有很多默認(rèn)的全局過濾器,其中有一個(gè)loadbalancerclientfilter是負(fù)責(zé)選擇路由服務(wù)的負(fù)載過濾器,里面會(huì)通過loadbalancer去選擇轉(zhuǎn)發(fā)的服務(wù),然后傳遞到下面的路由nettyroutingfilter過濾器去執(zhí)行,那么我們就可以基于這個(gè)機(jī)制來實(shí)現(xiàn)。
filter中往下一個(gè)filter中傳遞數(shù)據(jù)實(shí)用下面的方式:
1
|
exchange.getattributes().put(gateway_request_url_attr, requesturl); |
獲取方直接獲?。?/p>
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,這樣就能在它執(zhí)行完之后來替換要路由的地址了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000018402335