6.7.2 热点规则
热点资源就是在一定时间内访问特别频繁的资源,而某个热点资源可能只是某些参数访问特别频繁;
Sentinel
不仅支持以资源为粒度的限制,还可以细化到参数,热点规则就是对具体参数进行限制保护的实现;
新增一个接口: 有两个(非必要)参数
@RequestMapping("/hotPointTest")
@SentinelResource(value = "hotPointTest", blockHandler = "hotPointTestHandler")
public String hotPointTest(@RequestParam(value = "paramOne", required = false) String paramOne, @RequestParam(value = "paramTwo", required = false) String paramTwo) {
return "return from hotPointTest" + RandomUtils.nextInt();
}
创建对应的兜底方法:
public String hotPointTestHandler(String paramOne, String paramTwo, BlockException blockException) {
return "return from customHotPointTestHandler,paramOne=" + paramOne + ",paramTwo=" + paramTwo + RandomUtils.nextInt();
}
添加热点规则如下:
我们现在针对的参数索引是0
,也就是接口中第一个参数paramOne
。
测试:
当请求不带参数,或者只带上参数索引为1
的参数paramTwo
。
http://127.0.0.1:8041/hotPointTest?origin=mobile
http://127.0.0.1:8041/hotPointTest?origin=mobile¶mTwo=%E6%B5%8B%E8%AF%95%E5%8F%82%E6%95%B02
请求全通过,说明热点规则没有生效。
当请求带上参数索引为0
的参数paramOne
。
http://127.0.0.1:8041/hotPointTest?origin=mobile¶mOne=%E6%B5%8B%E8%AF%95%E5%8F%82%E6%95%B01
这时候热点规则生效了,因为我们配置的热点规则是针对paramOne
这个参数的,只有带上这个参数的时候才会进行流控。