阿里Sentinel熔断限流
阿里Sentinel熔断限流概述
功能特点
1. 丰富的应用场景:例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等
完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
2 .广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
3.完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel配置
下载sentinel jar包
Spring Boot项目中引入依赖
<!--引入Sentinel依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
配置yml文件
server:
port: 8010
tomcat:
max-threads: 1500
min-spare-threads: 300
uri-encoding: UTF-8
spring:
application:
name: demo-service
cloud:
sentinel:
transport:
dashboard: localhost:8080
#取消延时加载
eager: true
配置启动类
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class DemoServiceApplication {
public static void main( String[] args ) {
// Linux下运行报下面错误时使用下面这行代码,Windows下运行Sentinel不要使用
//Failed to fetch metric from <http://15.128.1.216:8719/metric?startTime=1574901081000&endTime=1574901087000&refetch=false> (ConnectionException: Connection timed out)
System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, "localhost");
SpringApplication.run(DemoServiceApplication.class, args);
}
}
Sentinel使用
启动sentinel jar包
执行命令 java -jar .\sentinel-dashboard-1.8.1.jar
启动SpringBoot项目
编写测试方法
打开Sentinel 控制台设置参数
1.输入用户名密码登录 sentinel sentinel
2.打开点击簇点链路
3.选择接口进行流控设置
每秒钟最多请求一次,超过1次,返回快速失败(浏览器看到Blocked by Sentinel (flow limiting)信息)
访问接口进行测试
1.正常情况下接口返回
2.请求超出设置值时返回
常见问题
将控制台部署在公网,本机启动连接出现错误日志
Failed to fetch metric from <http://15.128.1.216:8719/metric?startTime=1574901081000&endTime=1574901087000&refetch=false> (ConnectionException: Connection timed out)
解决方式,在启动类加上设置
System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, "localhost");