一文搞懂基于 Sentinel Dashboard 进行接口限流
Hello folks,今天我们介绍一下由 Spring Cloud Alibaba 推出的开源项目 Sentinel。Sentinel 是一款面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应保护等多个维度来帮助用户保障微服务的稳定性。
Sentinel 的使用可以基于如下两个部分:
- 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
- 控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。
在进行 Sentinel 实践之前,我们先来了解一下其所涉及的基础理论知识。主要涉及基本概念、原理等。
Sentinel 基本概念
关于 Sentinel,我们先来了解以下概念:
1、资源
资源是 Sentinel 的一项关键因素。其可以是 Java 应用程序中的任何内容,例如,由应用程序提供的服务,或由应用程序调用的其它应用提供的服务,甚至可以是一段代码。在接下来的文档中,我们都会用资源来描述代码块。
只要通过 Sentinel API 定义的代码,就是资源,能够被 Sentinel 保护起来。通常情况下,我们可以使用方法签名,URL,甚至服务名称作为资源名来标识资源。
2、规则
围绕资源的实时状态设定的规则,可以包括流量控制规则、熔断降级规则以及系统保护规则。所有规则可以动态实时调整。
Sentinel 基本原理
在 Sentinel 体系里面,所有的资源都对应一个资源名称以及一个 Entry。Entry 可以通过对主流框架的适配自动创建,也可以通过注解的方式或调用 API 显式创建;每一个 Entry 创建的时候,同时也会创建一系列功能插槽(slot chain)。这些插槽有不同的职责,例如:
1、NodeSelectorSlot 负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级;
2、ClusterBuilderSlot 则用于存储资源的统计信息以及调用者信息,例如该资源的 RT、QPS 以及 Thread Count 等等,这些信息将用作为多维度限流,降级的依据;
3、StatisticSlot 则用于记录、统计不同纬度的 Runtime 指标监控信息;
4、FlowSlot 则用于根据预设的限流规则以及前面 Slot 统计的状态,来进行流量控制;
5、AuthoritySlot 则根据配置的黑白名单和调用来源信息,来做黑白名单控制;
6、DegradeSlot 则通过统计信息以及预设的规则,来做熔断降级;
7、SystemSlot 则通过系统的状态,例如 Load 等,来控制总的入口流量。
Sentinel 总体框架如下所示:
基于上述系统框架所示,Sentinel 的主要工作机制可以概括如下:
1、对主流框架提供适配或者显示的 API,来定义需要保护的资源,并提供设施对资源进行实时统计和调用链路分析。
2、根据预设的规则,结合对资源的实时统计信息,对流量进行控制。同时,Sentinel 提供开放的接口,方便您定义及改变规则。
3、Sentinel 提供实时的监控系统,方便快速了解目前系统的状态。
介绍完 Sentinel 相关的理论知识后,接下来,我们进入本文的主题。
Sentinel-Dashboard 安装
在 https://github.com/alibaba/Sentinel/releases 页面下载最新的版本包,然后通过命令行启动,具体如下所示:
[leonli@Leon usr ] % nohup java -jar sentinel-dashboard-1.8.6.jar > sentinel.log &
[1] 2096
此时,浏览器中输入:http://localhost:8080/#/login ,页面显示如下:
默认情况下,sentinel-dashboard 以 8080 端口启动运行,当然可以进行配置其他端口。此处的登陆凭证信息,我们可以在 sentinel-dashboard 包中的配置文件中获取到。
此时,我们输入账户密码(sentinel/sentinel)登录后,可以看到如下页面:
编写 DemoApplication 程序
1、POM.XML 文件配置
在 Spring Cloud 应用的 pom.xml 中引入Spring Cloud Alibaba 的 Sentinel 模块,具体如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>alibaba-sentinel-rate-limiting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、Resource 定义
在 Spring Cloud 应用中通过 spring.cloud.sentinel.transport.dashboard 参数配置 sentinel dashboard 的访问地址。通常在 application.properties 或 yml 文件中定义,具体如下所示:
spring.application.name=alibaba-sentinel-rate-limiting
server.port=8001
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080
3、编写 Demo 代码
创建应用主类,并提供一个 Rest 接口,具体如下所示:
package com.leon.alibaba.sentinel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Slf4j
@RestController
static class DemoController {
@GetMapping("/devops")
public String devops() {
return "Starting DemoApplication on leon.com.cn";
}
}
}
完成后,启动程序,如下所示:
此时,通过后台进行 Curl 命令操作,有结果返回。具体如下所示:
[leonli@Leon usr ] % curl -i http://localhost:8001/devops/
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 39
Date: Wed, 16 Nov 2022 14:40:16 GMT
Starting DemoApplication on leon.com.cn
再次进入 Sentinel-Dashboard 页面,可以看到如下:
Sentinel 限流实践
接下来,点击左侧的“簇点链路”菜单栏目,基于所调用的 /devops 接口进行“流控”操作,以配置自定义的限流规则,具体如下所示:
点击“流控”操作后,弹出新增流控规则窗口,如下:
点击“新增”按钮后,进入流控规则页面,这里我们基于 QPS 指标,每秒最多允许“ 5 个请求进入”此限流规则进行策略定义。所设置的限流规则将会显示在列表中,此列表显示所有规则,并可以对其进行更新操作。
接下来,我们来验证一下这个限流规则,我们可以尝试一下借用 Curl 命令行快速的调用此接口,以确认其是否会触发限流控制,具体如下所示:
[leonli@Leon ~ ] % curl http://192.168.0.102:8001/devops
Starting DemoApplication on leon.com.cn
[leonli@Leon ~ ] % curl http://192.168.0.102:8001/devops
Starting DemoApplication on leon.com.cn
[leonli@Leon ~ ] % curl http://192.168.0.102:8001/devops
Starting DemoApplication on leon.com.cn
[leonli@Leon ~ ] % curl http://192.168.0.102:8001/devops
Starting DemoApplication on leon.com.cn
[leonli@Leon ~ ] % curl http://192.168.0.102:8001/devops
Blocked by Sentinel (flow limiting)
基于上述的测试结果:我们发现,快速的进行五次 “/deveops” 调用接口之后,第六次调用时被 Sentinel 阻塞了,即进行流量管控。
如上为 Sentinel 及 Dashboard 的 限流实践解析,希望对大家有用。关于 Sentinel更多需要了解的信息,欢迎大家交流、关注!
Adiós !
- EOF -
相关文章
- 工程技术TOP期刊《Engineering》观点评述:基于脑电图的脑-机接口系统在实用化进程中面临的挑战
- Nature子刊 | 可应用于脑机接口的信号处理方法速度提高100倍
- EasyCVR平台基于萤石云SDK接入的设备播放流程及接口调用
- OpenFeign服务接口调用
- 2021年软件测试工具大全(自动化、接口、性能、安全、测试管理)
- 全网最详细的postman接口测试教程,一篇文章满足你
- 接口测试第8讲:基于unittest框架requests自动化测试上
- 接口测试第9讲:基于unittest框架requests自动化测试下
- 《上海悠悠接口自动化平台》体验地址与使用教程
- 接口隔离原则:接口里的方法,你都用得到吗?
- 接口测试|Postman持久化保存
- 基于Python接口自动化测试框架+数据与代码分离(进阶篇)附源码
- 基于脑机接口的闭环运动想象脑电图仿真
- TSINGSEE基于国网B接口的设备注册流程与鉴权干货技术分享
- Go语言圣经–接口约定习题2详解编程语言
- 0Linux环境下的eth0接口使用秘籍(linuxeth)
- Redis实现接口幂等性的技术实践(接口幂等性 redis)
- 脑机接口人体实验首度成功,它用 50 年把科幻电影变为现实
- 基于接口实现java动态代理示例
- 基于NodeJS的前后端分离的思考与实践(三)轻量级的接口配置建模框架