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
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 usingsentinel-spring-webmvc-adapter
, you may refer to: https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-spring-webmvc-adapter
本文来自博客园,作者:miozus,转载请注明原文链接:https://www.cnblogs.com/miozus/p/16076417.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具