关于使用SentinelResource碰到的问题
@GetMapping("/HotKey")
@SentinelResource(value = HOT_KEY, blockHandlerClass = CustomizeBlockHandler.class, blockHandler = "handlerMethod")
public CommonResult hotKey(@RequestParam(name = "p1", required = false) String p1, @RequestParam(name = "p2", required = false) String p2) {
return new CommonResult(0x00001L, "请求成功", "hot key request success > p1: " + p1 + " > p2 :" + p2);
}
private final static String HOT_KEY = "testHotKey";
@PostConstruct
public void init() {
List<FlowRule> rules = new ArrayList<>();
FlowRule ruleUser = new FlowRule();
ruleUser.setResource(HOT_KEY);
ruleUser.setGrade(RuleConstant.FLOW_GRADE_QPS);
ruleUser.setCount(1);
rules.add(ruleUser);
FlowRuleManager.loadRules(rules);
}
BlockHandlerClass类
public class CustomizeBlockHandler {
public static CommonResult handlerMethod(String p1,String p2,BlockException blockException) {
return new CommonResult(0x10000L, "热点key设置成功", "hotkey");
}
}
不生效的原因
1.指定的方法必须是static的,否则不生效

2.blockhandler指定的方法必须参数必须与注解的方法参数一致,并添加BlockException的方法参数,否则不生效

3.修改blockhandler方法参数,必须重启项目,否则不会生效