Search in sources :

Example 1 with ParamFlowException

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();
    }
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException) PrintWriter(java.io.PrintWriter)

Example 2 with ParamFlowException

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);
        }
    }
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)

Example 3 with ParamFlowException

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();
}
Also used : AjaxResult(com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult) OutputStream(java.io.OutputStream) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)

Example 4 with ParamFlowException

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));
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)

Aggregations

ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)4 AuthorityException (com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)3 DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)3 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)3 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)1 AjaxResult (com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1