Skip to content
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

增加基于注解的流控和降级配置 #797

Closed
wants to merge 1 commit into from
Closed

增加基于注解的流控和降级配置 #797

wants to merge 1 commit into from

Conversation

code1986
Copy link
Contributor

Describe what this PR does / why we need it

简化FlowRule和DegradeRule的配置,目前的配置方式是在需要降级的地方写上SentinelResource注解,
然后专门找个文件写FlowRule和DegradeRule,配置内容分散,resource名需要在多处拷贝,需要多加小心保持各处resource名的一致性,给代码维护增加负担.
使用注解定义FlowRule和DegradeRule后,资源名只需要出现一次, 限流和降级规则在定义SentinelResource处直接可见,降低配置分散度,维护起来比较轻松.

Does this pull request fix one issue?

Describe how you did it

1.增加一个BeanPostProcess类SentinelAnnotationBeanProcessor在每个bean生成时扫描SentinelResource注解,在有注解的方法上找FlowRuleDefine和DegradeRuleDefine注解,如果找到就记录下来.
2.所有bean加载完后,调用FlowRuleManager和DegradeRuleManager的loadRules方法加载第一步找到的所有rule.

Describe how to verify it

1.如果是Spring Boot项目, 在Starter类上加上EnableSentinel注解启用Sentinel.同时不再需要手工配置SentinelResourceAspect这个bean. Starter类看起来如下:
image
在定义SentinelResource资源的地方加上FlowRuleDefine和DegradeRuleDefine注解
image

2.如果是基于xml的Spring配置,需要在xml中额外配置上 SentinelAnnotationBeanProcessor 这个bean,之后FlowRuleDefine和DegradeRuleDefine的注解方法和上面一样

Special notes for reviews

@CLAassistant
Copy link

CLAassistant commented May 29, 2019

CLA assistant check
All committers have signed the CLA.

@sczyh30 sczyh30 added the to-review To review label May 29, 2019
@codecov-io
Copy link

Codecov Report

Merging #797 into master will decrease coverage by 0.24%.
The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #797      +/-   ##
============================================
- Coverage     41.69%   41.45%   -0.25%     
- Complexity     1375     1377       +2     
============================================
  Files           304      306       +2     
  Lines          8766     8824      +58     
  Branches       1182     1189       +7     
============================================
+ Hits           3655     3658       +3     
- Misses         4663     4719      +56     
+ Partials        448      447       -1
Impacted Files Coverage Δ Complexity Δ
...n/aspectj/configuration/SentinelConfiguration.java 0% <0%> (ø) 0 <0> (?)
...ation/aspectj/SentinelAnnotationBeanProcessor.java 0% <0%> (ø) 0 <0> (?)
...a/csp/sentinel/slots/statistic/base/LeapArray.java 70.58% <0%> (+2.94%) 34% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4182c01...3139f55. Read the comment docs.

@finefuture
Copy link
Contributor

注解=写死,并不能灵活的控制

@code1986
Copy link
Contributor Author

code1986 commented May 30, 2019

注解提供了一个方便易于管理的配置方案,目的是避免配置的分散和资源名手误引起的配置错误,减少机械化的流控编码,并不是强制必须使用.
需要灵活性完全可以不使用注册,采用编程方式实现.
现实中大部分接口的QPS和降级措施都是明确的,可以直接写出,只有少量活动和突发状况才需要"灵活"的修改.大部分接口并不需要经常的调整限流量.
在需要调整限流规则时还有AHAS Sentinel 控制台, 控制台随时能够覆盖掉注解上的设置.

@sczyh30
Copy link
Member

sczyh30 commented May 31, 2019

Hi, thanks for contributing. Some kinds of rules are complex (e.g. for FlowRule it has plenty of attributes), so it might not be appropriate to configure with annotation. For circuit breaking it's okay, but it's not a recommended way.

Would you like to add your implementation in Awesome Sentinel list?

@code1986
Copy link
Contributor Author

Hi, thanks for contributing. Some kinds of rules are complex (e.g. for FlowRule it has plenty of attributes), so it might not be appropriate to configure with annotation. For circuit breaking it's okay, but it's not a recommended way.

Would you like to add your implementation in Awesome Sentinel list?

很荣幸能加入到Awesome Sentinel list.

@sczyh30
Copy link
Member

sczyh30 commented May 31, 2019

Hi, thanks for contributing. Some kinds of rules are complex (e.g. for FlowRule it has plenty of attributes), so it might not be appropriate to configure with annotation. For circuit breaking it's okay, but it's not a recommended way.
Would you like to add your implementation in Awesome Sentinel list?

很荣幸能加入到Awesome Sentinel list.

You can submit a PR to add your implementation into the Awesome Sentinel :)

@sczyh30
Copy link
Member

sczyh30 commented Jun 4, 2019

Hi, any progress about the PR to awesome-sentinel.md list?

@code1986
Copy link
Contributor Author

code1986 commented Jun 6, 2019

Hi, any progress about the PR to awesome-sentinel.md list?

#815, 已加到awesome-sentinel.md, 改awesome-sentinel.md后Travis CI check失败,真奇怪,又没改过代码

@sczyh30
Copy link
Member

sczyh30 commented Jun 6, 2019

Hi, any progress about the PR to awesome-sentinel.md list?

#815, 已加到awesome-sentinel.md, 改awesome-sentinel.md后Travis CI check失败,真奇怪,又没改过代码

I've re-triggered the CI build and it has passed.

@sczyh30
Copy link
Member

sczyh30 commented Jun 10, 2019

Added to awesome-sentinel.md via #815.

@sczyh30 sczyh30 closed this Jun 10, 2019
@sczyh30 sczyh30 added kind/enhancement Category issues or prs related to enhancement. and removed to-review To review labels Jun 10, 2019
CST11021 pushed a commit to CST11021/Sentinel that referenced this pull request Nov 3, 2021
[RIP-9] Commit docs Example_OpenMessaging.md
CST11021 pushed a commit to CST11021/Sentinel that referenced this pull request Nov 3, 2021
[RIP-9] Commit docs Example_OpenMessaging.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants