01
preface
Sentinel is a flow control component for distributed service architecture. It mainly takes flow as the starting point to help developers ensure the stability of micro services from multiple dimensions such as current limiting, flow shaping, fuse degradation, system load protection and hotspot protection. Since hytrix entered the maintenance state in 2018, and then hytrix was removed in spring cloud 2020.0, it can be expected that alibaba sentinel is basically the first choice for the fuse degradation component of spring cloud in the future.
Today, let's talk about some examples of fuse degradation failure caused by improper use of alibaba sentinel. Because sentinel is still in iterative updating, there will be some differences between different versions, and some problems may have been fixed in the iteration of versions.
The sentinel dashboard used in the version demonstrated in this article is 1.8.0. The version of Alibaba using spring cloud is 2.2.3 RELEASE
02
Failure scenario example
Downgrade does not take effect
A
Cause analysis
Custom global exception handling is used in the project. The statistics of different constants or exception proportions are shown in
com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion
When this method is executed, the custom global exception will be handled before
com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion
This method executes because we have handled the exception in the global exception, such as converting it into an object, which leads to abstractsentinelinterceptor Aftercompletion is unable to get the exception, so it is unable to count the different constant or exception proportion
B
Solution
In the official issue, some netizens have put forward solutions
https://github.com/alibaba/Sentinel/issues/1281
https://github.com/alibaba/Sentinel/issues/404
Because I found the answer through source code tracking before checking the issue. Here is my implementation idea. My idea is to define a section and make exception statistics in AfterThrowing of the section. Because the slice is executed before the global exception. I directly copy the source of sentinel statistics. The core code is as follows
@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); } /** * Statistical anomaly * @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; } }
Non effectiveness of authorization rules
A
Cause analysis
Not implemented in project
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser
Interface, so the request source cannot be resolved
B
Solution
Customize the request source parser in your project. The example code is as follows
** * @description: Resolve access sources for authorization rules--Black and white list. * When authorization rules are to be, they must be configured RequestOriginParser,Otherwise, the authorization rule cannot take effect * **/ @Component public class CustomRequestOriginParser implements RequestOriginParser { @Override public String parseOrigin(HttpServletRequest request) { String origin = request.getParameter("origin"); if(!StringUtils.isEmpty(origin)){ //It depends on whether the interface carries the origin parameter. If the carried parameter is origin=pc, // When the source of the sentinel dashborder authorization rule is set to pc, it means that the request source is pc and the black-and-white list configuration is required return origin; } //If the interface does not carry the requested parameters, it means that the black-and-white list is set according to ip return request.getRemoteAddr(); } }
Hot spot rule does not take effect
A
Cause analysis
If the web buried point takes url as the resource name, the rule will not take effect
B
Solution
Take the name defined by the @ SentinelResource annotation as the resource name
Refer to official issue
https://github.com/alibaba/Sentinel/issues/1734
After configuring the hotspot rule and configuring @ SentinelResource, it may also appear
java.lang.reflect.UndeclaredThrowableException: null
Solution: you need to add throws BlockException or blockHandler in the method to handle exceptions
Refer to official issue
https://github.com/alibaba/Sentinel/issues/776
Sample code
@GetMapping(value = "/paramFlowRule/{msg}") @ApiImplicitParams({ @ApiImplicitParam(name="msg",defaultValue = "hello",value="information", paramType = "path"), }) @ApiOperation(value = "Test hotspot rule") @SentinelResource(value = "testParamFlowRule") public AjaxResult<String> testParamFlowRule(@PathVariable("msg") String msg) throws BlockException { System.out.println(String.format("msg : %s",msg)); return AjaxResult.success("Test hotspot rule"); }
03
summary
This article mainly introduces the common problems that may be encountered when using alibaba sentinel. It can not be said that Alibaba is really good at open source in China. Most of the problems can be solved in the official issue
The demo link at the bottom of the article provides other fuse degradation examples and function examples based on file persistent fuse degradation configuration. Interested friends can have a look.
04
demo link
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-sentinel