-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add global fallback support for AspectJ annotation extension #3383
base: 1.8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 1999-2018 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.fallback; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Default global fallback | ||
* | ||
* @author luffy | ||
*/ | ||
public class DefaultGlobalFallback implements IGlobalFallback { | ||
Check warning on line 25 in sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java Codecov / codecov/patchsentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java#L25
|
||
|
||
@Override | ||
public Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable{ | ||
throw t; | ||
Check warning on line 29 in sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java Codecov / codecov/patchsentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java#L29
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 1999-2018 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.fallback; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Global fallback interface | ||
* | ||
* @author luffy | ||
*/ | ||
public interface IGlobalFallback { | ||
|
||
/** | ||
* | ||
* @param originalMethod the original method called | ||
* @param args The parameters of the method that was originally called | ||
* @param t The exception thrown by the method originally called | ||
* @return The result of the global fallback handler execution | ||
* @throws Throwable | ||
*/ | ||
Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.alibaba.csp.sentinel.annotation.aspectj.integration.fallback; | ||
|
||
import com.alibaba.csp.sentinel.fallback.IGlobalFallback; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author luffy | ||
* @version 1.0 | ||
* @date 2023/5/5 5:18 下午 | ||
*/ | ||
public class AnnotationGlobalFallback implements IGlobalFallback { | ||
|
||
@Override | ||
public Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable{ | ||
return "AnnotationGlobalFallback"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,8 @@ | |
package com.alibaba.csp.sentinel.annotation.aspectj.integration.service; | ||
|
||
import com.alibaba.csp.sentinel.annotation.SentinelResource; | ||
import com.alibaba.csp.sentinel.annotation.aspectj.integration.fallback.AnnotationGlobalFallback; | ||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
|
@@ -53,6 +53,25 @@ public String fooWithFallback(int i) throws Exception { | |
return "Hello for " + i; | ||
} | ||
|
||
@SentinelResource(value = "apiFooWithFallback", globalFallback = AnnotationGlobalFallback.class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If convenient, could you please add relevant example code in the sentinel-demo-annotation-spring-aop module? |
||
exceptionsToTrace = {IllegalArgumentException.class}) | ||
public String fooWithAnnotationGlobalFallback(int i) throws Exception { | ||
if (i == 5758) { | ||
throw new IllegalAccessException(); | ||
} | ||
return "Hello for " + i; | ||
} | ||
|
||
@SentinelResource(value = "apiFooWithDefaultFallback",exceptionsToTrace = {IllegalArgumentException.class}) | ||
public String fooWithDefaultGlobalFallback(int i) throws Exception { | ||
if (i == 5758) { | ||
throw new IllegalAccessException(); | ||
} | ||
return "Hello for " + i; | ||
} | ||
|
||
|
||
|
||
@SentinelResource(value = "apiAnotherFooWithDefaultFallback", defaultFallback = "globalDefaultFallback", | ||
fallbackClass = {FooUtil.class}) | ||
public String anotherFoo(int i) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, the scenario the related issue(#3110) aims to solve is to have a global fallback even when neither fallback nor defaultFallback is configured in the annotations. So, the global fallback should be configured in the construction method of SentinelResourceAspect, rather than through annotations (like what defaultFallback did).