Support setting class-level defaultFallback for annotation extension (#1493)
This commit is contained in:
parent
0d7da77d03
commit
6b86721b36
|
|
@ -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 @@ import java.lang.annotation.*;
|
|||
* 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 {
|
||||
|
|
|
|||
|
|
@ -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 @@ import java.util.Arrays;
|
|||
* Some common functions for Sentinel annotation aspect.
|
||||
*
|
||||
* @author Eric Zhao
|
||||
* @author zhaoyuguang
|
||||
*/
|
||||
public abstract class AbstractSentinelAspectSupport {
|
||||
|
||||
|
|
@ -186,8 +187,16 @@ public abstract class AbstractSentinelAspectSupport {
|
|||
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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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 @@ import static org.assertj.core.api.Assertions.*;
|
|||
* 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 class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringConte
|
|||
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<FlowRule>());
|
||||
|
|
|
|||
|
|
@ -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...";
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue