diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java index 95f6584841..fe1ab396dd 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,9 +23,10 @@ * The annotation indicates a definition of Sentinel resource. * * @author Eric Zhao + * @author zhaoyuguang * @since 0.1.1 */ -@Target(ElementType.METHOD) +@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface SentinelResource { diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java index b67a219e94..b2040dca42 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ * Some common functions for Sentinel annotation aspect. * * @author Eric Zhao + * @author zhaoyuguang */ public abstract class AbstractSentinelAspectSupport { @@ -186,7 +187,15 @@ private Method extractFallbackMethod(ProceedingJoinPoint pjp, String fallbackNam private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defaultFallback, Class[] locationClass) { if (StringUtil.isBlank(defaultFallback)) { - return null; + 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(); diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java index bf127cc932..370df67ac7 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.alibaba.csp.sentinel.annotation.aspectj.integration; import com.alibaba.csp.sentinel.annotation.aspectj.integration.config.AopTestConfig; +import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService; import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooService; import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooUtil; import com.alibaba.csp.sentinel.node.ClusterNode; @@ -41,12 +42,15 @@ * Integration test for Sentinel annotation AspectJ extension. * * @author Eric Zhao + * @author zhaoyuguang */ @ContextConfiguration(classes = {SentinelAnnotationIntegrationTest.class, AopTestConfig.class}) public class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringContextTests { @Autowired private FooService fooService; + @Autowired + private BarService barService; @Test public void testProxySuccessful() { @@ -181,6 +185,29 @@ public void testNormalBlockHandlerAndFallback() throws Exception { assertThat(cn.blockQps()).isPositive(); } + @Test + public void testClassLevelDefaultFallbackWithSingleParam() { + assertThat(barService.anotherBar(1)).isEqualTo("Hello for 1"); + String resourceName = "apiAnotherBarWithDefaultFallback"; + ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); + assertThat(cn).isNotNull(); + assertThat(cn.passQps()).isPositive(); + + assertThat(barService.doSomething(1)).isEqualTo("do something"); + String resourceName1 = "com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService:doSomething(int)"; + ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1); + assertThat(cn1).isNotNull(); + assertThat(cn1.passQps()).isPositive(); + + assertThat(barService.anotherBar(5758)).isEqualTo("eee..."); + assertThat(cn.exceptionQps()).isPositive(); + assertThat(cn.blockQps()).isZero(); + + assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback"); + assertThat(cn1.exceptionQps()).isPositive(); + assertThat(cn1.blockQps()).isZero(); + } + @Before public void setUp() throws Exception { FlowRuleManager.loadRules(new ArrayList()); diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java new file mode 100644 index 0000000000..69674ea12b --- /dev/null +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java @@ -0,0 +1,48 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.annotation.aspectj.integration.service; + +import com.alibaba.csp.sentinel.annotation.SentinelResource; +import org.springframework.stereotype.Service; + +/** + * @author zhaoyuguang + */ +@Service +@SentinelResource(defaultFallback = "doFallback", fallbackClass = GlobalFallback.class) +public class BarService { + + @SentinelResource(value = "apiAnotherBarWithDefaultFallback", defaultFallback = "fallbackFunc") + public String anotherBar(int i) { + if (i == 5758) { + throw new IllegalArgumentException("oops"); + } + return "Hello for " + i; + } + + @SentinelResource() + public String doSomething(int i) { + if (i == 5758) { + throw new IllegalArgumentException("oops"); + } + return "do something"; + } + + public String fallbackFunc(Throwable t) { + System.out.println(t.getMessage()); + return "eee..."; + } +} diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java new file mode 100644 index 0000000000..3571b57720 --- /dev/null +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java @@ -0,0 +1,26 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.annotation.aspectj.integration.service; + +/** + * @author zhaoyuguang + */ +public class GlobalFallback { + + public static String doFallback(Throwable t) { + return "GlobalFallback:doFallback"; + } +}