Sentinel入门

1.侵入式的方式

侵入式的代码如下,用SphU.entry定义要限制的业务逻辑

csharp 复制代码
package com.hamster.sentineldemo;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

//测试侵入式代码的方式
public class Test {

    public static void main(String[] args) throws Exception {
        initFlowRules();
        while (true) {
            Entry entry = null;
            try {
                entry = SphU.entry("HelloWorld");
                /*您的业务逻辑 - 开始*/
                System.out.println("hello world");
                /*您的业务逻辑 - 结束*/
            } catch (BlockException e1) {
                /*流控逻辑处理 - 开始*/
                System.out.println("block!");
                /*流控逻辑处理 - 结束*/
                break;
            } finally {
                if (entry != null) {
                    entry.exit();
                }
            }
        }
    }

    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

2.注解方式

首先用@SentinelResource定义要限流的方法:

csharp 复制代码
package com.hamster.sentineldemo;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/test")
    @SentinelResource(
            value = "test",
            blockHandler = "testBlockHandler",
            fallback = "testFallback"
    )
    public String test(){
        return "test";
    }
    public String testBlockHandler(BlockException ex){
        return "testBlockHandler";
    }
}

blockHandler 是被限流后的操作

fallback 是出错误时的操作,即熔断

然后定义规则:

csharp 复制代码
package com.hamster.sentineldemo;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration
public class SentinelConfig {
    @PostConstruct
    public void initRules(){
        FlowRule rule = new FlowRule("test")
                .setCount(1)
                .setGrade(RuleConstant.FLOW_GRADE_QPS)
                .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        FlowRuleManager.loadRules(Collections.singletonList(rule));
        System.out.println("Rules loaded successfully.");
    }
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

注解不生效的可能情况:

1.两个方法都要用public不要用private,第一个方法不加会导致sentinel找不到getResult这个资源名,同样的第二个不加会导致sentinel找不到这个方法然后报错。

2.两个方法返回类型必须一样。

3.两个方法的参数类型必须一样,数量和参数类型要保持一致,注意第一个方法加了@PathVariable这个注解,这个只是url传参用的不需要加。

4.blockHandler的值必须与你的兜底方法,也就是第二个方法保持一致。

5.blockHandler对应方法必须要加BlockException这个异常类参数。

参考:

入门教程 https://developer.aliyun.com/article/783342

不生效情况 https://www.cnblogs.com/ningyyrx/p/16018484.html

相关推荐
异常君2 小时前
Windows 与 Linux 虚拟内存机制对比:设计理念与实现差异
java·linux·windows
搏博4 小时前
将图形可视化工具的 Python 脚本打包为 Windows 应用程序
开发语言·windows·python·matplotlib·数据可视化
电手5 小时前
Win10停更,Win11不好用?现在Mac电脑比Win11电脑更便宜
windows·macos·电脑·mac
拾回程序猿的圈圈∞5 小时前
PyCharm项目和文件运行时使用conda环境的教程
windows·pycharm·conda
波点兔5 小时前
【亲测有效 | Cursor Pro每月500次快速请求扩5倍】(Windows版)Cursor中集成interactive-feedback-mcp
windows·mcp·cursor pro
饮长安千年月7 小时前
JavaSec-SSTI - 模板引擎注入
java·windows·安全·web安全·网络安全·系统安全·安全架构
字节高级特工10 小时前
【Linux篇】0基础之学习操作系统进程
linux·运维·服务器·数据结构·windows·学习·list
Amo Xiang19 小时前
Python 解释器安装全攻略(适用于 Linux / Windows / macOS)
linux·windows·python·环境安装
小邓儿◑.◑1 天前
C++初阶 | 模板
网络·c++·windows
豆芽脚脚1 天前
spel 多层list嵌套表达式踩坑记
windows·list·spel