use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException 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.flow.param.ParamFlowException in project Sentinel by alibaba.
the class GatewayFlowSlot method checkGatewayParamFlow.
private void checkGatewayParamFlow(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
if (args == null) {
return;
}
List<ParamFlowRule> rules = GatewayRuleManager.getConvertedParamRules(resourceWrapper.getName());
if (rules == null || rules.isEmpty()) {
return;
}
for (ParamFlowRule rule : rules) {
// Initialize the parameter metrics.
ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
String triggeredParam = "";
if (args.length > rule.getParamIdx()) {
Object value = args[rule.getParamIdx()];
triggeredParam = String.valueOf(value);
}
throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
}
}
}
use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException 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.flow.param.ParamFlowException 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