alibaba / Sentinel

A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)
https://sentinelguard.io/
Apache License 2.0
22.32k stars 8k forks source link

How can I config exception num by degradeRule with sentinel-spring-webmvc-adapter #3141

Open 24kpure opened 1 year ago

24kpure commented 1 year ago

com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor#preHandle can't feel business exception, I config exception num by degradeRule,but it doesn't work.

bchengwang commented 1 year ago

com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor#preHandle感觉不到业务异常,我通过degraderRule配置了异常num,但是没有用。

你是不是使用里全局异常处理器?如果使用了全局异常处理器那么异常会被处理器拦截导致AbstractSentinelInterceptor拦截器无法获取到异常信息。

24kpure commented 1 year ago

Nowadays,does it exist that webapplication runs without globalExceptionHandler? I try to extend SentinelWebInterceptor and override mehtod traceExceptionAndExit and make exception visible for sentinel, at last ,it works.

bchengwang commented 1 year ago

如今,是否存在 webapplication 运行时没有globalExceptionHandler?我尝试扩展 SentinelWebInterceptor和覆盖 mehtodtraceExceptionAndExit并使异常对哨兵可见,最后,它起作用了。

可以发一下你重写的SentinelWebInterceptor的代码吗?让我参考一下。谢谢

24kpure commented 1 year ago
  protected void traceExceptionAndExit(Entry entry, Exception ex) {
        if (entry == null) {
            return;
        }
        if (ex == null) {
            HttpServletRequest httpServletRequest = SerialNoUtils.getHttpServletRequest();
            if (httpServletRequest == null) {
                return;
            }

           // you have to make exception visible for sentinel, my plan is quite simple,just put it into request.
            ex = (Exception) httpServletRequest.getAttribute(requestRefExceptionName);
        }

        if (ex != null) {
            Tracer.traceEntry(ex, entry);
        }
        entry.exit();
    }
24kpure commented 1 year ago

3149 @bchengwang you can have a try ~