使用RateLimiter可查看另外两篇文章:

spring cloud网关(zuul)使用RateLimiter限流,使用jMeter性能测试高并发

spring cloud微服务间限流,使用jMeter性能测试高并发

导入包,pom.xml配置

<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>1.4.1</version>
</dependency>

初始化限流规则,示例是在启动类初始化,也可在其他地方编写。

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {public static void main(String[] args) {init();SpringApplication.run(ZuulApplication.class, args);}/*** 初始化限流*/private static void init(){List<FlowRule> rules = new ArrayList<>();FlowRule flowRule = new FlowRule();//资源名称flowRule.setResource("service-order");//限流类型flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//每秒2 QPSflowRule.setCount(2);rules.add(flowRule);FlowRuleManager.loadRules(rules);}
}

方案1:使用Filter

网关过滤器编写相关逻辑

@Component
public class SentinelFilter extends ZuulFilter {@Overridepublic String filterType() {return FilterConstants.PRE_TYPE;}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() throws ZuulException {Entry entry = null;try{entry = SphU.entry("service-order");//业务逻辑System.out.println("正常请求");}catch (Exception  e){System.out.println("限流了");}finally {if(entry != null){entry.exit();}}return null;}
}

注意Entry选择:阿里巴巴包下:com.alibaba.csp.sentinel.Entry

方案2:使用注解

新增服务类:

@Service
public class SentinelService {/*** 正常请求* @return*/@SentinelResource(value = "service-order",blockHandler = "fail")public String success(){System.out.println("正常请求!");return "success";}/*** 阻塞* @return*/public String fail(BlockException e){System.out.println("被限流了");return "fail";}
}

编写controller测试类:

@RestController
@RequestMapping("/limit")
public class LimitTestController {@Autowiredprivate SentinelService sentinelService;@RequestMapping("/limitTest")public String limitTest(){//        return "1111111";return sentinelService.success();}
}

在启动类增加注入bean:

@Beanpublic SentinelResourceAspect sentinelResourceAspect(){return new SentinelResourceAspect();}

初始化限流规则与方案1一致。

使用sentinel实现限流相关推荐

  1. 流量治理神器-Sentinel的限流模式,选单机还是集群?

    大家好,架构摆渡人.这是我的第5篇原创文章,还请多多支持. 上篇文章给大家推荐了一些限流的框架,如果说硬要我推荐一款,我会推荐Sentinel,Sentinel的限流模式分为两种,分别是单机模式和集群 ...

  2. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!

    前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...

  3. 【alibaba-cloud】网关整合sentinel实现限流

    网关整合sentinel实现限流 还是在前几篇博客的基础上搭建的,需要有服务端,客户端,网关等 客户端服务端的搭建:https://blog.csdn.net/wangyunzhao007/artic ...

  4. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战

    文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流实现,过滤器是RequestRateLimiterGatewayFilterFactory,不过这种上不了台面的就 ...

  5. sentinel 网关限流

    sentinel 网关限流 官网:https://sentinelguard.io/zh-cn/docs/api-gateway-flow-control.html 网关限流 sentinel支持对主 ...

  6. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    Spring Cloud Alibaba | Sentinel: 服务限流高级篇 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明 ...

  7. Spring Cloud Alibaba实战(三) - Sentinel之限流

    目录 (一)Nacos动态配置 (二)Nacos注册中心 (三)Sentinel之限流 (四)Sentinel之熔断 (五)Gateway之路由.限流 (六)Gateway之鉴权.日志 (七)Gate ...

  8. sentinel 热点限流

    sentinel 热点限流 官网:https://sentinelguard.io/zh-cn/docs/parameter-flow-control.html 热点限流 sentinel使用lru算 ...

  9. SpringCloud Alibaba微服务实战(五) - Sentinel实现限流熔断

    什么是Sentinel? 请查看文章:SpringCloud Alibaba微服务实战(一) - 基础环境搭建 构建服务消费者cloud-sentinel进行服务调用 服务创建请查看文章:Spring ...

  10. Sentinel实现限流,竟是如此的简单!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来源:https://tianyalei.blog.csd ...

最新文章

  1. Mysql备份与还原及优化方法
  2. Spring学习笔记之一----基于XML的Spring IOC配置
  3. java中的builder模式
  4. 单列索引和联合索引,有什么区别?
  5. 如何修改CSDN博客的标题?
  6. PropertyPlaceholderConfigurer实现配置文件读取
  7. 超详细:Springboot连接centos7下redis6的必要配置和失败分析
  8. DTC精彩回顾—黄东旭《TiDB数据驱动的企业智能化转型新方向》丨PPT视频
  9. 使用计算机眼睛保护方法,电脑工作者保护眼睛的22个方法
  10. 江苏计算机等级考试试卷,江苏省计算机等级考试程序设计 试卷.docx
  11. 远程桌面连接无法相互拷贝文件了?
  12. ETCD for java_etcd-java使用
  13. 对WITH和from(select ...)的一点比较
  14. 关于CF平台中基础服务的监控方案
  15. 《大规模元搜索引擎技(1)》一 2.1 系统体系结构
  16. JS常用事件兼容性处理方法
  17. 往后余生-程序员版,哈哈哈
  18. google chrome adobe flash player过期解决方法
  19. 微软必应词典UWP -2017春
  20. 计蒜客-A1024 淘宝卖家评价体系

热门文章

  1. CALL TRANSACTION 小节
  2. 横线登记式明细分类账
  3. 高压线下,恶俗短视频为何仍在批量生产?
  4. 长沙望城:以“速”大干一百天,以“质”实现双过半
  5. 成都七中实验学校爆食品安全问题,互联网+后勤能做点什么吗?
  6. Python:用生成器的方式计算任意起止范围内质数的和。
  7. linux常用ogg脚本,ogg基本监控脚本
  8. python 调用gpu算力_GPU捉襟见肘还想训练大批量模型?谁说不可以
  9. python循环五角星做法_python实现while循环打印星星的四种形状
  10. linux 线程流水线,linux线程同步