最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring cloud zuul 與 sentinel的結合使用操作

 更新時間:2021年06月25日 10:53:03   作者:mjlfto  
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結合使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

spring cloud zuul 與 sentinel結合

本來大型服務處理請求超時,限流,降級熔斷工作用hystrix,但是這個這個項目不再更新了,雖說它現(xiàn)在提供的版本不會影響到大多數(shù)開發(fā)者的使用,但是長遠考慮,被更換是一件必然的事,而且現(xiàn)在像resilience4j, Sentinel這樣的替代品出現(xiàn),今天我們就看看使用zuul 與 Sentinel整合,實現(xiàn)降級與超時處理,其實網(wǎng)上有很多這樣的教程,這里我只是做一個自己的筆記而已

1、必須的依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-zuul-adapter</artifactId>
            <version>1.7.1</version>
        </dependency>

2、配置文件,其實Sentinel在這里沒什么配置

server:
  port: 6001
spring:
  application:
    name: e-zuul

eureka:
  instance:
    hostname: localhost
    lease-expiration-duration-in-seconds: 90 #表示服務端多長時間沒有接受到心跳信息后可以刪除自己
    lease-renewal-interval-in-seconds: 30 #表示需要要向服務端發(fā)送信息,表示自己還活著
    ip-address: true
  client:
    healthcheck:
      enabled: true #客戶端心跳檢測
    service-url:
      defaultZone: http://${eureka.instance.hostname}:3001/eureka/

zuul:
  add-proxy-headers: true
  LogFilter:
    pre:
      disable=true:
  routes:
    e-email:
      serviceId: e-email
      path: /email/**
    e-user:
      serviceId: e-user
      path: /user/**

3、配置類, 其實配置類和后邊的降級回調(diào)處理類才是關鍵

而且配置類中幾個關于zuul與Sentinel的過濾器非常關鍵,這里要是不提供它們,將無法實現(xiàn)我們想要的功能,還有就是網(wǎng)關規(guī)則,可以選擇qps, 超時,線程等,setGrade(RuleConstant.DEGRADE_GRADE_RT)提供選擇不同的策略

package com.mjlf.ezuul.config;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.netflix.zuul.ZuulFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class ZuulConfig {
    @Bean
    public ZuulFilter sentinelZuulPreFilter() {
        // We can also provider the filter order in the constructor.
        return new SentinelZuulPreFilter();
    }

    @Bean
    public ZuulFilter sentinelZuulPostFilter() {
        return new SentinelZuulPostFilter();
    }

    @Bean
    public ZuulFilter sentinelZuulErrorFilter() {
        return new SentinelZuulErrorFilter();
    }

    @PostConstruct
    public void doInit() {
        // 注冊 FallbackProvider
        ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
        initGatewayRules();
    }

    /**
     * 配置限流規(guī)則
     */
    private void initGatewayRules() {
        Set<GatewayFlowRule> rules = new HashSet<>();
        rules.add(new GatewayFlowRule("e-user").setCount(3) // 限流閾值
                .setIntervalSec(1) // 統(tǒng)計時間窗口,單位是秒,默認是 1 秒
        );
        rules.add(new GatewayFlowRule("e-user")
                .setGrade(RuleConstant.DEGRADE_GRADE_RT)//設置超時類型規(guī)則
                .setMaxQueueingTimeoutMs(500)
        );
        GatewayRuleManager.loadRules(rules);
    }
}

4、回調(diào)處理類,當有請求被攔截到后,就會調(diào)用降級回調(diào)方法

// 自定義 FallbackProvider
@Component
public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
    private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class);
    // you can define route as service level 
    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public BlockResponse fallbackResponse(String route, Throwable cause) {
        RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route));
        if (cause instanceof BlockException) {
            return new BlockResponse(429, "Sentinel block exception", route);
        } else {
            return new BlockResponse(500, "System Error", route);
        }
    }
}

zuul集成Sentinel最新的網(wǎng)關流控組件

一、說明

Sentinel 網(wǎng)關流控支持針對不同的路由和自定義的 API 分組進行流控,支持針對請求屬性(如 URL 參數(shù),Client IP,Header 等)進行流控。

Sentinel 1.6.3 引入了網(wǎng)關流控控制臺的支持,用戶可以直接在 Sentinel 控制臺上查看 API Gateway 實時的 route 和自定義 API 分組監(jiān)控,管理網(wǎng)關規(guī)則和 API 分組配置。

二、功能接入

1. 網(wǎng)關添加sentinel相關的jar依賴

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

2. 網(wǎng)關zuul的sentinel配置

spring:
  # sentinel動態(tài)配置規(guī)則
  cloud:
    sentinel:
      zuul:
        enabled: true
        order:
          pre: 2000
          post: 500
          error: -100
      filter:
        enabled: false
      datasource:
        # 限流
        ds1:
          nacos:
            server-addr: ${zlt.nacos.server-addr}
            dataId: ${spring.application.name}-sentinel-gw-flow
            groupId: DEFAULT_GROUP
            rule-type: gw-flow
        # api分組
        ds2:
          nacos:
            server-addr: ${zlt.nacos.server-addr}
            dataId: ${spring.application.name}-sentinel-gw-api-group
            groupId: DEFAULT_GROUP
            rule-type: gw-api-group

綁定gw-flow(限流)和gw-api-group(api分組)的規(guī)則數(shù)據(jù)源為nacos
并指定nacos上對應的dataId和groupId

3. nacos規(guī)則配置

3.1. 限流配置gw-flow

在這里插入圖片描述

Data ID:api-gateway-sentinel-gw-flow

Group:DEFAULT_GROUP

配置內(nèi)容:

[
  {
    "resource": "user",
    "count": 0,
    "paramItem": {
      "parseStrategy": 3,
      "fieldName": "name"
    }
  },
  {
    "resource": "uaa_api",
    "count": 0
  }
]

規(guī)則1:所有user的請求只要參數(shù)帶有name的都攔截(qps=0),user為zuul路由配置上的routeId
規(guī)則2:api分組為uaa_api的所有請求都攔截(qps=0)

3.2. api分組配置gw-api-group

在這里插入圖片描述

Data ID:api-gateway-sentinel-gw-api-group

Group:DEFAULT_GROUP

配置內(nèi)容:

[
  {
    "apiName": "uaa_api",
    "predicateItems": [
      {
        "pattern": "/user/login"
      },
      {
        "pattern": "/api-uaa/oauth/**",
        "matchStrategy": 1
      }
    ]
  }
]

上面配置意思為滿足規(guī)則的api都統(tǒng)一分組為uaa_api
分組規(guī)則1:精準匹配/user/login
分組規(guī)則2:前綴匹配/api-uaa/oauth/**

4. 網(wǎng)關zuul啟動參數(shù)

需要在接入端原有啟動參數(shù)的基礎上添加-Dcsp.sentinel.app.type=1啟動以將您的服務標記為 API Gateway,在接入控制臺時您的服務會自動注冊為網(wǎng)關類型,然后您即可在控制臺配置網(wǎng)關規(guī)則和 API 分組,例如:

java -Dcsp.sentinel.app.type=1 -jar zuul-gateway.jar

三、sentinel控制臺管理

API管理(分組)

網(wǎng)關流控規(guī)則

四、測試限流api

1. 測試限流規(guī)則1

所有user的請求只要參數(shù)帶有name的都攔截(qps=0)

不加name參數(shù),可以訪問api

后面加上name參數(shù),請求被攔截

在這里插入圖片描述

2. 測試限流規(guī)則2

api分組為uaa_api的所有請求都攔截(qps=0)

前綴匹配/api-uaa/oauth/**

在這里插入圖片描述

精準匹配/user/login

在這里插入圖片描述 

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Mybatis執(zhí)行多條語句/批量更新方式

    Mybatis執(zhí)行多條語句/批量更新方式

    這篇文章主要介紹了Mybatis執(zhí)行多條語句/批量更新方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 關于post請求內(nèi)容無法重復獲取的解決方法

    關于post請求內(nèi)容無法重復獲取的解決方法

    這篇文章主要介紹了關于post請求內(nèi)容無法重復獲取的解決方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • Feign調(diào)用可重試的最佳方案分享

    Feign調(diào)用可重試的最佳方案分享

    通過spring-retry框架集合Feign去實現(xiàn)重試機制,可以為每個調(diào)用實現(xiàn)不同的重試機制,那這究竟是如何做到的呢,本文將為大家詳細講講
    2023-01-01
  • 淺談java中對集合對象list的幾種循環(huán)訪問

    淺談java中對集合對象list的幾種循環(huán)訪問

    下面小編就為大家?guī)硪黄猨ava中對集合對象list的幾種循環(huán)訪問詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Java中的數(shù)組基礎知識學習教程

    Java中的數(shù)組基礎知識學習教程

    這篇文章主要介紹了Java中的數(shù)組基礎知識學習教程,文中同時也整理了Java對數(shù)字類型的支持狀況及Number類中的方法,需要的朋友可以參考下
    2016-02-02
  • Java中notify()和notifyAll()的使用區(qū)別

    Java中notify()和notifyAll()的使用區(qū)別

    本文主要介紹了Java中notify()和notifyAll()的使用區(qū)別,文中通過示例代碼介紹的非常詳細,感興趣的小伙伴們可以參考一下
    2021-06-06
  • JAVA 對象創(chuàng)建與對象克隆

    JAVA 對象創(chuàng)建與對象克隆

    這篇文章主要介紹了JAVA 對象創(chuàng)建與對象克隆,new 創(chuàng)建、反射、克隆、反序列化,克隆它分為深拷貝和淺拷貝,通過調(diào)用對象的 clone方法,進行對象的克隆,下面來看看文章的詳細內(nèi)容吧
    2022-02-02
  • 一起學JAVA基礎之運算符

    一起學JAVA基礎之運算符

    計算機的最基本用途之一就是執(zhí)行數(shù)學運算,作為一門計算機語言,Java也提供了一套豐富的運算符來操縱變量,下面這篇文章主要給大家介紹了關于JAVA基礎之運算符的相關資料,需要的朋友可以參考下
    2022-01-01
  • Java數(shù)據(jù)類型的規(guī)則

    Java數(shù)據(jù)類型的規(guī)則

    這篇文章主要介紹了Java數(shù)據(jù)類型的規(guī)則的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • springboot集成Mybatis的詳細教程

    springboot集成Mybatis的詳細教程

    今天給大家?guī)淼倪€是關于springboot的相關知識,文章圍繞著springboot集成Mybatis的詳細教程展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

武夷山市| 邛崃市| 红安县| 乐东| 虎林市| 闵行区| 罗甸县| 精河县| 资溪县| 江源县| 南雄市| 汤原县| 阿拉善左旗| 石家庄市| 喀什市| 威宁| 宁强县| 江山市| 栾川县| 康定县| 德令哈市| 且末县| 敖汉旗| 赣州市| 白银市| 阜阳市| 正阳县| 辽阳市| 沾化县| 盐池县| 鹤壁市| 延寿县| 政和县| 扎赉特旗| 宜都市| 青州市| 仁布县| 额济纳旗| 舟山市| 昌平区| 汨罗市|