提交 4c590ec1 authored 作者: 陈世营's avatar 陈世营

[修改] timeloit 自定义异常

上级 513f08c9
......@@ -18,6 +18,7 @@ package com.alibaba.cloud.sentinel;
import java.util.Optional;
import com.alibaba.cloud.sentinel.timeloit.CustomBlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler;
......@@ -108,8 +109,11 @@ public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
e) -> response.sendRedirect(properties.getBlockPage())));
}
else {
// sentinelWebMvcConfig
// .setBlockExceptionHandler(new DefaultBlockExceptionHandler());
// for loitTime custom sentinel exception by chenshiying
sentinelWebMvcConfig
.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
.setBlockExceptionHandler(new CustomBlockExceptionHandler());
}
}
......
package com.alibaba.cloud.sentinel.timeloit;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class CustomBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
response.setStatus(HttpStatus.OK.value());
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("{\"success\":false,\"code\":\"444\",\"msg\":\"系统繁忙请稍后再试\"}");
out.flush();
out.close();
}
}
# 时代凌宇sentinel 自定义异常类
SentinelWebAutoConfiguration.sentinelWebMvcConfig
```$xslt
// for loitTime custom sentinel exception by chenshiying
sentinelWebMvcConfig
.setBlockExceptionHandler(new CustomBlockExceptionHandler());
```
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论