java sentinel_alibaba限流组件Sentinel实战

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

maven

org.springframework.cloud

spring-cloud-starter-sentinel

0.2.0.BUILD-SNAPSHOT

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-web

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

增加限流规则

4d92e6cd0a35fa6488ce71a6b324a1db.png

{

"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

72c03a10a89d1a395c4ad8b69328f66c.png

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

024229db2428f65e35c00c9f22c13503.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值