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

posted @   miozus  阅读(347)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示