Search in sources :

Example 1 with SentinelResource

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());
        }
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Example 2 with SentinelResource

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();
}
Also used : SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource) Method(java.lang.reflect.Method)

Example 3 with SentinelResource

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

Example 4 with SentinelResource

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());
}
Also used : FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) FlashItemsQuery(com.actionworks.flashsale.app.model.query.FlashItemsQuery) GetMapping(org.springframework.web.bind.annotation.GetMapping) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource)

Example 5 with SentinelResource

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());
}
Also used : PlaceOrderResult(com.actionworks.flashsale.app.model.result.PlaceOrderResult) FlashPlaceOrderCommand(com.actionworks.flashsale.app.model.command.FlashPlaceOrderCommand) PostMapping(org.springframework.web.bind.annotation.PostMapping) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource)

Aggregations

SentinelResource (com.alibaba.csp.sentinel.annotation.SentinelResource)11 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)3 Method (java.lang.reflect.Method)3 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)2 FlashActivitiesQuery (com.actionworks.flashsale.app.model.query.FlashActivitiesQuery)2 FlashItemsQuery (com.actionworks.flashsale.app.model.query.FlashItemsQuery)2 Entry (com.alibaba.csp.sentinel.Entry)2 EntryType (com.alibaba.csp.sentinel.EntryType)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)2 FlashPlaceOrderCommand (com.actionworks.flashsale.app.model.command.FlashPlaceOrderCommand)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 Person (com.xiaolyuh.entity.Person)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1