阿里巴巴

alibaba限流组件Sentinel实战

2018-08-12  本文已影响332人  go4it

本文主要研究一下如何使用alibaba开源的限流组件Sentinel

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>

配置

server.port=8080
spring.application.name=sentinel-demo
spring.cloud.sentinel.port=7080
spring.cloud.sentinel.dashboard=localhost:9999
management.endpoints.web.exposure.include=*

启动dashboard

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

增加限流规则

屏幕快照 2018-08-12 下午9.36.42.png

访问应用的sentinel端点:http://localhost:8080/actuator/sentinel,返回如下:

{
  "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

屏幕快照 2018-08-12 下午9.37.25.png

可以看到每分钟的拒绝次数,另外也可以通过实时监控来看图形化曲线

屏幕快照 2018-08-12 下午9.37.15.png

蓝色的曲线为b_qps,即被blocked的请求的qps

被流控之后,接口返回

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的集成,非常方便。

doc

上一篇下一篇

猜你喜欢

热点阅读