序
本文主要研究一下如何使用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>
- spring-cloud-starter-sentinel在maven倉(cāng)庫(kù)沒有的話自己本地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=*
- 這里指定應(yīng)用的端口為8080魂仍,與sentinel server通信端口為7080侦另,sentinel server的地址為localhost:9999
啟動(dòng)dashboard
java -Dserver.port=9999 \
-Dcsp.sentinel.dashboard.server=localhost:9999 \
-Dproject.name=sentinel-dashboard \
-jar sentinel-dashboard.jar
增加限流規(guī)則
訪問應(yīng)用的sentinel端點(diǎn):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": [
"/*"
]
}
}
}
限流驗(yàn)證
wrk -t12 -c1000 -d10s -T30s --latency http://localhost:8080/actuator
訪問dashboard
可以看到每分鐘的拒絕次數(shù),另外也可以通過實(shí)時(shí)監(jiān)控來看圖形化曲線
藍(lán)色的曲線為b_qps罢绽,即被blocked的請(qǐng)求的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)
小結(jié)
這里使用的是spring-cloud-alibaba的組件投队,跟spring-cloud-netlfix類似寝志,是alibaba的開源組件融入spring cloud的部分,現(xiàn)在提供了對(duì)sentinel的集成缸剪,非常方便吗铐。