其他分享
首页 > 其他分享> > Sentinel 1.8.0 自定义配置限流/流控返回异常信息

Sentinel 1.8.0 自定义配置限流/流控返回异常信息

作者:互联网

Sentinel version 1.8.0 (which not exists WebCallbackManager or UrlBlockHandler )

✅ correct demonstation

pom.xml import

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-webmvc-adapter</artifactId>
    <version>x.y.z</version>
</dependency>

override interface


/**
 * 秒杀限流异常处理程序
 *
 * @author Miozus
 * @date 2022/03/30
 */
@Configuration
public class SeckillBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        R error = R.error(BizCodeEnum.TOO_MANY_REQUESTS);
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.getWriter().write(JSONUtil.toJsonStr(error));
    }
}

response:

{"msg":"请求流量过大","code":10003}

❌ incorrected demonstration

addInterceptors

addInterceptors

Not Exists field cause of SentinelWebMvcConfig does not exist blockExceptionHandler which belongs to its father Object (BaseWebMvcConfig).

public class SentinelWebMvcConfig extends BaseWebMvcConfig {

    public static final String DEFAULT_REQUEST_ATTRIBUTE_NAME = "$$sentinel_spring_web_entry_attr";

    /**
     * Specify the URL cleaner that unifies the URL resources.
     */
    private UrlCleaner urlCleaner;

    /**
     * Specify whether the URL resource name should contain the HTTP method prefix (e.g. {@code POST:}).
     */
    private boolean httpMethodSpecify;

    /**
     * Specify whether unify web context(i.e. use the default context name), and is true by default.
     *
     * @since 1.7.2
     */
    private boolean webContextUnify = true;

//...

The offical document I have read , it is helpful for testing but not accuracy.

The UrlBlockHandler was not changed. If you're using sentinel-spring-webmvc-adapter, you may refer to: https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-spring-webmvc-adapter

标签:自定义,流控,URL,spring,adapter,public,限流,sentinel,response
来源: https://www.cnblogs.com/miozus/p/16076417.html