參考博客:https://blog.csdn.net/yangliuhbhd/article/details/89948169
https://blog.csdn.net/weixin_38336658/article/details/88549959
Sentinel 介紹
隨着微服務的流行,服務和服務之間的穩定性變得越來越重要。 Sentinel 以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。
Sentinel 具有以下特徵:
豐富的應用場景: Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的範圍)、消息削峯填谷、實時熔斷下游不可用應用等。
完備的實時監控: Sentinel 同時提供實時的監控功能。您可以在控制檯中看到接入應用的單臺機器秒級數據,甚至 500 臺以下規模的集羣的彙總運行情況。
廣泛的開源生態: Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。
完善的 SPI 擴展點: Sentinel 提供簡單易用、完善的 SPI 擴展點。您可以通過實現擴展點,快速的定製邏輯。例如定製規則管理、適配數據源等。
1、下載Sentinel服務端jar
訪問:https://github.com/alibaba/Sentinel/releases 下載 sentinel-dashboard-1.6.0.jar
2、啓動Sentinel服務端後臺管理
執行命令:java -Dserver.port=8082 -jar sentinel-dashboard-1.6.0.jar 默認8080端口
3、訪問 localhost:8082
- 默認登錄用戶:sentinel
- pwd:sentinel
4、創建項目springboot-sentinel添加sentinel的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.1.4.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.willow</groupId>
- <artifactId>springboot-sentinel</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot-sentinel</name>
- <description>springboot-sentinel</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
-
-
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- <version>0.2.0.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
5、springboot-sentinel項目中properties添加配置
- server.port=8081
- spring.application.name=sentine
- # sentinel-dashboard-1.6.0 的訪問路徑 ,啓動方式java -jar sentinel-dashboard-1.6.0.jar
- spring.cloud.sentinel.transport.dashboard=localhost:8082
- #取消Sentinel控制檯懶加載
- spring.cloud.sentinel.eager=true
6、springboot-sentinel項目添加controller測試限流
- /**
- * @Auther: willow
- * @Date: 2019/5/7 17:42
- * @Description:
- */
- @Controller
- public class SentinelController {
- @RequestMapping("/sentinel")
- @ResponseBody
- public String sentinel(){
- return "sentinel ....";
- }
- }
7、啓動創建的springboot-sentinel項目
訪問項目路徑 http://localhost:8081/sentinel
訪問sentinel管理後臺 http://localhost:8082/ 如圖 ,可以看到已經記錄/sentinel路徑訪問一次了
點擊流控設置QPS爲2
多次連續點擊訪問:http://localhost:8081/sentinel
每秒的前2次返回數據正常,後面可以看到瀏覽器返回:
Blocked by Sentinel (flow limiting)
</div>