Sentinel热点key

1.基本介绍

在这里插入图片描述

官方文档

何为热点?热点即经常访问的数据。很多时候我们希望统计某个热点数据中访问频次最高的 Top K 数据,并对其访问进行限制。比如:

  • 商品 ID 为参数,统计一段时间内最常购买的商品 ID 并进行限制
  • 用户 ID 为参数,针对一段时间内频繁访问的用户 ID 进行限制

热点参数限流会统计传入参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用进行限流。热点参数限流可以看做是一种特殊的流量控制,仅对包含热点参数的资源调用生效。

在这里插入图片描述

Sentinel 利用 LRU 策略统计最近最常访问的热点参数,结合令牌桶算法来进行参数级别的流控。热点参数限流支持集群模式。

2.基本项配置

兜底方法,分为系统默认和客户自定义,两种

之前的case,限流出问题后,都是用sentinel系统默认的提示: Blocked by Sentinel (flow limiting)

我们能不能自定?类似hystrix,某个方法出问题了,就找对应的兜底降级方法?

结论 - 从HystrixCommand到@SentinelResource

测试

/**
 * Sentinel热点 - 测试方法
 * blockHandler 兜底方法
 * @author liqb
 * @date 2023-05-25 19:56
 * @return
 */
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                         @RequestParam(value = "p2",required = false) String p2) {
    // int age = 10/0;
    return "------testHotKey";
}
// 兜底方法
public String deal_testHotKey (String p1, String p2, BlockException exception) {
    // sentinel系统默认的提示:Blocked by Sentinel (flow limiting)
    return "------deal_testHotKey,o(╥﹏╥)o";  
}

配置

在这里插入图片描述

@SentinelResource的两种情况

  1. @SentinelResource(value = “testHotKey”)
    1. 异常打到了前台用户界面看到,不友好
  2. @SentinelResource(value = “testHotKey”, blockHandler = “dealHandler_testHotKey”)
    1. 方法testHotKey里面第一个参数只要QPS超过每秒1次,马上降级处理
    2. 异常用了我们自己定义的兜底方法

测试

  • error
    • http://localhost:8401/testHotKey?p1=abc
    • http://localhost:8401/testHotKey?p1=abc&p2=33
  • right
    • http://localhost:8401/testHotKey?p2=abc

3.参数例外项

  • 普通 - 超过1秒钟一个后,达到阈值1后马上被限流
  • 我们期望p1参数当它是某个特殊值时,它的限流值和平时不一样
  • 特例 - 假如当p1的值等于5时,它的阈值可以达到200

配置

在这里插入图片描述

测试

  • right - http://localhost:8401/testHotKey?p1=5
  • error - http://localhost:8401/testHotKey?p1=3
  • 当p1等于5的时候,阈值变为200
  • 当p1不等于5的时候,阈值就是平常的1

前提条件 - 热点参数的注意点,参数必须是基本类型或者String

其它

在方法体抛异常

/**
 * Sentinel热点 - 测试方法
 * blockHandler 兜底方法
 * @author liqb
 * @date 2023-05-25 19:56
 * @return
 */
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                         @RequestParam(value = "p2",required = false) String p2) {
    int age = 10/0; //<----------------------------会抛异常的地方
    return "------testHotKey";
}
// 兜底方法
public String deal_testHotKey (String p1, String p2, BlockException exception) {
    // sentinel系统默认的提示:Blocked by Sentinel (flow limiting)
    return "------deal_testHotKey,o(╥﹏╥)o";  
}

将会抛出Spring Boot 2的默认异常页面,而不是兜底方法。

  • @SentinelResource - 处理的是sentinel控制台配置的违规情况,有blockHandler方法配置的兜底处理;
  • RuntimeException int age = 10/0,这个是java运行时报出的运行时异常RunTimeException,@SentinelResource不管

**总结 **- @SentinelResource主管配置出错,运行出错该走异常走异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
new features: 1: Add a dongle detection method. 2: Increase the detection of a series of dongle models. Currently there are more than 50 types of dongles that can be detected. The specific types that can be detected are as follows: Flying faithful, solid integrity series dongle: Flying Integrity -ROCKEY 2 Flying Integrity -ROCKEY 3 Flying faith -ROCKEY 4 (drive type) Flying ftsafe Flying 4 Driveless Flying Integrity -ROCKEY 6 Kennedy Credit -ET199 Kennedy Credit -ET99/ET299 Feitian integrity / Kennedy integrity unknown model encryption dog Rainbow/Sailor Series dongle: Rainbow UMI Series dongle Rainbow UDA Series dongle SafeNet Elf Dog UGA (GeniusDog) Rainbow Macro Dog Rainbow Unknown series dongle SafeNet SafeNet Ultra Pro SafeNet USB Security Security Token SafeNet Sentinel Hardware Key SafeNet Sentinel Hardware Key SafeNet Sentinel Hardware Key RTC SafeNet Sentinel Hardware Key Mega-Memory SafeNet Sentinel Hardware Key Mega-Memory RTC SafeNet Sentinel Dual Hardware Key SafeNet Sentinel Dual Hardware Key RTC SafeNet HASP HL ​​SRM Dongguan domain days series dongle: Dongguan Tiantian YT88 dongle Domain Day Simple (with password) Domain days Simple type Tiantian professional/economical and easy to use Dongguan Tianzhi 32-bit smart dongle Dongguan Domain 32-bit smart dongle (with driver) Dongguan Tiantian Unknown Model Dongle Shenzhen Rock Series dongles: Deepsense (senseLock) Meditation 3 dongle Deepsense (SenseLock) SenseIV2.x Shensi 4 smart dongle SenseLock Elite E Elite Dog Deepsense (senseLock) Ling Rui dongle SenseLock OME Speed ​​3000 Pro, Driveless Dragon Pulse series dongle: Dragon Vein nox2 dongle Century Longmai NOX Dongle Dragon Pulse DAM2+ dongle Century Longmai DAM2 Streamlined Dragon Pulse DAM2 dongle Century Longmai NOX+ Single Dog, No Drive Century Longmai (Century Longmai DAM2+ Network Dog Century Longmai NOX5 Master Lock Dragon Vein NOX5 dongle Dragon Pulse other models dongle Jiang Bolong series dongle: NetCom Security Chip (SChip) Dong
Sentinel 是阿里巴巴开源的一个流量控制框架,它支持热点限流功能。要通过 Nacos 配置 Sentinel热点限流,首先需要在 Nacos 中管理 Sentinel 相关的服务发现配置。 1. **创建Nacos配置**: - 登录到 Nacos 控制台,进入 `配置` 或者 `Config Center` 页面。 - 创建一个新的数据源,用于存放 Sentinel 的配置文件,比如命名空间为 `sentinel-config`。 2. **配置热点规则**: - 编辑一个名为 `hot_rule.yaml` 或类似名称的配置文件,添加如下内容: ```yaml # 热点限流配置示例 rule: # 定义一个应用名,如your-app-name app: your-app-name # 规则组名,可以自定义 group: default # 开启热点规则 enabled: true # 热点阈值,例如每分钟访问次数超过50次,则触发限流 count: 50 # 时间窗口,例如1分钟内计数 window: 60s # 激活模式,可以是固定阈值(fixed)、滑动平均(movingAverage) mode: fixed # 如果激活模式是 movingAverage,还可以配置滑动平均周期,单位秒 smoothWindow: 60s # 具体策略,这里通常是 error 或者 slow strategy: error ``` - 将这个配置文件发布到 Nacos 数据源。 3. **在应用中引用配置**: - 在你的 Spring Boot 应用中,通过 Sentinel 提供的自动配置类(如 `SentinelProperties`)来获取 Nacos 中的热点限流配置。你可以在 `application.properties` 或者 `application.yml` 文件中设置 Sentinel 自动发现 Nacos 地址: ```properties sentinel.config-center=nacos://<your-nacos-server>:8848/sentinel-config ``` 完成以上步骤后,Sentinel 会从 Nacos 获取热点限流配置,并根据配置对指定的应用、组别和接口进行限流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值