sentinel网关限流配置及使用

sentinel控制台源码:https://download.csdn.net/download/yixin605691235/89543923

sentinel控制台jar包:https://download.csdn.net/download/yixin605691235/89543931


不同环境直接修改jar包中的application.yml文件中的nacos地址就可以了。

一、网关限流配置及使用

1、nacos配置

新增文件gdebs-gateway-sentinel.yml、gdebs-gateway-sentinel-dynamic.properties

XML 复制代码
spring:
  cloud:
    sentinel:
      eager: true
      #配置网关
      scg:
        fallback:
          content-type: application/json
          # 模式 response、redirect
          mode: response
          # 响应状态码
          response-status: ${sentinel.renturn.code}
          # 响应信息
          response-body: ${sentinel.renturn.msg}
      transport:
        dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}
        filter:
          enabled: false
        ip: gdebs-gateway-service
      # 控制台数据持久化配置
      datasource:
        ds1:
          nacos:
            server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}
            username: ${sentinel.nacos.name}
            password: ${sentinel.nacos.pwd}
            namespace: ${sentinel.nacos.namespace}
            group-id: ${sentinel.nacos.group}
            data-id: ${spring.application.name}-sentinel-flow-rules.json
            data-type: json
            rule-type: gw-flow
        ds2:
          nacos:
            server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}
            username: ${sentinel.nacos.name}
            password: ${sentinel.nacos.pwd}
            namespace: ${sentinel.nacos.namespace}
            group-id: ${sentinel.nacos.group}
            data-id: ${spring.application.name}-sentinel-api-rules.json
            data-type: json
            rule-type: gw-api-group
    gateway:
      # spring cloud gateway 路由配置方式
      discovery:
        locator:
          #表明gateway开启服务注册和发现的功能
          enabled: true
          #将请求路径上的服务名配置为小写
          lower-case-service-id: true
XML 复制代码
# sentinel控制台信息
sentinel.dashboard.host=xxxx
sentinel.dashboard.port=xxx

# sentinel返回信息
sentinel.renturn.msg= 对不起,已经被限流了!!!
sentinel.renturn.code= xxx

# sentinel数据持久化配置
sentinel.nacos.host=xxx.xx.xx.xx
sentinel.nacos.port=xxxx
sentinel.nacos.name=xx
sentinel.nacos.pwd=nacxxos
sentinel.nacos.namespace=xxx
sentinel.nacos.group=xxx
spring.application.name=xxx

2、网关应用增加依赖:

XML 复制代码
<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</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>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
        </dependency>

3、控制台配置限流规则

http://localhost:8081/#/login

帐号/密码:sentinel/sentinel

此处注意:sentinel控制台采用懒加载,因此需要现有请求才能看到链路

API管理:可以根据请求路径来设置API组

设置网关可以按照API分组设置,也可以设置app应用的总体限流


流控方式有快速失败和匀速排队两种:

快速失败可以设置Burst size,这是一个突刺个数,如果QPS设置为1,突刺数设置为10 ,当高并发请求时,首先能消耗的请求数是11个,超过11则拒绝,后续还是可通过一个请求。

匀速排队可以设置等待时长毫秒,当高并发请求超过QPS时,进入等待状态,如果超过等待时长则拒绝。

二、资源和规则配置说明

官网详细说明:basic-api-resource-rule | Sentinel

1、资源定义

主流框架默认适配:系统当前使用的springcloud本身支持请求链路
另外一种方式通过注解引用的方式:@SentinelResource

注解需要增加一个依赖

XML 复制代码
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.6</version>
</dependency>
java 复制代码
@RestController
@RequestMapping(value = "/sentinel")
@Api(value = "sentinel", tags = {"testController"})
public class TestController {
    @SentinelResource(value = "tesSentinel" , blockHandler = "myBlockHander")
    @PostMapping("/tesSentinel")
    public String tesSentinel() {return "1";}

    // 限流或者熔断之后执行的方法
    public String myBlockHander(BlockException blockException){
        if(blockException instanceof FlowException){
            // 限流异常
            return "您被限流了";
        }else if(blockException instanceof DegradeException){
            // 熔断异常
            return "您被熔断了";
        }
        return "被限制了";
    }
}

2、规则定义

可以通过代码设置规则,也可以通过控制台设置规则

java 复制代码
private void initFlowQpsRule() {

        List<FlowRule> rules=new ArrayList<>();
        System.out.println("限流初始化规则");
        // 定义一个限流规则
        FlowRule flowRule=new FlowRule();
        flowRule.setResource("tesSentinel"); // 资源名|必须参数
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流指标:QPS/线程数 |必须参数
        flowRule.setCount(1);  // 限流数量(上一步 QPS 或线程数的值) |必须参数
        flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT); //调用关系限流策略【非必须设置】
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER); // 流控效果【非必须设置】 排队等待
        flowRule.setMaxQueueingTimeMs(1000); // 等待超时时间
        flowRule.setClusterMode(false); // 是否集群限流【非必须设置,默认非集群】
        rules.add(flowRule);
        FlowRuleManager.loadRules(rules);


    }

提供的关联流控和链路流控可以应用到一些特殊的业务场景的流控

相关推荐
码代码的小农7 小时前
深入浅出Sentinel:分布式系统的流量防卫兵
sentinel
搬砖天才、1 天前
日常记录-redis主从复制(master-slave)+ Sentinel 哨兵(高可用)
数据库·redis·sentinel
是赵敢敢啊1 天前
sentinel
sentinel
东阳马生架构2 天前
Sentinel源码—9.限流算法的实现对比二
算法·sentinel
东阳马生架构2 天前
Sentinel源码—9.限流算法的实现对比一
算法·sentinel
东阳马生架构3 天前
Sentinel源码—9.限流算法的实现对比
sentinel
东阳马生架构3 天前
Sentinel源码—7.参数限流和注解的实现二
java·sentinel
东阳马生架构3 天前
Sentinel源码—8.限流算法和设计模式总结一
算法·sentinel
东阳马生架构4 天前
Sentinel源码—8.限流算法和设计模式总结
sentinel
东阳马生架构5 天前
Sentinel源码—6.熔断降级和数据统计的实现二
java·sentinel