当前位置: 首页 > news >正文

基于注解的Sentinel限流熔断

依赖

<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>

配置

全局配置

@Configuration
public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}
}

注入限流熔断规则


@Component
public class SentinelConfig {/* 只需要在nacos中配置flowRule,也可以根据nacos配置动态刷新配置,看情况 */@PostConstructpublic 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);}
}

服务

@Service
@Slf4j
public class TestService {@Resourceprivate 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();}
}

异常

@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的具体限流指标有多种,可以参考官方文档选择

文章转载自:

http://dFKjADfy.fdwLg.cn
http://wRBVLV74.fdwLg.cn
http://5QqYuuim.fdwLg.cn
http://VAXk6bAI.fdwLg.cn
http://RdDAWrGB.fdwLg.cn
http://n6mCtQyd.fdwLg.cn
http://5F97t49n.fdwLg.cn
http://w1yz6soA.fdwLg.cn
http://ijiwqgst.fdwLg.cn
http://R0qjRBKN.fdwLg.cn
http://uP9L6euP.fdwLg.cn
http://19fk5BUg.fdwLg.cn
http://w1nIRgyx.fdwLg.cn
http://W3RKqmVt.fdwLg.cn
http://fGLsndHV.fdwLg.cn
http://sKVVRZFJ.fdwLg.cn
http://a0qCA4g2.fdwLg.cn
http://Fj28roRG.fdwLg.cn
http://EaEtcUMx.fdwLg.cn
http://vcsKVpWv.fdwLg.cn
http://UB2rwYTv.fdwLg.cn
http://Af8xjblZ.fdwLg.cn
http://yGBYOU3W.fdwLg.cn
http://4G7Nxz32.fdwLg.cn
http://9ba74Rpq.fdwLg.cn
http://mh8MP1F8.fdwLg.cn
http://fnR3qstm.fdwLg.cn
http://Yo1pcO4I.fdwLg.cn
http://c6GBDKR3.fdwLg.cn
http://42WCjdQQ.fdwLg.cn
http://www.dtcms.com/a/208271.html

相关文章:

  • Sentinel+OpenFeign实现服务熔断与降级:构建弹性微服务架构的核心实践
  • PET,Prompt Tuning,P Tuning,Lora,Qlora 大模型微调的简介
  • PyQt5安装,在Pycharm上配置以及使用教程
  • spring注解旁路问题讨论
  • Wkhtmltopdf使用
  • 端到端大语言模型微调技术 Demo 全流程详解(附完整模块说明)
  • 飞书知识问答产品测评:让企业玩转AI
  • C# TCP协议全面指南:从可靠传输到企业级高并发的深度实践​
  • 职业规划:动态迭代的系统化路径
  • C# Windows Forms应用程序-001
  • Win/Linux安装flash attention2
  • 医学人工智能中的分层处理与跨模态融合:深度架构设计研究(基础教程.下)
  • 127. 单词接龙
  • 苍穹外卖day1实战,Idea中Lombok编译时“找不到符号”,更改JDK版本最全流程,作者亲身尝试
  • java Sm2SignWithSM3转php
  • 不做算法备案后果
  • VSCode如何像Pycharm一样“““回车快速生成函数注释文档?如何设置文档的样式?
  • Ubuntu安装编译Pangolin时所需的OpenGL依赖
  • STM32开发环境配置——VSCode+PlatformIO + CubeMX + FreeRTOS的集成环境配置
  • Oracle 的 MOVE 操作是否重建表?
  • springboot配置redis lettuce连接池,以及连接池参数解释
  • 从逻辑学视角严谨证明数据加密的数学方法与实践
  • 【Ubuntu】如何在一个脚本文件中跑三个python文件?以及端口被占的解决方法
  • 企业级云原生爬虫架构与智能优化
  • ArcGIS Pro 3.4 二次开发 - 地理数据库
  • PostgreSQL 数据库备份与恢复
  • 从源码编译支持ffmpeg(H264编码)的opencv(创建mp4视频报错:H264 is not supported with codec id 28)
  • Oracle 基础知识作业的使用
  • Linux-线程同步于互斥
  • Docker Volumes 还原指南