欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程詳解

 更新時間:2023年02月10日 08:45:46   作者:楊宸楊  
這篇文章主要介紹了Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程,首先Sentinel規(guī)則的存儲默認(rèn)是存儲在內(nèi)存的,應(yīng)用重啟之后規(guī)則會丟失。因此我們通過配置中心Nacos保存規(guī)則,然后通過定時拉取Nacos數(shù)據(jù)來獲取規(guī)則配置,可以做到動態(tài)實(shí)時的刷新規(guī)則

Gateway-過濾器Filter

過濾器就是在請求的傳遞過程中,對請求和響應(yīng)做一些手腳.

在Gateway中, Filter的生命周期只有兩個:“pre”和“post”"。

.PRE:這種過濾器在請求被路由之前調(diào)用。我們可利用這種過濾器實(shí)現(xiàn)身份驗(yàn)證、在集群中選擇請求的微服務(wù)、記錄調(diào)試信息等。

.POST:這種過濾器在路由到微服務(wù)以后執(zhí)行。這種過濾器可用來為響應(yīng)添加標(biāo)準(zhǔn)的HTTPHeader、收集統(tǒng)計信息和指標(biāo)、將響應(yīng)從微服務(wù)發(fā)送給客戶端等。

在Gateway中,F(xiàn)ilter的作用范圍兩種:

.GatewayFilter:應(yīng)用到單個路由或者一個分組的路由上。

.GlobalFilter:應(yīng)用到所有的路由上

局部路由過濾器

第一步:編寫配置文件

第二步:創(chuàng)建局部過濾器類

@Component
    public class TimeGatewayFilterFactory extends AbstractGatewayFilterFactory<TimeGatewayFilterFactory.Config> {
        //解析參數(shù)
        public TimeGatewayFilterFactory(){
            super(Config.class);
        }
        //讀取配置文件中的參數(shù)  賦值到  配置類中
        public List<String> shortcutFieldOrder(){
            return Arrays.asList("show");
        }
        //攔截到之后就會調(diào)用apply方法,把創(chuàng)建對象時候反射創(chuàng)建出來的config傳入進(jìn)來
        @Override
        public GatewayFilter apply(Config config) {
            return new GatewayFilter() {
                @Override
                public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                    //前置的邏輯
                    System.out.println("前置邏輯");
                    return chain.filter(exchange).then(Mono.fromRunnable(()->{
                        //后置的邏輯
                        System.out.println("后置邏輯");
                    }));
                }
            };
        }
        //將解析好的參數(shù)注入其中
        @Setter
            @Getter
            static class Config{
                private boolean show;
            }
    }

使用局部過濾器

假設(shè)我們給商品類創(chuàng)建一個局部過濾器,當(dāng)傳入?yún)?shù)為true的時候,控制臺內(nèi)返回網(wǎng)關(guān)轉(zhuǎn)發(fā)到服務(wù)的時間

創(chuàng)建一個過濾器類:

@Component
    public class TimeGatewayFilterFactory extends AbstractGatewayFilterFactory<TimeGatewayFilterFactory.Config> {
        //解析參數(shù)
        public TimeGatewayFilterFactory(){
            super(Config.class);
        }
        //讀取配置文件中的參數(shù)  賦值到  配置類中
        public List<String> shortcutFieldOrder(){
            return Arrays.asList("show");
        }
        //攔截到之后就會調(diào)用apply方法,把創(chuàng)建對象時候反射創(chuàng)建出來的config傳入進(jìn)來
        @Override
        public GatewayFilter apply(Config config) {
            return new GatewayFilter() {
                @Override
                public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                    if(!config.show){
                        return chain.filter(exchange);
                    }

                    //前置的邏輯
                    long start = System.currentTimeMillis();
                    return chain.filter(exchange).then(Mono.fromRunnable(()->{
                        System.out.println("請求耗時:"+(System.currentTimeMillis()-start));
                    }));
                }
            };
        }
        //將解析好的參數(shù)注入其中
        @Setter
            @Getter
            static class Config{
                private boolean show;
            }
    }

運(yùn)行結(jié)果:

當(dāng)參數(shù)為true的時候

如果訪問別的模塊,控制臺是不會返回的,這就是局部過濾

當(dāng)參數(shù)為false的時候

全局過濾器

全局過濾器作用于所有路由,無需配置,通過全局過濾器可以實(shí)現(xiàn)對權(quán)限的統(tǒng)一校驗(yàn),安全性驗(yàn)證等功能。

假設(shè)我們現(xiàn)在有一個需求:實(shí)現(xiàn)統(tǒng)一鑒權(quán)的功能,我們需要在網(wǎng)關(guān)判斷請求是否包含token且,如果沒有則不轉(zhuǎn)發(fā)路由,有則正常邏輯。

使用全局過濾器

1.編寫全局過濾類

@Component
public class AuthGlobalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //前置邏輯
        //獲取請求中的token信息,驗(yàn)證token是否有效,如果無效攔截請求,
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if(StringUtils.isEmpty(token)||!"123".equals(token)){
            System.out.println("鑒定失敗");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}

運(yùn)行結(jié)果:

集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流

網(wǎng)關(guān)是所有請求的公共入口,所以可以在網(wǎng)關(guān)進(jìn)行限流,而且限流的方式也很多,我們本次采用前

面學(xué)過的Sentinel組件來實(shí)現(xiàn)網(wǎng)關(guān)的限流。Sentinel支持對SpringCloud Gateway、Zuul等主流網(wǎng)關(guān)進(jìn) 行限流。 從1.6.0版本開始,Sentinel提供了SpringCloud Gateway的適配模塊,可以提供兩種資源維度的限流: . route維度:即在spring配置文件中配置的路由條目,資源名為對應(yīng)的routeld ·自定義API維度:用戶可以利用Sentinel提供的API來自定義一些API分組

實(shí)現(xiàn)步驟:

1.添加依賴

<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

2.添加配置文件

cloud:
  sentinel:
    transport:
      port: 9999
      dashboard: localhost:8080

運(yùn)行結(jié)果

那么問題來了,如果給網(wǎng)關(guān)限流了,那還有必要給接口限流嗎?

答案是一定的,網(wǎng)關(guān)限流是為了控制通過網(wǎng)關(guān)轉(zhuǎn)發(fā)到各個微服務(wù)的流量,為了防止網(wǎng)關(guān)因?yàn)榱髁窟^大而損壞,但是萬一該微服務(wù)中被其它若干個別的模塊調(diào)用的時候,同樣也會遭受到很大的壓力,容易造成該模塊服務(wù)器的損壞

總結(jié)一句話

網(wǎng)關(guān)限流是為了控制訪問該微服務(wù)的總體流量,但沒有辦法控制訪問該其中特定接口的流量,接口同樣也要增加限流

網(wǎng)關(guān)限流

API分組限流

第一步:創(chuàng)建API分組

創(chuàng)建流控:

到此這篇關(guān)于Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程詳解的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)網(wǎng)關(guān)限流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論