Search in sources :

Example 1 with DegradeException

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 2 with DegradeException

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();
    }
}
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 3 with DegradeException

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();
}
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 DegradeException

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;
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)

Example 5 with DegradeException

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));
}
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

DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)6 AuthorityException (com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)4 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)4 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)3 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)3 PrintWriter (java.io.PrintWriter)2 Entry (com.alibaba.csp.sentinel.Entry)1 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)1 AjaxResult (com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult)1 AjaxResult (com.github.lybgeek.common.model.AjaxResult)1 OutputStream (java.io.OutputStream)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)1