Sentinel 1.8.0 自定義配置限流/流控返回異常資訊
阿新 • • 發佈:2022-03-30
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