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

[修改]sentinel 自定义默认异常

上级 dd18ffee
......@@ -16,17 +16,15 @@
package com.timeloit.cloud.sentinel;
import java.util.Optional;
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;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
import com.timeloit.cloud.sentinel.loit.CustomBlockExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
......@@ -39,6 +37,8 @@ import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Optional;
/**
* @author xiaojing
* @author yuhuangbin
......@@ -68,6 +68,7 @@ public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
@Autowired
private Optional<SentinelWebInterceptor> sentinelWebInterceptorOptional;
@Override
public void addInterceptors(InterceptorRegistry registry) {
if (!sentinelWebInterceptorOptional.isPresent()) {
......@@ -100,15 +101,17 @@ public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
if (blockExceptionHandlerOptional.isPresent()) {
blockExceptionHandlerOptional
.ifPresent(sentinelWebMvcConfig::setBlockExceptionHandler);
}
else {
} else {
if (StringUtils.hasText(properties.getBlockPage())) {
sentinelWebMvcConfig.setBlockExceptionHandler(((request, response,
e) -> response.sendRedirect(properties.getBlockPage())));
}
else {
} else {
// sentinelWebMvcConfig
// .setBlockExceptionHandler(new DefaultBlockExceptionHandler());
// for loitTime custom sentinel exception by chenshiying
sentinelWebMvcConfig
.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
.setBlockExceptionHandler(new CustomBlockExceptionHandler());
}
}
......
package com.timeloit.cloud.sentinel.loit;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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());
System.out.println(MediaType.APPLICATION_JSON.toString());
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论