use of com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping in project springboot-learning by lyb-geek.
the class AbstractCircuitBreakerAspectSupport method extractDefaultFallbackMethod.
private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defaultFallback, Class<?>[] locationClass) {
if (StringUtil.isBlank(defaultFallback)) {
CircuitBreakerMapping annotationClass = pjp.getTarget().getClass().getAnnotation(CircuitBreakerMapping.class);
if (annotationClass != null && StringUtil.isNotBlank(annotationClass.defaultFallback())) {
defaultFallback = annotationClass.defaultFallback();
if (locationClass == null || locationClass.length < 1) {
locationClass = annotationClass.fallbackClass();
}
} else {
return null;
}
}
boolean mustStatic = locationClass != null && locationClass.length >= 1;
Class<?> clazz = mustStatic ? locationClass[0] : pjp.getTarget().getClass();
MethodWrapper m = ResourceMetadataRegistry.lookupDefaultFallback(clazz, defaultFallback);
if (m == null) {
// First time, resolve the default fallback.
Class<?> originReturnType = resolveMethod(pjp).getReturnType();
// Default fallback allows two kinds of parameter list.
// One is empty parameter list.
Class<?>[] defaultParamTypes = new Class<?>[0];
// The other is a single parameter {@link Throwable} to get relevant exception info.
Class<?>[] paramTypeWithException = new Class<?>[] { Throwable.class };
// We first find the default fallback with empty parameter list.
Method method = findMethod(mustStatic, clazz, defaultFallback, originReturnType, defaultParamTypes);
// If default fallback with empty params is absent, we then try to find the other one.
if (method == null) {
method = findMethod(mustStatic, clazz, defaultFallback, originReturnType, paramTypeWithException);
}
// Cache the method instance.
ResourceMetadataRegistry.updateDefaultFallbackFor(clazz, defaultFallback, method);
return method;
}
if (!m.isPresent()) {
return null;
}
return m.getMethod();
}
use of com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping 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();
}
}
use of com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping in project springboot-learning by lyb-geek.
the class CircuitBreakerMappingHandlerMapping method createRequestMappingInfo.
@Nullable
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
CircuitBreakerMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, CircuitBreakerMapping.class);
RequestCondition<?> condition = element instanceof Class ? this.getCustomTypeCondition((Class) element) : this.getCustomMethodCondition((Method) element);
return requestMapping != null ? this.createRequestMappingInfo(requestMapping, condition) : null;
}
Aggregations