SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流
Sentinel是阿里巴巴開源的限流器熔斷器,并且?guī)в锌梢暬僮鹘缑妗?/p>
在日常開發(fā)中,限流功能時常被使用,用于對某些接口進(jìn)行限流熔斷,譬如限制單位時間內(nèi)接口訪問次數(shù);或者按照某種規(guī)則進(jìn)行限流,如限制ip的單位時間訪問次數(shù)等。
之前我們已經(jīng)講過接口限流的工具類ratelimter可以實(shí)現(xiàn)令牌桶的限流,很明顯sentinel的功能更為全面和完善。來看一下sentinel的簡介:
https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel
Sentinel 介紹
隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。Sentinel 以流量為切入點(diǎn),從流量控制、熔斷降級、系統(tǒng)負(fù)載保護(hù)等多個維度保護(hù)服務(wù)的穩(wěn)定性。
Sentinel具有以下特征:
- 豐富的應(yīng)用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發(fā)流量控制在系統(tǒng)容量可以承受的范圍)、消息削峰填谷、實(shí)時熔斷下游不可用應(yīng)用等。
- 完備的實(shí)時監(jiān)控:Sentinel 同時提供實(shí)時的監(jiān)控功能。您可以在控制臺中看到接入應(yīng)用的單臺機(jī)器秒級數(shù)據(jù),甚至 500 臺以下規(guī)模的集群的匯總運(yùn)行情況。
- 廣泛的開源生態(tài):Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應(yīng)的依賴并進(jìn)行簡單的配置即可快速地接入 Sentinel。
- 完善的 SPI 擴(kuò)展點(diǎn):Sentinel 提供簡單易用、完善的 SPI 擴(kuò)展點(diǎn)。您可以通過實(shí)現(xiàn)擴(kuò)展點(diǎn),快速的定制邏輯。例如定制規(guī)則管理、適配數(shù)據(jù)源等。
來簡單使用一下Sentinel。
Sentinel包括服務(wù)端和客戶端,服務(wù)端有可視化界面,客戶端需引入jar后即可和服務(wù)端通信并完成限流功能。
啟動服務(wù)端的jar
https://github.com/alibaba/Sentinel/releases 在這個地址,下載release的jar,然后啟動即可。
這個jar是個標(biāo)準(zhǔn)的Springboot應(yīng)用,可以通過
java -jar sentinel-dashboard-1.6.0.jar來啟動,這樣就是默認(rèn)的設(shè)置,啟動在8080端口。也可以加上一些自定義配置來啟動
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
具體配置的解釋,可以到GitHub上看一下文檔。
這里我們直接使用默認(rèn)java -jar sentinel-dashboard-1.6.0.jar來啟動,之后訪問localhost:8080??梢钥吹浇缑妫?/p>

輸入賬號密碼sentinel后進(jìn)入主界面

此時因?yàn)槲覀儾]有啟動客戶端,所以界面是空的。
啟動客戶端
新建一個Springboot項(xiàng)目,pom如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maimeng.baobanq</groupId>
<artifactId>baobanserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>baobanserver</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--sentinel-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--sentinel end-->
<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>${spring-cloud.version}</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>
需要注意引用的SpringCloud-alibaba的版本是0.2.2,當(dāng)前的最新版,如果是Springboot2.x的項(xiàng)目,需要引0.2.x的。Springboot1.x的引0.1.x的。
Sentinel的客戶端依賴也很簡單,spring-cloud-starter-alibaba-sentinel加這一個引用即可。
之后在application.yml里添加server的地址配置:
spring:
application:
name: baobanserver
cloud:
sentinel:
transport:
dashboard: localhost:8080
#eager: true
另外由于8080端口已被占用,自行設(shè)置一個端口,如8888.
做完這些,新建一個controller,
@RestController
public class TestController {
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel";
}
}
就是一個普通的controller接口。
之后啟動該項(xiàng)目。啟動后回到server的控制臺界面

發(fā)現(xiàn)并沒有什么變化。然后我們調(diào)用一下hello接口。之后再次刷新server控制臺。

界面已經(jīng)出現(xiàn)了我們的項(xiàng)目,并且有一堆規(guī)則。

因?yàn)镾entinel采用延遲加載,只有在主動發(fā)起一次請求后,才會被攔截并發(fā)送給服務(wù)端。如果想關(guān)閉這個延遲,就在上面的yml里把eager的注釋放掉。
然后在簇點(diǎn)鏈路里hello接口的流控那里設(shè)置限流規(guī)則,將單機(jī)閾值設(shè)為1.就代表一秒內(nèi)最多只能通過1次請求到達(dá)該hello接口

之后再次連續(xù)訪問hello接口。

發(fā)現(xiàn)已經(jīng)被攔截了,限流已經(jīng)生效。
這樣就完成了一次簡單的限流操作,并且能看到各接口的QPS的統(tǒng)計(jì)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
- 在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
- springboot?整合sentinel的示例代碼
- 詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實(shí)戰(zhàn)(附源碼)
- springboot整合sentinel的方法教程
- 詳解SpringBoot Redis自適應(yīng)配置(Cluster Standalone Sentinel)
- Springboot 中使用Sentinel的詳細(xì)步驟
相關(guān)文章
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之在線高中考試系統(tǒng)的實(shí)現(xiàn)
這是一個使用了java+SSM+Jsp+Mysql+Maven開發(fā)的在線高中考試系統(tǒng),是一個畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有考試系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-02-02
Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
SpringBoot整合mybatis-generator-maven-plugin的方法
這篇文章主要介紹了SpringBoot整合mybatis-generator-maven-plugin,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
利用MyBatis實(shí)現(xiàn)條件查詢的方法匯總
這篇文章主要給大家介紹了關(guān)于利用MyBatis實(shí)現(xiàn)條件查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Intellij IDEA基于Springboot的遠(yuǎn)程調(diào)試(圖文)
這篇文章主要介紹了Intellij IDEA基于Springboot的遠(yuǎn)程調(diào)試(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java中Queue的poll()和remove()區(qū)別詳解
這篇文章主要介紹了Java中Queue的poll()和remove()區(qū)別詳解,Queue接口提供了許多方法,其中poll()和remove()是兩個常用的方法,它們的區(qū)別在于,當(dāng)隊(duì)列為空時,poll()方法返回null,而remove()方法會拋出,需要的朋友可以參考下2023-07-07

