基于注解的Sentinel限流熔断

依赖

xml 复制代码
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
    <version>1.8.8</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.8</version>
</dependency>

配置

全局配置

java 复制代码
@Configuration
public class SentinelAspectConfiguration {
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

注入限流熔断规则

java 复制代码
@Component
public class SentinelConfig {

    /* 只需要在nacos中配置flowRule,也可以根据nacos配置动态刷新配置,看情况 */
    @PostConstruct
    public void initFlowRules() {
        List<FlowRule> flowRules = new ArrayList<>();

        FlowRule rule = new FlowRule();
        rule.setResource(SentinelResourceConstant.TEST_API); // 资源名要与 @SentinelResource 的 value 一致
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1); // QPS 限流阈值

        FlowRule rul2 = new FlowRule();
        rul2.setResource(SentinelResourceConstant.HELLO_API); // 资源名要与 @SentinelResource 的 value 一致
        rul2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rul2.setCount(2); // QPS 限流阈值

        flowRules.add(rule);
        flowRules.add(rul2);
        FlowRuleManager.loadRules(flowRules);
    }
}

服务

java 复制代码
@Service
@Slf4j
public class TestService {

    @Resource
    private HelloClient helloClient;

    @SentinelResource(value = SentinelResourceConstant.TEST_API, blockHandler = "blockHandler", blockHandlerClass = ExceptionUtil.class)
    public String chain(String pa) {
        return pa + "OKK";
    }

    @SentinelResource(value = SentinelResourceConstant.HELLO_API, blockHandler = "blockHandlerForHello", blockHandlerClass = ExceptionUtil.class)
    public String hello() {
        return helloClient.hello();
    }
}

异常

java 复制代码
@Slf4j
public class ExceptionUtil {

    public static String blockHandler(String pa, BlockException ex) {
        log.error("限流:{}", String.valueOf(ex));
        return "限流";
    }

    public static String blockHandlerForHello(BlockException ex) {
        log.error("限流:{}", String.valueOf(ex));
        return "限流";
    }
}

注意事项

  • SentinelResource注解中的value必须和SentinelConfig 中rule的resource保持完全一致
  • FlowRule的具体限流指标有多种,可以参考官方文档选择
相关推荐
java叶新东老师2 天前
四、搭建springCloudAlibaba2021.1版本分布式微服务-加入openFeign远程调用和sentinel流量控制
分布式·微服务·sentinel
曹朋羽5 天前
Spring Cloud Alibaba Sentinel 基本工作原理源码阅读
sentinel
Zfox_8 天前
Redis:哨兵(Sentinel)
服务器·数据库·redis·缓存·sentinel
Bug改不动了9 天前
微服务雪崩防护最佳实践之sentinel
java·sentinel
朱杰jjj10 天前
Sentinel配置Nacos持久化
sentinel
AI_RSER10 天前
基于GEE与哨兵2号的土地覆盖分类方法及实现
机器学习·信息可视化·分类·sentinel·可视化·遥感·gee
@淡 定14 天前
Sentinel热点参数限流完整示例实现
windows·sentinel
okok__TXF21 天前
Sentinel入门篇【流量治理】
java·sentinel
述雾学java24 天前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
虚!!!看代码24 天前
【Sentinel学习】
网络·sentinel