当前位置: 首页 > news >正文

Sentinel 服务限流机制

Sentinel 服务限流

雪崩效应
高并发系统中,由一个小问题而引发的系统崩溃。


解决方案

  1. 设置线程超时,当某个请求在特定的时间内无法调通,则直接释放该线程。
  2. 设置限流,某个微服务到达访问上限之后,其他请求就暂时无法访问该服务。
  3. 熔断器,这是目前微服务中比较主流的解决方案,如 Hystrix,相当于家里的保险丝,如果电流过大,为了保护家电设备,自动烧断保险丝,断电,阻断电流。

1. Sentinel 基本接入步骤

1.1 pom.xml 引入依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

1.2 application.yml 配置

management:endpoints:web:exposure:include: '*'
spring:cloud:sentinel:transport:dashboard: localhost:8080

1.3 启动 Sentinel 控制台

  • 下载地址:https://github.com/alibaba/Sentinel/releases
  • 启动命令(控制台默认端口 8080):
java -jar sentinel-dashboard-1.7.2.jar
  • 浏览器访问:http://localhost:8080

启动服务后访问一次接口,再刷新控制台页面,就能看到监控数据。


2. 流控规则

直接限流

在 Sentinel 控制台中选择簇点链路,直接对指定资源(如 /index)设置 QPS 阈值。


关联限流

@RestController
public class SentinelController {@Value("${server.port}")private String port;@GetMapping("/index")public String index(){return this.port;}@GetMapping("/list")public String list(){return "list";}
}

模拟访问压力:

public class Test {public static void main(String[] args) throws Exception {RestTemplate restTemplate = new RestTemplate();for (int i = 0; i < 1000; i++) {restTemplate.getForObject("http://localhost:8280/list", String.class);Thread.sleep(500);}}
}

链路限流(重点)

链路限流是一种更细粒度的限流方式,对某个资源(Service)在不同调用路径下分别进行限流控制。

⚠️ 注意:Sentinel 1.6.3+ 默认会将所有 URL 的 context 统一为 sentinel_web_context,链路限流失效。

正确开启链路限流的步骤

1)pom.xml 添加依赖
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.7.1</version>
</dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-web-servlet</artifactId><version>1.7.1</version>
</dependency>

2)application.yml 禁用默认 Web Filter 收敛
spring:cloud:sentinel:filter:enabled: false

3)编写配置类,手动注册 CommonFilter
@Configuration
public class FilterConfiguration {@Beanpublic FilterRegistrationBean registrationBean(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new CommonFilter());registrationBean.addUrlPatterns("/*");registrationBean.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY,"false");registrationBean.setName("sentinelFilter");return registrationBean;}
}

4)Service 层定义资源
@Service
public class HelloService {@SentinelResource("test")public void test() {System.out.println("test");}
}

5)Controller 多路径调用资源
@RestController
public class TestController {@Autowiredprivate HelloService helloService;@GetMapping("/test1")public String test1() {this.helloService.test();return "test1";}@GetMapping("/test2")public String test2() {this.helloService.test();return "test2";}
}

然后在 Sentinel 控制台中添加:

  • 资源名称:test
  • 规则类型:链路
  • 来源 /test1 → test 设置限流规则
  • 来源 /test2 → test 不设置

这样就实现了“链路级别”的资源访问控制!


3. 流控效果

快速失败

超过阈值立即抛出异常,返回错误提示。


Warm Up(预热)

在应用刚启动时,预热期间单机 QPS 阈值较低,逐渐升至设定值。

  • 初始:阈值为设置值的 1/3
  • 过渡:按比例提升
  • 最终:恢复到设定值

排队等待(匀速排队)

请求未被立即处理,而是进入等待队列,直到到达时间窗口内的空闲处理时间。

public class Test {public static void main(String[] args) throws Exception {RestTemplate restTemplate = new RestTemplate();for (int i = 0; i < 1000; i++) {restTemplate.getForObject("http://localhost:8280/index", String.class);Thread.sleep(500);}}
}

总结

机制描述
直接限流对资源本身进行 QPS 限制
关联限流当前资源访问量受另一个资源访问量影响
链路限流对相同资源在不同调用路径下设置不同的限流规则(需关闭统一 context)
Warm Up适用于刚启动的系统,慢启动保护
排队等待请求进入队列处理,防止短时间内系统被压垮

欢迎收藏、点赞👍、评论💬,如需更多微服务实践文章,欢迎关注我持续更新的专栏!

如需 .md 文件、配图打包或配套项目源码,请留言~

http://www.lqws.cn/news/454663.html

相关文章:

  • 信息抽取数据集:多层次分类与深度分析综述
  • C#读取OPCUA节点数据
  • API 调试工具校验 JSON Mock 接口(一):无参请求与基础响应验证
  • Android 当apk是系统应用时,无法使用webView的解决方案
  • 汽车加气站操作工考试题库含答案【最新】
  • DB面试题
  • mysql查询使用`_rowid` 虚拟列
  • vtk和opencv和opengl直接的区别是什么?
  • Pinia在多步骤表单中的实践应用
  • 芯谷科技--高性能、高可靠性降压转换器D3502C
  • 泛型方法调用需要显示指定泛型类型的场景
  • 【JUC面试篇】Java并发编程高频八股——线程池
  • 5G 浪潮:发展全景、困境突围与未来航向
  • Fiddler抓包工具使用技巧:如何结合Charles和Wireshark提升开发调试效率
  • conda(Anaconda/Miniconda)常用命令
  • 头部黄金珠宝品牌扬帆狮城,百胜双中台一体化方案护航全球化运营
  • Linux下nginx访问路径页面
  • 【MySQL篇10】:四种分库分表详解
  • 云蝠智能大模型呼叫系统:为企业提供专业的智能客户联络
  • 第3讲、LangChain性能优化:上下文缓存与流式响应实战指南
  • 通过Radius认证服务器实现飞塔/华为防火墙二次认证:原理、实践与安全价值解析
  • django serializer __all__中 额外添加外键里的某一个属性
  • King’s LIMS 系统引领汽车检测实验室数字化转型
  • Unity3d中使用Mirror进行自定义消息通信
  • 解锁决策树:数据挖掘的智慧引擎
  • YOLO系列模型基础与实战(YOLO系列发展线)
  • YOLOv11改进 | RCS-OSA与C3k2融合架构技术详解
  • 容器技术技术入门与Docker环境部署
  • 在 CentOS 7.9 中 Node 18.20.2 安装指南
  • HTTP 请求方法与状态码