SpringCloud网关限流方式一:Sentinel限流

前言:限流的关键有两点:

        1.生成令牌

        2.抢令牌

step1:pom.xml配置

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.3</version>
</dependency>
 <dependency>
     <groupId>com.alibaba.csp</groupId>
     <artifactId>sentinel-annotation-aspectj</artifactId>
     <version>1.8.3</version>
</dependency>

step2: 生成令牌:

@SpringBootApplication
@EnableZuulProxy
public class CloudZuulApplication {

    public static void main(String[] args) {
        initQPS();//sentinel初始化
        SpringApplication.run(CloudZuulApplication.class, args);
    }

    private static void initQPS() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();

        rule.setResource("SentinelService.success");//需要保护的资源名称 方式1
//        rule.setResource("helloworld");//需要保护的资源名称 方式2
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//限流的类型
        rule.setCount(5);//设置最大并发数为5
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    /**
     * 在执行之前进行限流判断,在捕获异常的时候,
     * 会根据异常类型判断是调用fallback方法还是调用block handler方法
     * @return
     */
    @Bean
    public SentinelResourceAspect sentinelResourceAspect(){
        return new SentinelResourceAspect();
    }


}

step3: 使用令牌以下方式二选一

方式1:value一般为类名+方法名,blockHandler为限流后的访问的方法

@Service
public class SentinelService {
    @SentinelResource(value = "SentinelService.success", blockHandler = "fail")
    public String success() {
        System.out.println("success...请求正常");
        return "success";
    }

    public String fail(BlockException e) {
        System.out.println("fail...阻塞");
        return "fail";
    }
}

方式2:通过自定义过滤器,

@Component
public class SentinelFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return -100;
    }

    @Override
    public boolean shouldFilter() {
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        // TODO: 2022/3/30 限流的业务逻辑,使用令牌
        Entry entry = null;
        try {
            entry = SphU.entry("helloword");
            //业务逻辑
            System.out.println("正常请求");
        } catch (BlockException e) {
            System.out.println("阻塞了");
            RequestContext.getCurrentContext().set("limit", false);
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
        return null;
    }

step4测试

@RestController
public class MyController {
    @Autowired
    private SentinelService service;

    @GetMapping("/myController")
    public String testController() {

        return service.success();
    }
}

step4:使用jemeter并发测试结果

 

step5:小心踩坑

Caused by: com.alibaba.csp.sentinel.slots.block.flow.FlowException: null_起球就杀的码农小王的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路出家的码农小王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值