[case32]alibaba限流組件Sentinel實戰

本文主要研究一下如何使用alibaba開源的限流組件Sentineljava

maven

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sentinel</artifactId>
            <version>0.2.0.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • spring-cloud-starter-sentinel在maven倉庫沒有的話本身本地install一下

配置

server.port=8080
spring.application.name=sentinel-demo
spring.cloud.sentinel.port=7080
spring.cloud.sentinel.dashboard=localhost:9999
management.endpoints.web.exposure.include=*
  • 這裏指定應用的端口爲8080,與sentinel server通訊端口爲7080,sentinel server的地址爲localhost:9999

啓動dashboard

java -Dserver.port=9999 \
-Dcsp.sentinel.dashboard.server=localhost:9999 \
-Dproject.name=sentinel-dashboard \
-jar sentinel-dashboard.jar

增長限流規則

圖片描述

訪問應用的sentinel端點:http://localhost:8080/actuator/sentinel,返回以下:git

{
  "DegradeRules": [],
  "SystemRules": [],
  "FlowRules": [
    {
      "resource": "/actuator",
      "limitApp": "default",
      "grade": 1,
      "count": 10,
      "strategy": 0,
      "refResource": null,
      "controlBehavior": 0,
      "warmUpPeriodSec": 10,
      "maxQueueingTimeMs": 500
    }
  ],
  "properties": {
    "enabled": true,
    "port": "7080",
    "dashboard": "localhost:9999",
    "filter": {
      "order": -2147483648,
      "urlPatterns": [
        "/*"
      ]
    }
  }
}

限流驗證

wrk -t12 -c1000 -d10s -T30s  --latency http://localhost:8080/actuator

訪問dashboard

圖片描述

能夠看到每分鐘的拒絕次數,另外也能夠經過實時監控來看圖形化曲線

圖片描述

藍色的曲線爲b_qps,即被blocked的請求的qps

被流控以後,接口返回github

curl -i http://localhost:8080/actuator
HTTP/1.1 200
Transfer-Encoding: chunked
Date: Sun, 12 Aug 2018 13:41:15 GMT

Blocked by Sentinel (flow limiting)

小結

這裏使用的是spring-cloud-alibaba的組件,跟spring-cloud-netlfix相似,是alibaba的開源組件融入spring cloud的部分,如今提供了對sentinel的集成,很是方便。web

doc

相關文章
相關標籤/搜索