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

springcloud-gateway的spi加载SlotChainBuilder问题 #2180

Closed
jasonliu459 opened this issue Apr 30, 2021 · 2 comments
Closed

springcloud-gateway的spi加载SlotChainBuilder问题 #2180

jasonliu459 opened this issue Apr 30, 2021 · 2 comments
Labels
area/gateway-flow-control Issues or PRs related to API gateway flow control duplicate This issue or pull request already exists

Comments

@jasonliu459
Copy link

问题描述:

使用springcloud-gateway+sentinel,引入

 <dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 </dependency>
 <dependency>
     <groupId>com.alibaba.csp</groupId>
     <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>

发现两者顺序对调,会影响网关流控是否生效,sentinel-spring-cloud-gateway-adapter在前会生效,在后则不会生效,经过分析源码,是由于两者都引入了

 <dependency>
   <groupId>com.alibaba.csp</groupId>
   <artifactId>sentinel-parameter-flow-control</artifactId>
 </dependency>

flow-control的SlotChainBuilder的spi是com.alibaba.csp.sentinel.slots.HotParamSlotChainBuilder
而gateway-adapter的SlotChainBuilder的spi是com.alibaba.csp.sentinel.adapter.gateway.common.slot.GatewaySlotChainBuilder,两者差了关键的:

chain.addLast(new GatewayFlowSlot());

继续分析,发现原因是

SlotChainProvider.newSlotChain()
...
slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class);
...

使用了SpiLoader.loadFirstInstanceOrDefault方法,所以获取到第一个非default的builder时就直接返回了,如果gateway-adapter在后面,则GatewaySlotChainBuilder不会被加载到,这里的加载方式改用SpiLoader.loadHighestPriorityInstance并且设置Builder的优先级会好点

@cdfive
Copy link
Collaborator

cdfive commented Apr 30, 2021

Refer to #1216, #1306
It has been improved via #411, may try latest version 1.8.1

@cdfive cdfive added the area/gateway-flow-control Issues or PRs related to API gateway flow control label Apr 30, 2021
@jasonliu459
Copy link
Author

@sczyh30 sczyh30 added the duplicate This issue or pull request already exists label Apr 30, 2021
CST11021 pushed a commit to CST11021/Sentinel that referenced this issue Nov 3, 2021
Co-authored-by: zhangxu16 <zhangxu16@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/gateway-flow-control Issues or PRs related to API gateway flow control duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants