Skip to content

Commit

Permalink
feature: 商城 BFF 层增加 Sentinel QPS 限流策略 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Apr 20, 2023
1 parent f69669c commit b3ac9ca
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
10 changes: 10 additions & 0 deletions congomall-bff/congomall-bff-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
<artifactId>congomall-bff-remote</artifactId>
<version>${project.version}</version>
</dependency>

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

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
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";
}
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);
}
}
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("当前访问网站人数过多,请稍后再试...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.opengoofy.congomall.bff.web.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
Expand All @@ -25,13 +26,16 @@
import org.opengoofy.congomall.bff.biz.dto.req.adapter.OrderCreateAdapterReqDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.OrderAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.OrderResultAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.handler.CustomBlockHandler;
import org.opengoofy.congomall.bff.biz.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.CREATE_ORDER_PATH;

/**
* 订单控制层
*
Expand All @@ -47,6 +51,11 @@ public class OrderController {

@PostMapping("/member/addOrder")
@ApiOperation(value = "订单创建", notes = "订单创建")
@SentinelResource(
value = CREATE_ORDER_PATH,
blockHandler = "createOrderBlockHandlerMethod",
blockHandlerClass = CustomBlockHandler.class
)
public ResultT<String> addOrder(@RequestBody OrderCreateAdapterReqDTO requestParam) {
return ResultT.success(orderService.addOrder(requestParam));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.opengoofy.congomall.bff.web.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,13 +29,16 @@
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ProductCartDeleteChecksAdapterReqDTO;
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ProductCartUpdateAdapterReqDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.ProductCartAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.handler.CustomBlockHandler;
import org.opengoofy.congomall.bff.biz.service.ProductCartService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.ADD_CART_PATH;

/**
* 购物车控制层
*
Expand All @@ -57,6 +61,11 @@ public ResultT<List<ProductCartAdapterRespDTO>> listAllProductCart(@RequestBody

@PostMapping("/member/addCart")
@ApiOperation(value = "新增购物车", notes = "新增购物车")
@SentinelResource(
value = ADD_CART_PATH,
blockHandler = "addCardBlockHandlerMethod",
blockHandlerClass = CustomBlockHandler.class
)
public ResultT<Integer> addProductCard(@RequestBody ProductCartAddAdapterReqDTO requestParam) {
return ResultT.success(productCartService.addProductCard(requestParam));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.opengoofy.congomall.bff.web.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
Expand All @@ -26,6 +27,7 @@
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ReceiveAddressSaveAdapterReqDTO;
import org.opengoofy.congomall.bff.biz.dto.req.adapter.ReceiveAddressUpdateAdapterReqDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.ReceiveAddressAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.handler.CustomBlockHandler;
import org.opengoofy.congomall.bff.biz.service.ReceiveAddressService;
import org.opengoofy.congomall.springboot.starter.log.annotation.MLog;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -34,6 +36,8 @@

import java.util.List;

import static org.opengoofy.congomall.bff.biz.common.SentinelLimitFlowConstant.ADD_ADDRESS_PATH;

/**
* 收货地址控制层
*
Expand All @@ -57,6 +61,11 @@ public ResultT<List<ReceiveAddressAdapterRespDTO>> listReceiveAddress(@RequestBo

@PostMapping("/member/addAddress")
@ApiOperation(value = "新增用户收货地址", notes = "新增用户收货地址")
@SentinelResource(
value = ADD_ADDRESS_PATH,
blockHandler = "addAddressBlockHandlerMethod",
blockHandlerClass = CustomBlockHandler.class
)
public ResultT<Integer> saveReceiveAddress(@RequestBody ReceiveAddressSaveAdapterReqDTO requestParam) {
return ResultT.success(receiveAddressService.saveReceiveAddress(requestParam));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ congomall:
description: BFF Service
title: BFF Service
version: 1.0.0
bff:
qps-count: 1

feign:
client:
Expand Down

0 comments on commit b3ac9ca

Please sign in to comment.