Search in sources :

Example 6 with EntryType

use of com.alibaba.csp.sentinel.EntryType in project springboot-learning by lyb-geek.

the class CircuitBreakerAspect method invokeResourceWithSentinel.

@Around("@annotation(circuitBreakerMapping)")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp, CircuitBreakerMapping circuitBreakerMapping) throws Throwable {
    Method originMethod = resolveMethod(pjp);
    CircuitBreakerMapping controllerCircuitBreakerMapping = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), CircuitBreakerMapping.class);
    String baseResouceName = "lybgeek:";
    if (circuitBreakerMapping != null) {
        baseResouceName = baseResouceName + controllerCircuitBreakerMapping.value()[0];
    }
    baseResouceName = baseResouceName + circuitBreakerMapping.value()[0];
    String resourceName = getResourceName(baseResouceName, originMethod);
    EntryType entryType = circuitBreakerMapping.entryType();
    int resourceType = circuitBreakerMapping.resourceType();
    Entry entry = null;
    try {
        String contextName = "lybgeek_circuitbreaker_context";
        RequestOriginParser parser = SpringUtil.getBean(RequestOriginParser.class);
        ContextUtil.enter(contextName, parser.parseOrigin(getRequest()));
        entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
        Object result = pjp.proceed();
        return result;
    } catch (BlockException ex) {
        return handleBlockException(pjp, circuitBreakerMapping, ex);
    } catch (Throwable ex) {
        Class<? extends Throwable>[] exceptionsToIgnore = circuitBreakerMapping.exceptionsToIgnore();
        // The ignore list will be checked first.
        if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
            throw ex;
        }
        if (exceptionBelongsTo(ex, circuitBreakerMapping.exceptionsToTrace())) {
            traceException(ex, circuitBreakerMapping);
            return handleFallback(pjp, circuitBreakerMapping, ex);
        }
        // No fallback function can handle the exception, so throw it out.
        throw ex;
    } finally {
        if (entry != null) {
            entry.exit(1, pjp.getArgs());
        }
        ContextUtil.exit();
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) RequestOriginParser(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser) Method(java.lang.reflect.Method) CircuitBreakerMapping(com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Aggregations

EntryType (com.alibaba.csp.sentinel.EntryType)6 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)5 Entry (com.alibaba.csp.sentinel.Entry)4 SentinelResource (com.alibaba.csp.sentinel.annotation.SentinelResource)2 Method (java.lang.reflect.Method)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 Around (org.aspectj.lang.annotation.Around)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 SphU (com.alibaba.csp.sentinel.SphU)1 Tracer (com.alibaba.csp.sentinel.Tracer)1 RequestOriginParser (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser)1 Context (com.alibaba.csp.sentinel.context.Context)1 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)1 CircuitBreakerMapping (com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AroundInvoke (javax.interceptor.AroundInvoke)1 Mono (reactor.core.publisher.Mono)1