0.1概念
一个强大的流控制组件,可实现微服务的可靠性,弹性和监视。
Sentinel以“流”为切入点,并在流控制, 流量整形,电路中断和系统自适应保护等多个领域工作,以确保微服务的可靠性和弹性。
Sentinel具有以下功能:
丰富的适用场景
实时监控
广泛的开源生态系统
多种语言支持
官网:https://github.com/alibaba/Sentinel
下载:https://github.com/alibaba/Sentinel/releases
怎么玩:服务雪崩、服务降级、服务熔断、服务限流
0.2安装
sentinel分为两个部分:
核心库:不依赖任何框架/库,能够运行于所有java运行时环境,同时对Dubbo/Spring Cloud等框架也有较好的支持。
控制台(Dashboard):基于springboot开发,打包后可以直接运行,不需要额外的Tomcat等应用容器。
安装步骤:
本次下载sentinel-dashboard-1.7.0.jar这个版本
运行命令,到sentinel的所在目录下:java -jar sentinel-dashboard-1.7.0.jar
访问sentinel管理界面:http://localhost:8080/#/login,账号密码均为sentinel
0.3Sentinel的初步使用
(1)先启动Nacos注册中心
(2)创建Maven工程作为服务提供者(cloudalibab-sentinel-service8401)
(3)添加Nacos和Sentinel依赖
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--服务注册中心依赖-->
(4)application.yml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务注册中心地址
sentinel:
transport:
dashboard: localhost:8080 #配置sentinel 控制台(dashboard)地址,使8080监控8401
port: 8719 #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口
management: #暴露监控的端点
endpoints:
web:
exposure:
include: '*'
(5)Controller
@RestController
public class FlowLimitController
{
@GetMapping("/testA")
public String testA() throws InterruptedException {
return "------testA";
}
@GetMapping("/testB")
public String testB() {
return "------testB";
}
}
(5)启动类上加@EnableDiscoveryClient
(6)测试:
启动Se