SpringBoot集成Sentinel 实现QPS限流

Spring Cloud Alibaba 的 Sentinel 组件提供了丰富的"流量控制"规则,

单体SpringBoot应用中也可以集成 Sentinel 来实现流量控制,本文主要讲 QPS流量控制。

SpringBoot集成Sentinel有两种方式:

一种是 dashboard控制面板的方式,需要下载sentinel-dashboard-1.6.3.jar

另一种是不需要dashboard,直接代码集成的方式。

这里采用的是第二种

注意:版本非常重要

有博文说 SpringBoot版本在 2.2.0 到 2.3.1之间才行,其他版本流量控制不生效。

我这里SpringBoot版本为 2.2.5.RELEASE,Sentinel版本为 2.2.1RELEASE(之前 sentinel版本为2.1.2.RELEASE,限流死活不生效)

1.添加Sentinel的Maven依赖包

XML 复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	<version>2.2.1.RELEASE</version>
</dependency>

2.定义限流规则

java 复制代码
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
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;
@Component
public class SentinelConfiguration implements InitializingBean{
    public final static String QPS_LIMIT_GET_USER= "getUser";
    @Override
    public void afterPropertiesSet() {
        initFlowQpsRule(QPS_LIMIT_GET_USER);
    }
    private void initFlowQpsRule(String resource) {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource(resource);
        //设置限流规则为  QPS限流(每秒请求量)
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1); //1秒内允许请求1次
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

3.限流触发时的处理逻辑

java 复制代码
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
@RestControllerAdvice
public class GlobalExceptionHandler{
    /**
     * 限流异常
     */
    @ExceptionHandler(FlowException.class)
    public String flowExceptionHandler(FlowException ex) {
        return "---接口被限流了---";
    }
}

注意:如果没有限流触发后的处理逻辑,会报以下异常

java 复制代码
com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

4.编写接口测试限流是否生效

java 复制代码
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.qyp.hpa.config.SentinelConfiguration;
@RestController
public class UserController {
	@RequestMapping("/getUser")
	@SentinelResource(value = SentinelConfiguration.QPS_LIMIT_GET_USER)
    public Map<String,Object> tempList() {
		Map<String,Object> user = new HashMap<String,Object>();
		user.put("name", "张三");
		user.put("age", 30);
        return user;
    }
}

浏览器请求getUser接口,正常返回

浏览器连续快速点击刷新(1秒内点2次以上),触发接口限流规则,返回预设的限流触发消息。

相关推荐
code_slave(码畜)8 小时前
微服务架构落地:基础服务 —— 报表服务(中篇:元数据、查询引擎、缓存与权限落地)
spring boot·spring cloud·缓存·微服务·架构
ZealSinger8 小时前
Boot4挂起函数丢traceId怎么修
spring boot·kotlin·协程·可观测性
code_slave(码畜)9 小时前
微服务架构落地:基础服务 —— 报表服务(AI 集成篇:AI 增强报表能力)
人工智能·spring boot·spring cloud·微服务·架构
code_slave(码畜)10 小时前
微服务架构落地:公共中间件层总览——不承载业务,只承载稳定性
spring boot·spring cloud·微服务·中间件·架构
谢亮_vipxieliang11 小时前
Spring Boot 自动配置原理:从 @SpringBootApplication 到自定义 Starter
java·spring boot·后端
AI人工智能+电脑小能手13 小时前
Java 企业级工具类全家桶-68-HTTP请求工具类
spring boot·网络请求·http请求·resttemplate·jdk 17·java工具类·企业级开发
paopaokaka_luck14 小时前
非遗文物数字化小程序(AI非遗问答、ONNX图像识别、协同过滤推荐、ECharts数据分析、非遗知识浏览与互动、文创商城订单闭环、文化活动报名签到、社区交流)
javascript·spring boot·mysql·数据分析·echarts·mybatis
EatFan16 小时前
Spring Boot 4 迁移避坑清单:Jackson 3、starter 拆分与最低 JDK 口径核对(含若依/芋道/CRMEB 升级对照)
java·数据库·spring boot·spring boot 4·java 21·jakarta ee 11·jackson 3
ly768917 小时前
Spring Boot 集成 Redis 企业级实践:连接池、序列化与缓存穿透雪崩的工程化防御
spring boot·redis·缓存·缓存穿透·布隆过滤器·lettuce 连接池
Wx-bishekaifayuan17 小时前
springboot陨石鉴收系统96265-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spring·课程设计