这篇文章主要讲解了“使用alibaba sentinel失败的问题有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“使用alibaba sentinel失败的问题有哪些”吧!
sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。自从hytrix 2018年进入维护状态,再到springcloud 2020.0版本hytrix被移除,就可以料想未来一段时间springcloud全家桶的熔断降级组件基本上的首选就是alibaba sentinel。
a、原因分析
项目中使用了自定义全局异常处理,而异常数或者异常比例的统计在
com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion
这个方法执行, 自定义全局异常的处理会先于
com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion
这个方法执行执行, 因为我们在全局异常里面已经对异常进行处理,比如转换为一个对象,这样导致AbstractSentinelInterceptor.afterCompletion无法获取到异常,进而无法统计异常数或者异常比例。
b、解决方案
在官方的issue中已经有网友提出了解决思路 https://github.com/alibaba/Sentinel/issues/1281 和 https://github.com/alibaba/Sentinel/issues/404
因为我是在查issue的之前,就通过源码跟踪,找到答案,这边说下我的实现思路。我的思路是定义一个切面,在切面的AfterThrowing进行异常统计。因为切面会在全局异常之前执行。统计的源码我是直接把sentinel统计的源拷贝过来,核心代码如下
@Aspect
@Component
@Slf4j
public class StatisticsExceptionCountAspect {
@Autowired
@Lazy
private BaseWebMvcConfig baseWebMvcConfig;
@Pointcut("execution(* com.github.lybgeek.sentinel.controller..*.*(..))")
public void pointcut(){
}
@AfterThrowing(pointcut = "pointcut()",throwing = "ex")
public void afterAfterThrowing(Throwable ex){
log.info("statisticsExceptionCount...");
traceException(ex);
}
/**
* 统计异常
* @param ex
*/
private void traceException(Throwable ex) {
Entry entry = getEntryInRequest();
if (entry != null) {
Tracer.traceEntry(ex, entry);
}
}
protected Entry getEntryInRequest() {
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes)requestAttributes;
HttpServletRequest request = attributes.getRequest();
Object entryObject = request.getAttribute(baseWebMvcConfig.getRequestAttributeName());
return entryObject == null ? null : (Entry)entryObject;
}
}
a、原因分析
项目中没有实现
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser
接口,导致无法解析请求来源
b、解决方案
在项目中自定义请求来源解析器。示例代码如下
**
* @description: 解析访问来源,用于授权规则--黑白名单。
* 当要进行授权规则时,则必须配置RequestOriginParser,否则授权规则无法生效
*
**/
@Component
public class CustomRequestOriginParser implements RequestOriginParser {
@Override
public String parseOrigin(HttpServletRequest request) {
String origin = request.getParameter("origin");
if(!StringUtils.isEmpty(origin)){
//根据接口是否携带origin参数,如果携带参数为origin=pc,
// 且sentinel-dashbord授权规则,来源设置为pc时,则表示要对请求来源为pc,进行黑白名单配置
return origin;
}
//如果没请求参数接口没有携带,则表示按ip进行黑白名单设置
return request.getRemoteAddr();
}
}
a、原因分析
web埋点如果以url作为资源名,规则不生效
b、解决方案
以@SentinelResource注解定义的name作为资源名
参考官方issue https://github.com/alibaba/Sentinel/issues/1734
配置热点规则配置@SentinelResource后,可能还会出现
java.lang.reflect.UndeclaredThrowableException: null
解决方法:需要在方法中添加throws BlockException或添加blockHandler来处理异常
参考官方issue
https://github.com/alibaba/Sentinel/issues/776
示例代码
@GetMapping(value = "/paramFlowRule/{msg}")
@ApiImplicitParams({
@ApiImplicitParam(name="msg",defaultValue = "hello",value="信息", paramType = "path"),
})
@ApiOperation(value = "测试热点规则")
@SentinelResource(value = "testParamFlowRule")
public AjaxResult<String> testParamFlowRule(@PathVariable("msg") String msg) throws BlockException {
System.out.println(String.format("msg : %s",msg));
return AjaxResult.success("测试热点规则");
}
感谢各位的阅读,以上就是“使用alibaba sentinel失败的问题有哪些”的内容了,经过本文的学习后,相信大家对使用alibaba sentinel失败的问题有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4494662/blog/5026874