sentinel 热点限流

官网:https://sentinelguard.io/zh-cn/docs/parameter-flow-control.html

热点限流

sentinel使用lru算法统计最近最常使用的热点参数,结合令牌桶算法进行限流

# 热点数据限流:统计热点参数,根据配置的限流阈值与模式,对包含热点参数的资源进行限流
参数为商品id,统计一段时间内最常购买的商品ID并进行限制
参数为用户id,针对一段时间内频繁访问的用户ID进行限制

***********

基本使用

相关依赖

        <!-- spring-cloud-alibaba-starter-sentinel中有该依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>x.y.z</version></dependency>

ParamFlowRule

public class ParamFlowRule extends AbstractRule {private int grade = 1;              //限流模式:QPS(默认)、并发线程数private Integer paramIdx;           //热点参数索引private double count;               //限流阈值private int controlBehavior = 0;    //流控行为:快速失败(默认)、匀速排队模式private int maxQueueingTimeMs = 0;  //匀速排队模式时排队等待时间private int burstCount = 0;         //处理突发流量private long durationInSec = 1L;    //限流窗口时长,默认1sprivate List<ParamFlowItem> paramFlowItemList = new ArrayList();//针对指定的参数值单独设置限流阈值,不受count影响//仅支持基本类型和字符串类型private Map<Object, Integer> hotItems = new HashMap();private boolean clusterMode = false;  //是否为集群模式,集群模式传入的参数只支持基本类型和字符串类型private ParamFlowClusterConfig clusterConfig;  //集群限流配置public ParamFlowRule() {}public ParamFlowRule(String resourceName) {this.setResource(resourceName);}

AbstractRule

public abstract class AbstractRule implements Rule {private String resource;    //需要限流的资源名称private String limitApp;    //特定资源、default(不限资源)、otherpublic AbstractRule() {}

ParamFlowClusterConfig

public class ParamFlowClusterConfig {private Long flowId;                    //热点参数集群流控id,需保证全局唯一private int thresholdType = 0;          //阀值模式:0(单机均摊)、1(全局模式)private boolean fallbackToLocalWhenFail = false;  //client连接失败或通信失败时,是否退化到本地的限流模式private int sampleCount = 10;           //窗口数量private int windowIntervalMs = 1000;    //窗口统计时长,默认1spublic ParamFlowClusterConfig() {}

设置热点参数规则

    private static void initParamFlowRules() {// QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY).setParamIdx(0).setGrade(RuleConstant.FLOW_GRADE_QPS)//.setDurationInSec(3)//.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)//.setMaxQueueingTimeMs(600).setCount(5);// We can set threshold count for specific parameter value individually.// Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)// in index 0 will be 10, rather than the global threshold (5).ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B)).setClassType(int.class.getName()).setCount(10);rule.setParamFlowItemList(Collections.singletonList(item));ParamFlowRuleManager.loadRules(Collections.singletonList(rule));}

使用示例

***********

相关依赖

        <!-- 服务注册与发现 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- 服务调用 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><!-- sentinel dubbo适配器 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo-adapter</artifactId><version>1.8.0</version></dependency><!-- dubbo应用向控制台传输数据 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.0</version></dependency><!-- 热点限流 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>1.8.0</version></dependency>

***********

服务端

application.yml

spring:application:name: nacos-providercloud:nacos:discovery:server-addr: localhost:8848dubbo:protocol:name: dubboport: -1

HelloService

public interface HelloService {String hello(String name);
}

HelloServiceImpl

@DubboService
public class HelloServiceImpl implements HelloService {@Overridepublic String hello(String name) {return "hello " + name;}
}

           

DemoApplication

@EnableDubbo
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

***********

消费端

application.yml

spring:application:name: nacos-consuemrcloud:nacos:discovery:server-addr: localhost:8848dubbo:protocol:name: dubboport: -1server:port: 8081

HelloService

public interface HelloService {String hello(String name);
}

HelloController

@RestController
public class HelloController {@DubboReferenceprivate HelloService helloService;@RequestMapping("/hello")public String hello(){System.out.println(helloService.hello("瓜田李下"));return "success";}
}

***********

本地限流配置

CustomParamFlowRule

public class CustomParamFlowRule implements InitFunc {@Overridepublic void init() throws Exception {ParamFlowRule paramFlowRule = new ParamFlowRule("com.example.demo.service.HelloService:hello(java.lang.String)");paramFlowRule.setParamIdx(0).setGrade(RuleConstant.FLOW_GRADE_QPS).setCount(2);ParamFlowItem paramFlowItem = new ParamFlowItem();paramFlowItem.setObject("瓜田李下")    //name=瓜田李下的限流阀值是4//name为其他值的限流阀值是2.setClassType(String.class.getName()).setCount(4);paramFlowRule.setParamFlowItemList(Collections.singletonList(paramFlowItem));ParamFlowRuleManager.loadRules(Collections.singletonList(paramFlowRule));}
}

META-INF/services/com.alibaba.csp.sentinel.init.InitFunc

com.example.demo.config.CustomParamFlowRule

jmeter测试

              

              

              

              

***********

控制台限流配置

控制台启动

java -Dserver.port=8000 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar参数说明:
-Dserver.port=8080:指定控制台启动端口
-Dcsp.sentinel.dashboard.server:指定控制台地址和端口
-Dproject.name=sentinel-dashboard:指定控制台项目名称

服务端启动

-Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=nacos-provider

消费端启动

-Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=nacos-consumer

sentinel 控制台:localhost:8000

              

              

jmeter 测试

sentinel 热点限流相关推荐

  1. Sentinel系列之热点限流

    热点数据:就是那些经常被访问或者经常出现的数据,所以针对这种数据就会进行限流,比如针对刷单,同一个账号或者同一个ip进来的用户进行限流等等 Sentinel提供了热点参数限流的策略,其实就是一种特殊的 ...

  2. Sentinel之限流、降级、系统保护、热点、授权规则

    简介 Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳定性. 本文主要讲限 ...

  3. java 限流熔断_SpringCloud-Alibaba-Sentinel服务降级,热点限流,服务熔断

    前言:除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.一个服务常常会调用别的模块,可能是另外的一个远程服务.数据库,或者第三方 api 等.例如,支付的时候,可能需 ...

  4. SpringCloud 第八期 Sentinel 熔断限流

    sentinel可以作为监控平台使用,下载jar包运行 官网说明文档,有中文 Wiki - Gitee.com sentinel下载地址 https://github.com/alibaba/Sent ...

  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. 流量治理神器-Sentinel的限流模式,选单机还是集群?

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

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

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

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

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

最新文章

  1. jni返回byte[]
  2. Response.Redirect() 跳转中的ThreadAbortException
  3. 常考数据结构与算法:求二叉树的层序遍历
  4. 嵌入式编程与c语言有何区别,有的嵌入式设备也提供C++编译器,那还有理由坚持使用C语言吗?...
  5. c语言发牌思路,C语言发牌机程序求详细解析
  6. web项目通过ajax提交数据太大报错
  7. shell脚本while read line的使用
  8. php 随机生成ip
  9. HDU 4031 Attack
  10. linux如何远程装java_使用Shell远程给Linux安装JDK
  11. QT找不到python27.dll或python36.dll解决办法
  12. Win10 Ubuntu 制作多系统U盘
  13. ASP.NET Core 中文文档目录
  14. 带你了解云计算的优势
  15. python 爬虫 第一周:学会爬取网页信息
  16. gta5维护服务器,GTA6再等10年?R星强行续命GTA5,下月更新地图挤爆服务器
  17. cnpm i 与 npm i
  18. 记一次内网jenkins自动发布血泪史
  19. yj.iOS 仿微信长按摄像点击拍照
  20. 机器人将颠覆零售业,看AI在零售行业有哪些应用?

热门文章

  1. iOS--3分钟教会你用mathjax显示数学公式
  2. Maxim and Discounts(贪心)
  3. 对话阿里云崔昊:在线教育市场的变与不变?
  4. 开发快手爬票项目(最终章)
  5. 基于Vue的gannt(甘特图)组件
  6. 计算机学生社会实践报告范文,计算机系学生社会实践报告范文.doc
  7. python爬虫爬取新闻标题_Python3爬虫实战(一):新闻标题及其URL
  8. “QQ找茬”游戏的识别小软件JAVA源码(netBeams项目)
  9. “纳米光学先驱”师生档详解光计算,全球首款商用光子芯片即将问世-1
  10. location.protocol是什么?