1. 概述
-
中文文档 :https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
-
是一个轻量级的流量控制、熔断降级 Java 库
-
主要特性
2. Sentinel 控制台
- Sentinel 组件由 2 部分组成
- 核心库(Java客户端)不依赖任何框架/库,能够运行于所有Java运行时环境,同时对 Dubbo /Spring Cloud等框架也有较好的支持。
- 控制台(Dashboard)基于Spring Boot 开发,打包后可以直接运行,不需要额外的Tomcat等应用容器。
- 下载 Sentinel
-
前提
确保有 Java 8 的环境,且 8080 端口没有被占用 -
进入存放 Sentinel 的目录,并运行命令
java -jar sentinel-dashboard-1.8.0.jar
-
登录账号密码均为 :sentinel
3. 工程环境搭建
-
新建模块 :alibaba-sentinel-service-8401
-
修改 POM
<dependencies> <dependency> <groupId>com.demo.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
编写 YML
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8080 port: 8719 #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口 management: endpoints: web: exposure: include: '*'
-
编写主启动类
@EnableDiscoveryClient @SpringBootApplication public class SentinelSerMain8401 { public static void main(String[] args) { SpringApplication.run(SentinelSerMain8401.class, args); } }
-
业务类
-
Controller :====
@RestController public class FlowLimitController { @GetMapping("/testA") public String testA() { return "------testA"; } @GetMapping("/testB") public String testB() { return "------testB"; } }
-
启动 Nacos、Sentinel 控制台
-
启动 alibaba-sentinel-service-8401 项目
-
查看 Sentinel 控制台
- 因为 Sentinel 采用的是懒加载
- 执行一下请求
http://localhost:8401/testA
http://localhost:8401/testB - 再刷新 Sentinel 控制台