SpringCloudAlibaba:@SentinelResource

SpringCloudAlibaba:@SentinelResource

一、按资源名称添加流控规则

1.新建controller

@RestController
public class SentinelResourceController {
    @GetMapping("/resource")
    @SentinelResource(value = "resource",blockHandler = "handleException")
    public String resource(){
        return "SentinelResourceController invoke resource success";
    }
    
    public String handleException(BlockException e){
        return "SentinelResourceController invoke handleException";
    }
}

然后启动项目,在浏览器输入地址,然后在sentinel的控制台就可以看到了

在这里插入图片描述

2.新建流控规则

在这里插入图片描述

3.测试

刷新频繁会出现

在这里插入图片描述

二、自定义异常返回类

1.自定义handler

public class ZrsBlockHandler {
    
    public static String handler1Exception(BlockException exception){
        return  "ZrsBlockHandler invoke handler【1】Exception";
    }
    public static String handler2Exception(BlockException exception){
        return  "ZrsBlockHandler invoke handler【2】Exception";
    }
}

2.修改SentinelResourceController

@RestController
public class SentinelResourceController {
    @GetMapping("/resource")
    @SentinelResource(value = "resource",blockHandler = "handleException")
    public String resource(){
        return "SentinelResourceController invoke resource success";
    }
    public String handleException(BlockException e){
        return "SentinelResourceController invoke handleException";
    }
    @GetMapping("/handler1")
    @SentinelResource(value = "handler1Exception",blockHandlerClass = ZrsBlockHandler.class,
    blockHandler = "handler1Exception")
    public String handler1(){
        return "SentinelResourceController invoke resource success";
    }

    @GetMapping("/handler2")
    @SentinelResource(value = "handler2Exception",blockHandlerClass = ZrsBlockHandler.class,
            blockHandler = "handler2Exception")
    public String handler2(){
        return "SentinelResourceController invoke resource success";
    }
}

3.启动服务

http://localhost:8005/handler1
http://localhost:8005/handler2

4.添加流控规则

在这里插入图片描述

在这里插入图片描述

5.测试

频繁访问

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值