/**
* @author 邓林妹
* @description TODO
* @date 2022/12/10
*/
@RestController
@RequestMapping("user")
//@RefreshScope
public class UserController {
@Autowired
private UserService userService;
@Autowired
private PatternProperties properties;
@GetMapping("{userId}")
//@SentinelResource(value = "fallback") //没有配置
//@SentinelResource(value = "fallback", fallback = "handlerFallback")//fallback只负责业务异常;java代码发生异常就调用handlerFallback方法处理
//@SentinelResource(value = "fallback", blockHandler = "blockHandler")//blockHandler只负责Sentinel控制台配置违规(配置违规:触发Sentinel配置的流控、降级规则)
//@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler") //两个都配置,同时发生java异常、触发Sentinel配置规则的话,blockHandlerd的优先级>fallback
@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler",exceptionsToIgnore = {IllegalAccessException.class})//exceptionsToIgnore属性:假如报该异常,不再调用fallback兜底方法,没有降级效果了;
public BaseResponse queryUserById(@PathVariable("userId") Long userId) throws Exception {
if (userId == 100) {
throw new IllegalAccessException("IllegalAccessException,非法参数异常");//这个就是java代码发生异常
} else if (userService.queryUserById(userId) == null) {
throw new NullPointerException("NullPointerException,空指针异常");
}
return BaseResponse.returnSuccess(userService.queryUserById(userId));
}
//本例是fallback
public BaseResponse handlerFallback(@PathVariable Long userId, Throwable e) {
return BaseResponse.returnFault("兜底异常handlerFallback,异常内容=" + e.getMessage());
}
//本例是blockHandler
public BaseResponse blockHandler(@PathVariable Long userId, BlockException e) {
return BaseResponse.returnFault("blockHandler-sentinel限流,blockException=" + e.getMessage());
}
}
Sentinel服务熔断-@SentinelResource属性配置
最新推荐文章于 2024-08-06 23:06:00 发布