fix:自定义异常

上级 1fdf90cf
......@@ -5,7 +5,6 @@ import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
......@@ -19,7 +18,6 @@ import java.util.List;
* @version : 2.2.0
* @date : 2023/2/8 10:45
*/
@Slf4j
public class FileContains {
/**
......
......@@ -2,19 +2,6 @@
server:
port: 8085
#端点打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置
spring:
application:
......
......@@ -78,6 +78,12 @@
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
......
package com.kwan.springcloudalibaba.constant;
public class RespConstant {
public static final Integer RESP_CODE_FLOW_LIMITING_EXCEPTION = 441;
public static final String RESP_MSG_FLOW_LIMITING_EXCEPTION = "流量控制";
}
......@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.kwan.springcloudalibaba.api.FeignUserService;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
......@@ -43,12 +44,14 @@ public class SentinelController {
* 获取配置的变量
* http://localhost:8086/sentinel/nameInfo
*/
// @SentinelResource(value = "SentinelExceptionHandler",
// blockHandlerClass = SentinelExceptionHandler.class,
// blockHandler = "handleRRException")
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok();
}
/**
* http://localhost:8086/sentinel/world
*
......
package com.kwan.springcloudalibaba.handler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class UrlBlockHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
String msg = null;
if (e instanceof FlowException) {//限流异常
msg = "接口已被限流";
} else if (e instanceof DegradeException) {//熔断异常
msg = "接口已被熔断,请稍后再试";
} else if (e instanceof ParamFlowException) { //热点参数限流
msg = "热点参数限流";
} else if (e instanceof SystemBlockException) { //系统规则异常
msg = "系统规则(负载不满足要求)";
} else if (e instanceof AuthorityException) { //授权规则异常
msg = "授权规则不通过";
}
httpServletResponse.setStatus(500);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json;charset=utf-8");
//ObjectMapper是内置Jackson的序列化工具类,这用于将对象转为JSON字符串
ObjectMapper mapper = new ObjectMapper();
//某个对象属性为null时不进行序列化输出
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.writeValue(httpServletResponse.getWriter(),
Result.error(300, msg)
);
}
}
\ No newline at end of file
......@@ -2,18 +2,6 @@
server:
port: 8086
#断电打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置
spring:
application:
......@@ -32,4 +20,6 @@ spring:
refresh-enabled: true #默认自动刷新
sentinel:
transport:
dashboard: 120.79.36.53:8181
\ No newline at end of file
dashboard: 127.0.0.1:8181
port: 8719 #默认端口,如果被占用则从8719依次+1扫描
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册