前言
sentinel 是面向分布式服务架构的流量管制组件,次要以流量为切入点,从限流、流量整形、熔断降级、零碎负载爱护、热点防护等多个维度来帮忙开发者保障微服务的稳定性。自从hytrix 2018年进入保护状态,再到springcloud 2020.0版本hytrix被移除,就能够预想将来一段时间springcloud全家桶的熔断降级组件基本上的首选就是alibaba sentinel。
明天就来聊聊因不失当应用alibaba sentinel,而导致熔断降级生效的一些例子。因为sentinel还在一直迭代更新中,不同版本会有一些差别,而且在版本的迭代中,有些问题可能也曾经修复。
本文演示的版本应用的sentinel-dashboard是1.8.0。应用springcloud alibaba的版本为2.2.3.RELEASE
生效场景例子
1、降级不失效问题
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@Slf4jpublic 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; }}
2、受权规定不失效问题
a、起因剖析
我的项目中没有实现
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser
接口,导致无奈解析申请起源
b、解决方案
在我的项目中自定义申请起源解析器。示例代码如下
** * @description: 解析拜访起源,用于受权规定--黑白名单。 * 当要进行受权规定时,则必须配置RequestOriginParser,否则受权规定无奈失效 * **/@Componentpublic 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(); }}
3、热点规定不失效问题
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可能遇到的问题,不得说下阿里在国内开源做的真的挺好的,大部分问题在官网issue都能找到解答
文章下方的demo链接,提供其余熔断降级例子以及基于文件长久熔断降级配置的性能例子。感兴趣的敌人能够蛮看看。
demo链接
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-sentinel