sentinel限流工具使用

文章目录

sentinel是阿里的一款限流工具,非常好用。

步骤

1、下载sentinel-dashboard(sentinel看板程序)

github下载地址(最下面的assets,点jar包即可下载):
https://github.com/alibaba/Sentinel/releases/tag/1.8.8

sentinel-dashboard百度网盘地址如下:

链接: https://pan.baidu.com/s/13J64bBNXk-6THBc58bzo0A

提取码: qc9g

2、启动sentinel-dashboard并访问界面

在sentinel-dashboard.jar同目录打开cmd,启动命令:

bash 复制代码
java -Dserver.port=8080 -jar sentinel-dashboard-1.8.8.jar

浏览器访问:
http://127.0.0.1:8080/,如图:

当然,一开始是没有项目的。

3、web项目里面引入sentinel配置

maven依赖:

starter版本建议2.7.18,以下的配置文件有点乱,需要哪个看着选即可。

xml 复制代码
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.7.18</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<java.version>8</java.version>
	<spring-cloud.version>2021.0.8</spring-cloud.version>
	<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<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>

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

<repositories>
		<repository>
			<id>aliyunmaven</id>
			<name>阿里云中央仓库</name>
			<url>https://maven.aliyun.com/repository/public</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>aliyunmaven</id>
			<name>阿里云中央仓库</name>
			<url>https://maven.aliyun.com/repository/public</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
4、修改配置文件

application.yaml添加如下配置(注意spring的层级肯定有了):

yml 复制代码
spring:
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080 # Sentinel 控制台地址
        port: 8719
      # 可选:关闭链路聚合,确保链路限流生效
      web-context-unify: false
5、创建请求类

创建TestController.java,代码:

java 复制代码
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/hello")
    // value 属性定义了资源的名称
    @SentinelResource(value = "hello")
    public String hello() {
        return "Hello Sentinel!";
    }
}
6、启动项目并请求一次

idea启动项目。

请求一次hello请求,必须的,因为只有至少请求一次才会注册到sentinel。

刷新看板,如图:

相关推荐
Promising_GEO1 天前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
我不会起名字3223 天前
Redis 从 0 到 1(九):主库宕机 30 秒自动切主——1 主 2 从 3 哨兵 + Go 客户端
redis·docker·sentinel·高可用·哨兵
Msshu1233 天前
PD快充诱骗取电芯片工作原理,XSP17诱骗取电5V9V12V15V20V功能测试
spring boot·spring cloud·sentinel·java-rocketmq·java-zookeeper·java-consul·java-activemq
流烟默7 天前
Spring Cloud 微服务 Sentinel 热点参数与授权规则集成指南(附架构建议)
spring cloud·微服务·架构·sentinel
Thomas.Sir8 天前
第23课:Sentinel熔断降级、系统自适应保护、集群限流
spring cloud·sentinel
jyOverQ9 天前
Redis 主从复制、Sentinel 和 Cluster 到底有什么区别?
java·redis·sentinel
流烟默11 天前
网关 Sentinel 限流实战:API 分组、规则加载与全量匹配踩坑记
nacos·gateway·sentinel·流控规则
guo_wen_qiang15 天前
云服务器sentinel搭建-控制台
服务器·sentinel
guo_wen_qiang15 天前
项目使用sentinel遇到的问题
java·sentinel
天天喝旺仔1 个月前
分布式服务容错实战:用 Sentinel 实现限流、熔断与降级
分布式·微服务·云原生·sentinel