Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

Question about CommonFilter

Open hosaos opened this issue 7 years ago • 4 comments

Issue Description

Type: bug report or feature request

Describe what happened (or what feature you want)

@sczyh30 i try to integrate spring-cloud-starter-alibaba-sentinel(0.1.2.RELEASE) to test the FlowControl function,in my program, i wrote test code like this

   @SentinelResource(value ="clusterFlowControl")
public int clusterFlowControlTest(int i)  throws BlockException{
    return i;
}

and the flow control works。

but i try to set all the request blocked by Sentinel a common response,so i wrote my own UrlBlockHandler and register it to WebCallbackManager。

the situation is that i found that in com.alibaba.csp.sentinel.adapter.servlet.CommonFilter,when flow control happens,the BlockException was wrapped by FrameworkServlet,so the BlockException changed to NestedServletException,only BlockException will be catched and use UrlBlockHandler to customize a response, so my own UrlBlockHandler does not work

spring-webmvc-4.3.13.RELEASE-sources.jar!/org/springframework/web/servlet/FrameworkServlet.java:980

    try {
        doService(request, response);
	}
	catch (ServletException ex) {
		failureCause = ex;
		throw ex;
	}
	catch (IOException ex) {
		failureCause = ex;
		throw ex;
	}
	catch (Throwable ex) {
                     // BlockException was wrapped here
		failureCause = ex;
		throw new NestedServletException("Request processing failed", ex);
	}

it might be a problem of excution orders of filters and servlets,and i want to know how to solve this problem

Describe what you expected to happen

there will be BlockException catched in com.alibaba.csp.sentinel.adapter.servlet.CommonFilter ,not NestedServletException

How to reproduce it (as minimally and precisely as possible)

Tell us your environment

spring-cloud-starter-alibaba-sentinel(0.1.2.RELEASE) springboot(1.5.9.RELEASE)

Anything else we need to know?

hosaos avatar Jul 16 '19 08:07 hosaos

Using CommonFilter of sentinel-web-servlet, each incoming different URL will be treated as a different resource, so we may needn't use @SentinelResource.

If use @SentinelResource, seems no need to use CommonFilter, suggest use blockHandler / blockHandlerClass, fallback, defaultFallback to do custorm block logic. @see https://github.com/alibaba/Sentinel/wiki/注解支持#sentinelresource-注解

If use them together, the BlockException thrown by aop of @SentinelResource will be firstly catch by FrameworkServlet of SpringMVC, and it wrapped to NestedServletException(which is not RuntimeException), and UrlBlockHandler take no effect.

cdfive avatar Jul 16 '19 13:07 cdfive

Using CommonFilter of sentinel-web-servlet, each incoming different URL will be treated as a different resource, so we may needn't use @SentinelResource.

If use @SentinelResource, seems no need to use CommonFilter, suggest use blockHandler / blockHandlerClass, fallback, defaultFallback to do custorm block logic. @see https://github.com/alibaba/Sentinel/wiki/注解支持#sentinelresource-注解

If use them together, the BlockException thrown by aop of @SentinelResource will be firstly catch by FrameworkServlet of SpringMVC, and it wrapped to NestedServletException(which is not RuntimeException), and UrlBlockHandler take no effect.

thanks for your anwser,so if i use CommonFilter,and i want to limit the Qps of some method , the resouceName (which i config in the sentinel dashboard) will be exactly the method name?

for example , the method is

 public int clusterFlowControlTest(int i)  throws BlockException{
     return i;
 }

and resouceName will be clusterFlowControlTest ?

hosaos avatar Jul 16 '19 16:07 hosaos

CommonFilter is used for web servlet adaptation, e.g. If use SpringMVC, the full path declared in @RequestMapping of Controller will be marked as resourceName, other methods will not be treated as resource.

cdfive avatar Jul 17 '19 01:07 cdfive

CommonFilter is used for web servlet adaptation, e.g. If use SpringMVC, the full path declared in @RequestMapping of Controller will be marked as resourceName, other methods will not be treated as resource.

thanks and i will have a try

hosaos avatar Jul 17 '19 02:07 hosaos