Sentinel是什么
Sentinel的官方標題是:分布式系統的流量防衛兵。從名字上來看,很容易就能猜到它是用來作服務穩定性保障的。對於服務穩定性保障組件,如果熟悉Spring Cloud的用戶,第一反應應該就是Hystrix。但是比較可惜的是Netflix已經宣布對Hystrix停止更新。那么,在未來我們還有什么更好的選擇呢?除了Spring Cloud官方推薦的resilience4j之外,目前Spring Cloud Alibaba下整合的Sentinel也是用戶可以重點考察和選型的目標。
Sentinel的功能和細節比較多,一篇內容很難介紹完整。所以下面我會分多篇來一一介紹Sentinel的重要功能。本文就先從限流入手,說說如何把Sentinel整合到Spring Cloud應用中,以及如何使用Sentinel Dashboard來配置限流規則。通過這個簡單的例子,先將這一套基礎配置搭建起來。
使用Sentinel實現接口限流
Sentinel的使用分為兩部分:
sentinel-dashboard:與hystrix-dashboard類似,但是它更為強大一些。除了與hystrix-dashboard一樣提供實時監控之外,還提供了流控規則、熔斷規則的在線維護等功能。
客戶端整合:每個微服務客戶端都需要整合sentinel的客戶端封裝與配置,才能將監控信息上報給dashboard展示以及實時的更改限流或熔斷規則等。
下面我們就分兩部分來看看,如何使用Sentienl來實現接口限流。
部署Sentinel Dashboard
使用Spring Cloud Alibaba 0.2.2,由於該版本中升級了Sentinel到1.5.2,所以對sentinel-dashboard做一次升級。但是sentinel-dashboard的1.5.2版本的打包文件沒有提供下載,如果一定要該版本的話,需要自己編譯。這里筆者嘗試了一下直接使用1.6.0的sentinel-dashboard,暫時也沒有發現什么問題,所以就以這個版本為例。
下載地址:sentinel-dashboard-1.6.0.jar https://github.com/alibaba/Sentinel/releases/download/1.6.0/sentinel-dashboard-1.6.0.jar
其他版本:Sentinel/releases
同以往的Spring Cloud教程一樣,這里也不推薦大家跨版本使用,不然可能會出現各種各樣的問題。
通過命令啟動:
java -jar sentinel-dashboard-1.6.0.jar --server port =8888 后面是指定端口的,
在瀏覽器輸入localhost:8888
然后使用項目整合:
pom.xml
<?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.cxy</groupId> <artifactId>sentinel</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sentinel</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置:
spring.application.name=alibaba-sentinel-rate-limiting server.port=8001 # sentinel dashboard spring.cloud.sentinel.transport.dashboard=localhost:8888
啟動類:
package com.cxy; 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 SentinelApplication { public static void main(String[] args) { SpringApplication.run(SentinelApplication.class, args); } @Slf4j @RestController static class TestController { @GetMapping("/hello") public String hello() { return "didispace.com"; } } }
訪問:
進行限流:
說明在控制台的限流是生效了