-
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
Support setting class-level defaultFallback for annotation extension #1493
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.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..."; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.alibaba.csp.sentinel.annotation.aspectj.integration.service; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
|
||
sczyh30 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class GlobalFallback { | ||
|
||
public static String doFallback(Throwable t) { | ||
return "GlobalFallback:doFallback"; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The priority of method-level annotation is higher than class-level annotation, so maybe here we need to check whether original
locationClass
is absent.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.
如果 locationClass 没有配置,那么就是用当前类的作为默认,这样的逻辑合适么?
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.
解析优先级:方法级别的 fallbackClass > 类级别的 fallbackClass,若不存在则用当前类替代。
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.
假如方法注解 写了的locationClass 没写defaultFallback
而类注解写了的locationClass 和defaultFallback
在这种case下是不是 使用者配置错误 应该全部使用类注解上的locationClass 和defaultFallback比较好?
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.
考虑一下这种场景(不一定合理):某个方法希望 override 对应的 fallbackClass,这种情况的话 可能还不能判定为配置错误
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.
改了下逻辑 如果取方法注解 -> 取类注解 -> 取本类
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.
看目前代码已符合满足该场景了
不过这句好像并没有完全覆盖到。
比如方法级的defaultFallback不为空,但方法级的fallbackClass为空,目前没有取类级别的,取的本类。因为第一个if判断是方法级别的defaultFallback。
这个场景也不一定合理,就看是否需要完全按
方法注解 -> 取类注解 -> 取本类
优先级了。BTW,有个疑问:
SentinelResource
注解:Class<?>[] blockHandlerClass() default {};
Class<?>[] fallbackClass() default {};
为何要设计成数组呢?看代码里只取了[0]第一个,是不是用
Class<?>
就好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.
We may improve this later.
Actually
fallbackClass
is optional. If usingClass<?>
, it cannot be null anymore.