com.alibaba.csp.sentinel.annotation.SentinelResource Java Examples

The following examples show how to use com.alibaba.csp.sentinel.annotation.SentinelResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSentinelProccesser.java    From jboot with Apache License 2.0 6 votes vote down vote up
protected Object handleBlockException(Invocation inv, SentinelResource annotation, BlockException ex)
        throws Throwable {

    // Execute block handler if configured.
    Method blockHandlerMethod = extractBlockHandlerMethod(inv, annotation.blockHandler(),
            annotation.blockHandlerClass());
    if (blockHandlerMethod != null) {
        Object[] originArgs = inv.getArgs();
        // Construct args.
        Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);
        args[args.length - 1] = ex;
        try {
            if (isStatic(blockHandlerMethod)) {
                return blockHandlerMethod.invoke(null, args);
            }
            return blockHandlerMethod.invoke(inv.getTarget(), args);
        } catch (InvocationTargetException e) {
            // throw the actual exception
            throw e.getTargetException();
        }
    }

    // If no block handler is present, then go to fallback.
    return handleFallback(inv, annotation, ex);
}
 
Example #2
Source File: AbstractSentinelAspectSupport.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
protected Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex)
    throws Throwable {

    // Execute block handler if configured.
    Method blockHandlerMethod = extractBlockHandlerMethod(pjp, annotation.blockHandler(),
        annotation.blockHandlerClass());
    if (blockHandlerMethod != null) {
        Object[] originArgs = pjp.getArgs();
        // Construct args.
        Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);
        args[args.length - 1] = ex;
        try {
            if (isStatic(blockHandlerMethod)) {
                return blockHandlerMethod.invoke(null, args);
            }
            return blockHandlerMethod.invoke(pjp.getTarget(), args);
        } catch (InvocationTargetException e) {
            // throw the actual exception
            throw e.getTargetException();
        }
    }

    // If no block handler is present, then go to fallback.
    return handleFallback(pjp, annotation, ex);
}
 
Example #3
Source File: AbstractSentinelAspectSupport.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
protected Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex)
    throws Throwable {

    // Execute block handler if configured.
    Method blockHandlerMethod = extractBlockHandlerMethod(pjp, annotation.blockHandler(),
        annotation.blockHandlerClass());
    if (blockHandlerMethod != null) {
        Object[] originArgs = pjp.getArgs();
        // Construct args.
        Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);
        args[args.length - 1] = ex;
        if (isStatic(blockHandlerMethod)) {
            return blockHandlerMethod.invoke(null, args);
        }
        return blockHandlerMethod.invoke(pjp.getTarget(), args);
    }

    // If no block handler is present, then go to fallback.
    return handleFallback(pjp, annotation, ex);
}
 
Example #4
Source File: TestServiceImpl.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
@SentinelResource(value = "hello", fallback = "helloFallback")
public String hello(long s) {
    if (s < 0) {
        throw new IllegalArgumentException("invalid arg");
    }
    return String.format("Hello at %d", s);
}
 
Example #5
Source File: ProfileController.java    From MyShopPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 获取个人信息
 *
 * @param username {@code String} 用户名
 * @return {@link ResponseResult}
 */
@GetMapping(value = "info/{username}")
@SentinelResource(value = "info", fallback = "infoFallback", fallbackClass = ProfileControllerFallback.class)
public ResponseResult<UmsAdminDTO> info(@PathVariable String username) {
    UmsAdmin umsAdmin = umsAdminService.get(username);
    UmsAdminDTO dto = new UmsAdminDTO();
    BeanUtils.copyProperties(umsAdmin, dto);
    return new ResponseResult<UmsAdminDTO>(ResponseResult.CodeStatus.OK, "获取个人信息", dto);
}
 
Example #6
Source File: TestServiceImpl.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
@SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",
    exceptionsToIgnore = {IllegalStateException.class})
public String helloAnother(String name) {
    if (name == null || "bad".equals(name)) {
        throw new IllegalArgumentException("oops");
    }
    if ("foo".equals(name)) {
        throw new IllegalStateException("oops");
    }
    return "Hello, " + name;
}
 
Example #7
Source File: AbstractSentinelInterceptorSupport.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #8
Source File: AbstractSentinelAspectSupport.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
protected void traceException(Throwable ex, SentinelResource annotation) {
    Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
    // The ignore list will be checked first.
    if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
        return;
    }
    if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
        traceException(ex);
    }
}
 
Example #9
Source File: AbstractSentinelAspectSupport.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defaultFallback,
                                            Class<?>[] locationClass) {
    if (StringUtil.isBlank(defaultFallback)) {
        SentinelResource annotationClass = pjp.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] : 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();
}
 
Example #10
Source File: FooService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiFoo", blockHandler = "fooBlockHandler",
    exceptionsToTrace = {IllegalArgumentException.class})
public String foo(int i) throws Exception {
    if (i == 5758) {
        throw new IllegalAccessException();
    }
    if (i == 5763) {
        throw new IllegalArgumentException();
    }
    return "Hello for " + i;
}
 
Example #11
Source File: FooService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiFooWithFallback", blockHandler = "fooBlockHandler", fallback = "fooFallbackFunc",
    exceptionsToTrace = {IllegalArgumentException.class})
public String fooWithFallback(int i) throws Exception {
    if (i == 5758) {
        throw new IllegalAccessException();
    }
    if (i == 5763) {
        throw new IllegalArgumentException();
    }
    return "Hello for " + i;
}
 
Example #12
Source File: FooService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiAnotherFooWithDefaultFallback", defaultFallback = "globalDefaultFallback",
    fallbackClass = {FooUtil.class})
public String anotherFoo(int i) {
    if (i == 5758) {
        throw new IllegalArgumentException("oops");
    }
    return "Hello for " + i;
}
 
Example #13
Source File: FooService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler",
        exceptionsToIgnore = {IllegalMonitorStateException.class})
public String baz(String name) {
    if (name.equals("fail")) {
        throw new IllegalMonitorStateException("boom!");
    }
    return "cheers, " + name;
}
 
Example #14
Source File: BarService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiAnotherBarWithDefaultFallback", defaultFallback = "fallbackFunc")
public String anotherBar(int i) {
    if (i == 5758) {
        throw new IllegalArgumentException("oops");
    }
    return "Hello for " + i;
}
 
Example #15
Source File: BarService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@SentinelResource()
public String doSomething(int i) {
    if (i == 5758) {
        throw new IllegalArgumentException("oops");
    }
    return "do something";
}
 
Example #16
Source File: ConsumerDemoController.java    From lion with Apache License 2.0 5 votes vote down vote up
@ApiOperation("Sentinel服务熔断降级")
@GetMapping("/sentinel/fallback")
@SentinelResource(value = "sentinelFallback", fallback = "sentinelFallback", fallbackClass = FallbackHandler.class)
public Result<String> sentinelFallback() {
    throw new LionException("This is sentinel control service fallback");
    //return Result.failure(500, "This is sentinel control service fallback");
}
 
Example #17
Source File: AbstractSentinelProccesser.java    From jboot with Apache License 2.0 5 votes vote down vote up
protected void traceException(Throwable ex, SentinelResource annotation) {
    Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
    // The ignore list will be checked first.
    if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
        return;
    }
    if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
        traceException(ex);
    }
}
 
Example #18
Source File: UserController.java    From j360-boot-app-all with Apache License 2.0 5 votes vote down vote up
@Trace("sayHello")
    @ResponseBody
    @RequestMapping("/sayHello")
    @SentinelResource(value = "sayHello", blockHandler = "handleException", blockHandlerClass = {UserController.class})
    public String sayHello() {
//        DefaultResult<User> result = userService.getUserById(1L);
//        if (result.isSuccess()) {
//            User user = result.getData();
//            return user.getName();
//        }
        return "null";
    }
 
Example #19
Source File: FooService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler",
        exceptionsToIgnore = {IllegalMonitorStateException.class})
public String baz(String name) {
    if (name.equals("fail")) {
        throw new IllegalMonitorStateException("boom!");
    }
    return "cheers, " + name;
}
 
Example #20
Source File: TestServiceImpl.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
@SentinelResource(value = "hello", fallback = "helloFallback")
public String hello(long s) {
    if (s < 0) {
        throw new IllegalArgumentException("invalid arg");
    }
    return String.format("Hello at %d", s);
}
 
Example #21
Source File: TestServiceImpl.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
@SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",
    exceptionsToIgnore = {IllegalStateException.class})
public String helloAnother(String name) {
    if (name == null || "bad".equals(name)) {
        throw new IllegalArgumentException("oops");
    }
    if ("foo".equals(name)) {
        throw new IllegalStateException("oops");
    }
    return "Hello, " + name;
}
 
Example #22
Source File: FooService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiAnotherFooWithDefaultFallback", defaultFallback = "globalDefaultFallback",
    fallbackClass = {FooUtil.class})
public String anotherFoo(int i) {
    if (i == 5758) {
        throw new IllegalArgumentException("oops");
    }
    return "Hello for " + i;
}
 
Example #23
Source File: FooService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiFooWithFallback", blockHandler = "fooBlockHandler", fallback = "fooFallbackFunc",
    exceptionsToTrace = {IllegalArgumentException.class})
public String fooWithFallback(int i) throws Exception {
    if (i == 5758) {
        throw new IllegalAccessException();
    }
    if (i == 5763) {
        throw new IllegalArgumentException();
    }
    return "Hello for " + i;
}
 
Example #24
Source File: FooService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@SentinelResource(value = "apiFoo", blockHandler = "fooBlockHandler",
    exceptionsToTrace = {IllegalArgumentException.class})
public String foo(int i) throws Exception {
    if (i == 5758) {
        throw new IllegalAccessException();
    }
    if (i == 5763) {
        throw new IllegalArgumentException();
    }
    return "Hello for " + i;
}
 
Example #25
Source File: AbstractSentinelAspectSupport.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
protected void traceException(Throwable ex, SentinelResource annotation) {
    Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
    // The ignore list will be checked first.
    if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
        return;
    }
    if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
        traceException(ex);
    }
}
 
Example #26
Source File: HelloService.java    From SpringAll with MIT License 4 votes vote down vote up
@SentinelResource("hello")
public String hello() {
    return "hello";
}
 
Example #27
Source File: TestController.java    From springbootexamples with Apache License 2.0 4 votes vote down vote up
@GetMapping(value = "/hello")
@SentinelResource("hello")
public String hello() {
    return "Hello Sentinel";
}
 
Example #28
Source File: TestController.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@GetMapping("/hello")
@SentinelResource("resource")
public String hello() {
	return "Hello";
}
 
Example #29
Source File: TestController.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@GetMapping("/aa")
@SentinelResource("aa")
public String aa(int b, int a) {
	return "Hello test";
}
 
Example #30
Source File: DemoService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@SentinelResource(blockHandler = "sayHelloBlockHandler")
public String sayHello(String name) {
    return "Hello, " + name;
}