use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeException in project flash-sale by ThoughtsBeta.
the class SentinelExceptionHandler method handleConflict.
@ExceptionHandler(value = { UndeclaredThrowableException.class })
protected ResponseEntity<Object> handleConflict(UndeclaredThrowableException ex, WebRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse();
if (ex.getUndeclaredThrowable() instanceof FlowException) {
exceptionResponse.setErrorCode(LIMIT_BLOCK.getCode());
exceptionResponse.setErrorMessage(LIMIT_BLOCK.getDesc());
}
if (ex.getUndeclaredThrowable() instanceof DegradeException) {
exceptionResponse.setErrorCode(DEGRADE_BLOCK.getCode());
exceptionResponse.setErrorMessage(DEGRADE_BLOCK.getDesc());
}
logger.error("expectedException|预期错误|{},{}", ex.getMessage(), ex);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.BAD_REQUEST, request);
}
use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeException in project java-demos by powerLeePlus.
the class CustomUrlBlockHandler method handle.
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
String msg = null;
if (ex instanceof FlowException) {
msg = "限流了";
} else if (ex instanceof DegradeException) {
msg = "降级了";
} else if (ex instanceof ParamFlowException) {
msg = "热点参数限流";
} else if (ex instanceof SystemBlockException) {
msg = "系统规则(负载/...不满足要求)";
} else if (ex instanceof AuthorityException) {
msg = "授权规则不通过";
}
// http状态码
response.setStatus(500);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "application/json;charset=utf-8");
response.setContentType("application/json;charset=utf-8");
try (PrintWriter writer = response.getWriter()) {
writer.write(msg);
writer.flush();
}
}
use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeException in project springboot-learning by lyb-geek.
the class DefaultBlockExceptionHandler method handle.
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
response.setStatus(200);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
StringBuffer url = request.getRequestURL();
if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {
url.append("?").append(request.getQueryString());
}
String msg = "Blocked by Sentinel Flow Limit";
log.error("sentinel flow limit url:{}", url);
if (e instanceof AuthorityException) {
msg = "Blocked by Sentinel Authority Limit";
} else if (e instanceof SystemBlockException) {
msg = "Blocked by Sentinel System Limit";
} else if (e instanceof DegradeException) {
msg = "Blocked by Sentinel Degrade Limit";
} else if (e instanceof ParamFlowException) {
msg = "Blocked by Sentinel ParamFlow Limit";
}
AjaxResult result = new AjaxResult();
result.setSuccess(false);
result.setCode(429);
result.setMessage(msg);
OutputStream out = response.getOutputStream();
out.write(JSON.toJSONString(result).getBytes("utf-8"));
out.flush();
out.close();
}
use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeException in project study-by-myself by Howinfun.
the class SentinelCoreHelloController method sayHelloByDubbo2Code.
/**
* 接入Sentinel方式:植入硬代码进行熔断限流
* @author winfun
* @param name name
* @return {@link ApiResult<String> }
*/
private ApiResult<String> sayHelloByDubbo2Code(final String name) {
ApiResult<String> result;
Entry entry = null;
// 务必保证 finally 会被执行
try {
entry = SphU.entry(RESOURCE_NAME);
result = this.dubboServiceOne.sayHello(name);
} catch (BlockException e) {
if (e instanceof DegradeException) {
log.error("资源:{} 被熔断了,message is {}", RESOURCE_NAME, e.getMessage());
result = ApiResult.fail("hello fallback");
} else {
log.error("资源:{} 被流控了", RESOURCE_NAME);
result = ApiResult.fail("hello block");
}
} catch (Exception e) {
log.error("资源:{} 发生异常,message is {}", RESOURCE_NAME, e.getMessage());
// 若需要配置降级规则,需要通过这种方式记录业务异常
Tracer.traceEntry(e, entry);
result = ApiResult.fail("exception");
// throw new RuntimeException("业务处理发生异常");
} finally {
// 务必保证 exit,务必保证每个 entry 与 exit 配对
if (entry != null) {
entry.exit();
}
}
return result;
}
use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeException in project amusing-project by m-lvqingyu.
the class GatewayBlockExceptionHandler method handle.
@Override
public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
ServerHttpResponse response = serverWebExchange.getResponse();
setProps(response);
ServerHttpRequest request = serverWebExchange.getRequest();
String path = request.getURI().getPath();
// 限流
if (throwable instanceof FlowException) {
log.error("[gateway-sentinel]-request is flow, path:{}", path);
return responseWrite(response, ApiResult.result(CommCode.FLOW_ERROR));
}
// 熔断
if (throwable instanceof DegradeException) {
log.error("[gateway-sentinel]-request is degrade, path:{}", path);
return responseWrite(response, ApiResult.result(CommCode.DEGRADE_ERROR));
}
// 热点参数限流
if (throwable instanceof ParamFlowException) {
log.error("[gateway-sentinel]-request is paramFlow, path:{}", path);
return responseWrite(response, ApiResult.result(CommCode.PARAM_FLOW_ERROR));
}
// 系统保护规则
if (throwable instanceof SystemBlockException) {
log.error("[gateway-sentinel]-request is systemBlock, path:{}", path);
return responseWrite(response, ApiResult.result(CommCode.SYSTEM_BLOCK_ERROR));
}
// 授权规则
if (throwable instanceof AuthorityException) {
log.error("[gateway-sentinel]-request is authority, path:{}", path);
return responseWrite(response, ApiResult.result(CommCode.AUTHORITY_ERROR));
}
return responseWrite(response, ApiResult.result(CommCode.FREQUENT_OPERATION_EXCEPTION));
}
Aggregations