Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems with the failure to use alibaba sentinel

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the problems of failure in using alibaba sentinel". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the problems of failure in using alibaba sentinel.

Preface

Sentinel is a flow control component oriented to distributed service architecture, which mainly takes traffic as a starting point to help developers ensure the stability of micro-services from many dimensions, such as current limit, traffic shaping, circuit breaker degradation, system load protection, hot spot protection and so on. Since hytrix went into maintenance in 2018 and the springcloud 2020.0 version of hytrix has been removed, it can be expected that for some time in the future, alibaba sentinel will basically be the first choice for the fuse downgrade components of springcloud buckets.

Failure scenario example 1. The problem that the downgrade does not take effect

A, cause analysis

Custom global exception handling is used in the project, and statistics on the number of exceptions or the proportion of exceptions are found in the

Com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion

This method executes, and custom global exception handling precedes the

Com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion

This method executes because we have already handled the exception in the global exception, such as converting it to an object, so that AbstractSentinelInterceptor.afterCompletion cannot get the exception and cannot count the number or proportion of the exception.

B, solution

In the official issue, some netizens have put forward solutions to https://github.com/alibaba/Sentinel/issues/1281 and https://github.com/alibaba/Sentinel/issues/404.

Because I am in the search for issue before, through the source code tracking, to find the answer, here I would like to achieve the idea. My idea is to define a section and do exception statistics in the AfterThrowing of the section. Because the section is executed before the global exception. I copy the source code of sentinel statistics directly. The core code is as follows.

@ 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);} / * * Statistical exception * @ 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. Authorization rules do not take effect

A, cause analysis

No implementation in the project

Com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser

Interface, so that the source of the request cannot be resolved

B, solution

Customize the request source parser in the project. The sample code is as follows

* * @ description: resolve the access source, which is used for authorization rules-blacklist and whitelist. * RequestOriginParser must be configured when authorization rules are to be implemented, otherwise authorization rules will not take effect * * / @ Componentpublic class CustomRequestOriginParser implements RequestOriginParser {@ Override public String parseOrigin (HttpServletRequest request) {String origin = request.getParameter ("origin") If (! StringUtils.isEmpty (origin)) {/ / depending on whether the API carries the origin parameter, if the parameter is origin=pc, / / and the sentinel-dashbord authorization rule is set, and the source is set to pc, it means that the request source is pc, and the blacklist and whitelist configuration is return origin. } / / if no request parameter is not carried by the API, it means that the blacklist and whitelist is set by ip to set return request.getRemoteAddr ();}} 3. The hot spot rule does not take effect.

A, cause analysis

If the web burial site uses url as the resource name, the rule will not take effect.

B, solution

Use the name defined by the @ SentinelResource annotation as the resource name

Refer to the official issue https://github.com/alibaba/Sentinel/issues/1734

After configuring the hotspot rule configuration @ SentinelResource, it may also appear

Java.lang.reflect.UndeclaredThrowableException: null

Solution: you need to add throws BlockException or blockHandler to the method to handle exceptions

Refer to the 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 rules") @ SentinelResource (value= "testParamFlowRule") public AjaxResult testParamFlowRule (@ PathVariable ("msg") String msg) throws BlockException {System.out.println (String.format ("msg:% s", msg)) Return AjaxResult.success ("Test Hotspot rules");} Thank you for reading, the above is the content of "what are the problems of failure in using alibaba sentinel". After the study of this article, I believe you have a deeper understanding of the problem of failure in using alibaba sentinel, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 270

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report