fix:自定义异常

上级 1fdf90cf
...@@ -5,7 +5,6 @@ import cn.hutool.core.date.StopWatch; ...@@ -5,7 +5,6 @@ import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
...@@ -19,7 +18,6 @@ import java.util.List; ...@@ -19,7 +18,6 @@ import java.util.List;
* @version : 2.2.0 * @version : 2.2.0
* @date : 2023/2/8 10:45 * @date : 2023/2/8 10:45
*/ */
@Slf4j
public class FileContains { public class FileContains {
/** /**
......
...@@ -2,19 +2,6 @@ ...@@ -2,19 +2,6 @@
server: server:
port: 8085 port: 8085
#端点打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置 #spring配置
spring: spring:
application: application:
......
...@@ -78,6 +78,12 @@ ...@@ -78,6 +78,12 @@
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version> <version>2.0.0.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies> </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; ...@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.kwan.springcloudalibaba.api.FeignUserService;
import com.kwan.springcloudalibaba.common.Result; import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -43,12 +44,14 @@ public class SentinelController { ...@@ -43,12 +44,14 @@ public class SentinelController {
* 获取配置的变量 * 获取配置的变量
* http://localhost:8086/sentinel/nameInfo * http://localhost:8086/sentinel/nameInfo
*/ */
// @SentinelResource(value = "SentinelExceptionHandler",
// blockHandlerClass = SentinelExceptionHandler.class,
// blockHandler = "handleRRException")
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) @GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() { public Result nameInfo() {
return Result.ok(); return Result.ok();
} }
/** /**
* http://localhost:8086/sentinel/world * 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 @@ ...@@ -2,18 +2,6 @@
server: server:
port: 8086 port: 8086
#断电打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置 #spring配置
spring: spring:
application: application:
...@@ -32,4 +20,6 @@ spring: ...@@ -32,4 +20,6 @@ spring:
refresh-enabled: true #默认自动刷新 refresh-enabled: true #默认自动刷新
sentinel: sentinel:
transport: transport:
dashboard: 120.79.36.53:8181 dashboard: 127.0.0.1:8181
\ No newline at end of file port: 8719 #默认端口,如果被占用则从8719依次+1扫描
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册