基于注解的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的具体限流指标有多种,可以参考官方文档选择
相关推荐
hzzzzzo05 小时前
微服务保护全攻略:从雪崩到 Sentinel 实战
数据库·微服务·sentinel
没有bug.的程序员1 天前
Redis Sentinel:高可用架构的守护者
java·redis·架构·sentinel
波波烤鸭1 天前
Sentinel 原理与源码解析:流控、熔断、降级到热点限流的一体化方案
sentinel
曾经的三心草2 天前
springcloud二-Sentinel
spring·spring cloud·sentinel
T_Ghost5 天前
SpringCloud微服务服务容错机制Sentinel熔断器
spring cloud·微服务·sentinel
你是人间五月天6 天前
sentinel实现控制台与nacos数据双向绑定
windows·sentinel
无名客06 天前
sentinel限流常见的几种算法以及优缺点
算法·sentinel·限流
tsxchen6 天前
centos9安装sentinel
sentinel
寒士obj6 天前
Sentinel服务治理:服务降级、熔断与线程隔离
sentinel
iiYcyk6 天前
Hystrix与Sentinel-熔断限流
hystrix·sentinel·springcloud