Skip to content

Commit

Permalink
Support setting class-level defaultFallback for annotation extension (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyuguang authored May 29, 2020
1 parent 0d7da77 commit 6b86721
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,6 +33,7 @@
* Some common functions for Sentinel annotation aspect.
*
* @author Eric Zhao
* @author zhaoyuguang
*/
public abstract class AbstractSentinelAspectSupport {

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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<FlowRule>());
Expand Down
Original file line number Diff line number Diff line change
@@ -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...";
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}

0 comments on commit 6b86721

Please sign in to comment.