springBoot整合alibaba-sentinel實現接口限流

原文鏈接:https://blog.csdn.net/yangliuhbhd/article/details/89948169

參考博客: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

  1. 默認登錄用戶:sentinel
  2. pwd:sentinel

4、創建項目springboot-sentinel添加sentinel的pom依賴

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.4.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.willow</groupId>
  12. <artifactId>springboot-sentinel</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-sentinel</name>
  15. <description>springboot-sentinel</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  27. <version>0.2.0.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

5、springboot-sentinel項目中properties添加配置

  1. server.port=8081
  2. spring.application.name=sentine
  3. # sentinel-dashboard-1.6.0 的訪問路徑 ,啓動方式java -jar sentinel-dashboard-1.6.0.jar
  4. spring.cloud.sentinel.transport.dashboard=localhost:8082
  5. #取消Sentinel控制檯懶加載
  6. spring.cloud.sentinel.eager=true

6、springboot-sentinel項目添加controller測試限流

  1. /**
  2.      * @Auther: willow
  3.      * @Date: 2019/5/7 17:42
  4.      * @Description:
  5.      */
  6.     @Controller
  7.     public class SentinelController {
  8.         @RequestMapping("/sentinel")
  9.         @ResponseBody
  10.         public String sentinel(){
  11.             return "sentinel ....";
  12.         }
  13.     }

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章