• zuul集成Sentinel最新的网关流控组件


    一、说明

    Sentinel 网关流控支持针对不同的路由和自定义的 API 分组进行流控,支持针对请求属性(如 URL 参数,Client IP,Header 等)进行流控。Sentinel 1.6.3 引入了网关流控控制台的支持,用户可以直接在 Sentinel 控制台上查看 API Gateway 实时的 route 和自定义 API 分组监控,管理网关规则和 API 分组配置。
    file

     

    二、功能接入

    1. 网关添加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. 网关zuul的sentinel配置

    spring:
      # sentinel动态配置规则
      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分组)的规则数据源为nacos
    并指定nacos上对应的dataIdgroupId

     

    3. nacos规则配置

    3.1. 限流配置gw-flow

    file

    • Data ID:api-gateway-sentinel-gw-flow
    • Group:DEFAULT_GROUP
    • 配置内容:
    [
      {
        "resource": "user",
        "count": 0,
        "paramItem": {
          "parseStrategy": 3,
          "fieldName": "name"
        }
      },
      {
        "resource": "uaa_api",
        "count": 0
      }
    ]
    

    规则1:所有user的请求只要参数带有name的都拦截(qps=0),user为zuul路由配置上的routeId
    规则2:api分组为uaa_api的所有请求都拦截(qps=0)

     

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

    file

    • Data ID:api-gateway-sentinel-gw-api-group
    • Group:DEFAULT_GROUP
    • 配置内容:
    [
      {
        "apiName": "uaa_api",
        "predicateItems": [
          {
            "pattern": "/user/login"
          },
          {
            "pattern": "/api-uaa/oauth/**",
            "matchStrategy": 1
          }
        ]
      }
    ]
    

    上面配置意思为满足规则的api都统一分组为uaa_api
    分组规则1:精准匹配/user/login
    分组规则2:前缀匹配/api-uaa/oauth/**

     

    4. 网关zuul启动参数

    需要在接入端原有启动参数的基础上添加-Dcsp.sentinel.app.type=1启动以将您的服务标记为 API Gateway,在接入控制台时您的服务会自动注册为网关类型,然后您即可在控制台配置网关规则和 API 分组,例如:

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

     

    三、sentinel控制台管理

    API管理(分组)
    file
    网关流控规则
    file

     

    四、测试限流api

    1. 测试限流规则1

    所有user的请求只要参数带有name的都拦截(qps=0)

    • 不加name参数,可以访问api
      file
    • 后面加上name参数,请求被拦截
      file

     

    2. 测试限流规则2

    api分组为uaa_api的所有请求都拦截(qps=0)

    • 前缀匹配/api-uaa/oauth/**
      file
    • 精准匹配/user/login
      file
  • 相关阅读:
    【转】Windows2012设置文件夹权限报错:failed to enumerate objects in the container.
    Vue 中背景图片路径怎么加参数?
    C#有小数位数直接进位为整数;JavaScript Math有小数位数取整方法
    C# Linq to Entity使用Groupby查询时超时或很慢解决思路
    colModel的设置(给单元格指定颜色等)
    金额的表示方法
    jqGrid表格时间格式化 ,formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s H:i:s'}
    HTTP Error 414. The request URL is too long. asp.net解决方案
    radio/checkbox各种操作
    Kubernetes常用命令总结
  • 原文地址:https://www.cnblogs.com/zlt2000/p/11375927.html
Copyright © 2020-2023  润新知