-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: 商城 BFF 层增加 Sentinel QPS 限流策略 (#55)
- Loading branch information
1 parent
f69669c
commit b3ac9ca
Showing
8 changed files
with
186 additions
and
0 deletions.
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
34 changes: 34 additions & 0 deletions
34
...f-biz/src/main/java/org/opengoofy/congomall/bff/biz/common/SentinelLimitFlowConstant.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,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.opengoofy.congomall.bff.biz.common; | ||
|
||
/** | ||
* 限流常量类 | ||
* | ||
* @author chen.ma | ||
* @github <a href="https://github.com/opengoofy" /> | ||
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册 | ||
*/ | ||
public class SentinelLimitFlowConstant { | ||
|
||
public static final String CREATE_ORDER_PATH = "createOrder"; | ||
|
||
public static final String ADD_CART_PATH = "addCart"; | ||
|
||
public static final String ADD_ADDRESS_PATH = "addAddress"; | ||
} |
67 changes: 67 additions & 0 deletions
67
...mall-bff-biz/src/main/java/org/opengoofy/congomall/bff/biz/config/SentinelRuleConfig.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,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.opengoofy.congomall.bff.biz.config; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.ADD_ADDRESS_PATH; | ||
import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.ADD_CART_PATH; | ||
import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.CREATE_ORDER_PATH; | ||
|
||
/** | ||
* 初始化限流配置 | ||
* | ||
* @author chen.ma | ||
* @github <a href="https://github.com/opengoofy" /> | ||
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册 | ||
*/ | ||
@Component | ||
public class SentinelRuleConfig implements InitializingBean { | ||
|
||
@Value("${congomall.bff.qps-count:1}") | ||
private Integer qpsCount; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
List<FlowRule> rules = new ArrayList<>(); | ||
FlowRule createOrderRule = new FlowRule(); | ||
createOrderRule.setResource(CREATE_ORDER_PATH); | ||
createOrderRule.setGrade(RuleConstant.FLOW_GRADE_QPS); | ||
createOrderRule.setCount(qpsCount); | ||
FlowRule addCartRule = new FlowRule(); | ||
addCartRule.setResource(ADD_CART_PATH); | ||
addCartRule.setGrade(RuleConstant.FLOW_GRADE_QPS); | ||
addCartRule.setCount(qpsCount); | ||
FlowRule addAddressRule = new FlowRule(); | ||
addAddressRule.setResource(ADD_ADDRESS_PATH); | ||
addAddressRule.setGrade(RuleConstant.FLOW_GRADE_QPS); | ||
addAddressRule.setCount(qpsCount); | ||
rules.add(createOrderRule); | ||
rules.add(addCartRule); | ||
rules.add(addAddressRule); | ||
FlowRuleManager.loadRules(rules); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...all-bff-biz/src/main/java/org/opengoofy/congomall/bff/biz/handler/CustomBlockHandler.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,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.opengoofy.congomall.bff.biz.handler; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import org.opengoofy.congomall.bff.biz.common.ResultT; | ||
import org.opengoofy.congomall.bff.biz.dto.req.adapter.OrderCreateAdapterReqDTO; | ||
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ProductCartAddAdapterReqDTO; | ||
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ReceiveAddressSaveAdapterReqDTO; | ||
|
||
/** | ||
* 自定义流控策略 | ||
* | ||
* @author chen.ma | ||
* @github <a href="https://github.com/opengoofy" /> | ||
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册 | ||
*/ | ||
public class CustomBlockHandler { | ||
|
||
public static ResultT<Boolean> addCardBlockHandlerMethod(ProductCartAddAdapterReqDTO requestParam, BlockException exception) { | ||
return new ResultT().setCode(500).setMessage("当前访问网站人数过多,请稍后再试..."); | ||
} | ||
|
||
public static ResultT<Boolean> createOrderBlockHandlerMethod(OrderCreateAdapterReqDTO requestParam, BlockException exception) { | ||
return new ResultT().setCode(500).setMessage("当前访问网站人数过多,请稍后再试..."); | ||
} | ||
|
||
public static ResultT<Boolean> addAddressBlockHandlerMethod(ReceiveAddressSaveAdapterReqDTO requestParam, BlockException exception) { | ||
return new ResultT().setCode(500).setMessage("当前访问网站人数过多,请稍后再试..."); | ||
} | ||
} |
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
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