use of com.alibaba.csp.sentinel.annotation.SentinelResource in project Sentinel by alibaba.
the class SentinelResourceAspect method invokeResourceWithSentinel.
@Around("sentinelResourceAnnotationPointcut()")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {
Method originMethod = resolveMethod(pjp);
SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
if (annotation == null) {
// Should not go through here.
throw new IllegalStateException("Wrong state for SentinelResource annotation");
}
String resourceName = getResourceName(annotation.value(), originMethod);
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
return pjp.proceed();
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
return handleFallback(pjp, annotation, ex);
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, pjp.getArgs());
}
}
}
use of com.alibaba.csp.sentinel.annotation.SentinelResource in project Sentinel by alibaba.
the class AbstractSentinelInterceptorSupport method extractDefaultFallbackMethod.
private Method extractDefaultFallbackMethod(InvocationContext ctx, String defaultFallback, Class<?>[] locationClass) {
if (StringUtil.isBlank(defaultFallback)) {
SentinelResource annotationClass = ctx.getTarget().getClass().getAnnotation(SentinelResource.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] : ctx.getTarget().getClass();
MethodWrapper m = ResourceMetadataRegistry.lookupDefaultFallback(clazz, defaultFallback);
if (m == null) {
// First time, resolve the default fallback.
Class<?> originReturnType = resolveMethod(ctx).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.alibaba.csp.sentinel.annotation.SentinelResource in project jboot by yangfuhai.
the class SentinelInterceptor method intercept.
@Override
public void intercept(Invocation inv) {
SentinelResource annotation = inv.getMethod().getAnnotation(SentinelResource.class);
if (annotation == null) {
inv.invoke();
return;
}
String resourceName = getResourceName(annotation.value(), inv.getMethod());
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, inv.getArgs());
inv.invoke();
} catch (BlockException ex) {
try {
inv.setReturnValue(handleBlockException(inv, annotation, ex));
} catch (Throwable throwable) {
if (inv.isActionInvocation()) {
inv.getController().renderText("Blocked by Sentinel " + ex.getRule());
} else {
throwable.printStackTrace();
}
}
return;
} catch (Throwable ex) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
try {
inv.setReturnValue(handleFallback(inv, annotation, ex));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return;
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, inv.getArgs());
}
}
}
use of com.alibaba.csp.sentinel.annotation.SentinelResource in project flash-sale by ThoughtsBeta.
the class FlashItemController method getOnlineFlashItems.
@GetMapping(value = "/activities/{activityId}/flash-items/online")
@SentinelResource("GetOnlineFlashItems")
public MultiResponse<FlashItemResponse> getOnlineFlashItems(@RequestAttribute Long userId, @PathVariable Long activityId, @RequestParam Integer pageSize, @RequestParam Integer pageNumber, @RequestParam(required = false) String keyword) {
FlashItemsQuery flashItemsQuery = new FlashItemsQuery().setKeyword(keyword).setPageSize(pageSize).setPageNumber(pageNumber).setStatus(FlashItemStatus.ONLINE.getCode());
AppMultiResult<FlashItemDTO> flashItemsResult = flashItemAppService.getFlashItems(userId, activityId, flashItemsQuery);
if (!flashItemsResult.isSuccess() || flashItemsResult.getData() == null) {
return ResponseBuilder.withMulti(flashItemsResult);
}
return MultiResponse.of(toFlashItemsResponse(flashItemsResult.getData()), flashItemsResult.getTotal());
}
use of com.alibaba.csp.sentinel.annotation.SentinelResource in project flash-sale by ThoughtsBeta.
the class FlashOrderController method placeOrder.
@PostMapping(value = "/flash-orders")
@SentinelResource("PlaceOrderResource")
public SingleResponse<PlaceOrderResult> placeOrder(@RequestAttribute Long userId, @RequestBody FlashPlaceOrderRequest flashPlaceOrderRequest) {
FlashPlaceOrderCommand placeOrderCommand = FlashOrderBuilder.toCommand(flashPlaceOrderRequest);
AppSimpleResult<PlaceOrderResult> placeOrderResult = flashOrderAppService.placeOrder(userId, placeOrderCommand);
if (!placeOrderResult.isSuccess() || placeOrderResult.getData() == null) {
return ResponseBuilder.withSingle(placeOrderResult);
}
return SingleResponse.of(placeOrderResult.getData());
}
Aggregations