Spring Cloud Alibaba系列(一)限流与防护组件Sentinel的简单使用
Sentinel是Spring Cloud Alibaba体系的安全防护组件,我们可以使用它以“非业务侵入”方式实现限流,熔断,服务降级需求。
一. 下载并启动Sentinel控制台
从GitHub网址https://github.com/alibaba/Sentinel/releases/tag/1.8.6中下载控制台jar,用以下命令启动:
1 | H:\> java '-Dserver.port=8090' '-jar' sentinel-dashboard-1.8.6.jar |
启动成功的界面如下:
在浏览器输入验证:http://localhost:8090/
出现登录界面
输入用户名和密码,用户名和密码都是sentinel,登录成功
二. 在项目中引入sentinel依赖包
在Spring Boot项目中引入以下依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在Spring Boot项目的resource目录的application.yml中添加如下配置:
spring:
cloud:
sentinel:
transport:
# 与sentinel控制台交互的端口
port: 9000
# sentinel控制台的工作地址和端口
dashboard: localhost:8090
在控制器类中新增@SentinelResource注解,通过注解value参数设置该方法在Sentinel控制台里的标识符,其中@SentinelResource(value = "hello")就是我们需要新增的注解
@RestController public class Demo { @RequestMapping("/hello") @SentinelResource(value = "hello")
public String hello() {
return "hello";
}
}
三. 在Sentinel控制台里设置限流参数
由于Sentinel采用的是懒加载机制,首先需要先访问对应接口才能在控制台里见到对应项目,才能进行接下来的设置,在浏览器中访问接口,访问接口成功后随后访问Sentinel控制台,已经可以看到对应项目
单击左侧的【流控规则】菜单,点击【新增流控规则】
输入资源名和单击阈值
在浏览器中快速刷新验证流控是否生效,由于我们刚刚设置“每秒限流1次”规则,多次快速刷新将突破该限制,出现以下默认错误界面,确认限流成功
同时在开发工具控制台可以看到应用打印了如下日志,也说明限流成功
四. 实现热点限流效果
模拟商品购买请求,编写以下示例
@RestController
public class HotSpot {
@RequestMapping("/buy")
@SentinelResource(value = "buy")
public String buy(@RequestParam(value = "item", required = false) String item,
@RequestParam(value = "itemPrice", required = false) String itemPrice) {
return "hot spot...";
}
}
可以作用于请求参数上,设置如下
其中参数索引代表接口里定义的对应参数,0代表item,1代码itemPrice,这里对item参数进行限流,只要包含item参数,则生效,
在浏览器多次刷新请求http://localhost:8181/buy?item=cellphone,在5秒的时间窗口内则只能访问一次,其他访问都将返回默认错误页面
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~