SentinelResource配置(上)

SentinelResource配置

一、按资源名称限流+后续处理

1、启动Nacos
在这里插入图片描述

2、启动Sentinel
在这里插入图片描述
3、在cloudalibaba-sentinel-service8401工程下的controller文件目录中新创建RateLimitController类

package com.junfu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.junfu.springcloud.entities.CommonResult;
import com.junfu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource()
    {
        return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
    }
    public CommonResult handleException(BlockException exception)
    {
        return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }

}

在这里插入图片描述
4、启动cloudalibaba-sentinel-service8401

5、配置流控规则
在这里插入图片描述
6、再次测试http://localhost:8401/byResource接口

  • 正常访问(单次点击)
    正常
    在这里插入图片描述
  • 快速多次访问
    异常、降级处理
    在这里插入图片描述

二、按url地址限流+后续处理

1、在RateLimitController类中新建方法byUrl()

package com.junfu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.junfu.springcloud.entities.CommonResult;
import com.junfu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource()
    {
        return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
    }
    public CommonResult handleException(BlockException exception)
    {
        return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }

    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按url地址限流测试OK",new Payment(2020L,"serial002"));
    }

}

在这里插入图片描述
2、重启cloudalibaba-sentinel-service8401

3、Sentinel控制台配置
在这里插入图片描述
4、再次测试http://localhost:8401/rateLimit/byUrl接口

  • 正常访问(单次点击)
    正常
    在这里插入图片描述
  • 快速多次访问
    异常、降级处理
    在这里插入图片描述
    由于没有手动写blockHandler处理函数,因此会调用Sentinel默认处理函数

三、兜底处理函数面临的问题

1、系统默认的,没有体现我们自己的业务要求。
2、依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。
3、每个业务方法都添加一个兜底的,那代码膨胀加剧。
4、全局统—的处理方法没有体现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值