Sentinel是阿里巴巴開源的限流器熔斷器,而且帶有可視化操做界面。java
在平常開發中,限流功能時常被使用,用於對某些接口進行限流熔斷,譬如限制單位時間內接口訪問次數;或者按照某種規則進行限流,如限制ip的單位時間訪問次數等。nginx
以前咱們已經講過接口限流的工具類ratelimter能夠實現令牌桶的限流,很明顯sentinel的功能更爲全面和完善。來看一下sentinel的簡介:git
https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinelgithub
隨着微服務的流行,服務和服務之間的穩定性變得愈來愈重要。Sentinel 以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。web
Sentinel 具備如下特徵:spring
豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量能夠承受的範圍)、消息削峯填谷、實時熔斷下游不可用應用等。apache
完備的實時監控:Sentinel 同時提供實時的監控功能。您能夠在控制檯中看到接入應用的單臺機器秒級數據,甚至 500 臺如下規模的集羣的彙總運行狀況。併發
普遍的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只須要引入相應的依賴並進行簡單的配置便可快速地接入 Sentinel。app
完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展點。您能夠經過實現擴展點,快速的定製邏輯。例如定製規則管理、適配數據源等。框架
來簡單使用一下Sentinel。
Sentinel包括服務端和客戶端,服務端有可視化界面,客戶端需引入jar後便可和服務端通訊並完成限流功能。
https://github.com/alibaba/Sentinel/releases 在這個地址,下載release的jar,而後啓動便可。
這個jar是個標準的Springboot應用,能夠經過
java -jar sentinel-dashboard-1.6.0.jar來啓動,這樣就是默認的設置,啓動在8080端口。也能夠加上一些自定義配置來啓動
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar。
具體配置的解釋,能夠到GitHub上看一下文檔。
這裏咱們直接使用默認java -jar sentinel-dashboard-1.6.0.jar來啓動,以後訪問localhost:8080。能夠看到界面:
輸入帳號密碼sentinel後進入主界面
此時由於咱們並無啓動客戶端,因此界面是空的。
新建一個Springboot項目,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,當前的最新版,若是是Springboot2.x的項目,須要引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端口已被佔用,自行設置一個端口,如8888.
作完這些,新建一個controller,
@RestController
public class TestController {
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel";
}}
就是一個普通的controller接口。
以後啓動該項目。啓動後回到server的控制檯界面
發現並無什麼變化。而後咱們調用一下hello接口。以後再次刷新server控制檯。
界面已經出現了咱們的項目,而且有一堆規則。
由於Sentinel採用延遲加載,只有在主動發起一次請求後,纔會被攔截併發送給服務端。若是想關閉這個延遲,就在上面的yml裏把eager的註釋放掉。
而後在簇點鏈路裏hello接口的流控那裏設置限流規則,將單機閾值設爲1.就表明一秒內最多隻能經過1次請求到達該hello接口。
以後再次連續訪問hello接口。
發現已經被攔截了,限流已經生效。
這樣就完成了一次簡單的限流操做,而且能看到各接口的QPS的統計。