一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring cloud gateway 全局過濾器的實現

spring cloud gateway 全局過濾器的實現

2021-07-19 09:08猿天地 Java教程

全局過濾器作用于所有的路由,不需要單獨配置,我們可以用它來實現很多統一化處理的業務需求,這篇文章主要介紹了spring cloud gateway 全局過濾器的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編

全局過濾器作用于所有的路由,不需要單獨配置,我們可以用它來實現很多統一化處理的業務需求,比如權限認證,ip訪問限制等等。

接口定義類:org.springframework.cloud.gateway.filter.globalfilter

?
1
2
3
public interface globalfilter {
  mono<void> filter(serverwebexchange exchange, gatewayfilterchain chain);
}

gateway自帶的globalfilter實現類有很多,如下圖:

spring cloud gateway 全局過濾器的實現

有轉發,路由,負載等相關的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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产无线码在线观看 | 国产成人久久精品区一区二区 | 热99精品| 肉肉小说在线阅读 | 含羞草传媒每天免费一次破解 | 亚洲视频一区二区在线观看 | 免费日批 | 无遮挡h肉动漫高清在线 | 啪啪免费网址 | 美女的让男生桶 | 亚洲色图中文字幕 | 牛牛影院成人免费网页 | 91大神精品 | 91最新国产| 婷婷综合缴情亚洲五月伊 | 精品国产区| 日本免费精品视频 | 亚洲免费精品视频 | 16男男gaygays | 国产成人综合精品 | 19+韩国女主播激情vip视频在线 | ffee性xxⅹ另类老妇hd | 日韩在线免费播放 | yellow最新视频2019 | 边吃胸边膜下刺激免费男对女 | 图片一区 | 国产高清精品自在久久 | 欧美日韩精品免费一区二区三区 | 国产成人v爽在线免播放观看 | nxgx欧美| 男人看的网址 | free性丰满hd性欧美厨房 | www.麻豆| pppd在线播放 | 精品久久伦理中文字幕 | 成人欧美一区二区三区黑人 | 天天久久影视色香综合网 | chinese真实incest| 女同志 videos| 久久sese | 国产青草亚洲香蕉精品久久 |