基于注解的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的具体限流指标有多种,可以参考官方文档选择
相关推荐
设计师小聂!6 小时前
spring cloud alibaba Sentinel详解
java·spring cloud·sentinel
咖啡啡不加糖6 小时前
Sentinel原理与SpringBoot整合实战
spring boot·后端·sentinel
神码小Z6 小时前
SpringCloud实战:使用Sentinel构建可靠的微服务熔断机制
spring cloud·sentinel
曼岛_1 天前
[Java实战] Docker 快速启动 Sentinel 控制台(二十八)
java·docker·sentinel
暖苏2 天前
SpringCloud Alibaba微服务-- Sentinel的使用(笔记)
java·spring boot·spring cloud·微服务·sentinel·微服务保护·服务熔断
陆小叁3 天前
若依项目集成sentinel、seata和shardingSphere
sentinel·seata·shardingsphere
曼岛_4 天前
[Java实战]Spring Boot整合Sentinel:流量控制与熔断降级实战(二十九)
java·spring boot·sentinel
快乐肚皮5 天前
基于Spring Cloud Sentinel自研Slot扩展实战
java·spring cloud·sentinel
快乐肚皮5 天前
Spring Cloud Sentinel 快速入门与生产实践指南
spring·spring cloud·sentinel