文章目录
官方文档
https://sentinelguard.io/zh-cn/docs/parameter-flow-control.html
http://xiajunhust.github.io/2018/12/04/Sentinel-%E7%83%AD%E7%82%B9%E5%8F%82%E6%95%B0%E9%99%90%E6%B5%81%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%8E%9F%E7%90%86/
https://github.com/all4you/sentinel-tutorial
redis弱脚本令牌桶方法
- https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#request-rate-limiter
- 原始github上的版本初始化的令牌桶容量总是满的,当请求的qps大于令牌桶生成速率后,总是会多出一个整个令牌桶的令牌来,造成设置的qps限制不准,对脚本进行了优化,初始化时会对过期时间进行判断
"local filled_tokens = 0 " +
"if delta > ttl then " +
"filled_tokens = 1 " +
"else " +
"filled_tokens = math.min(capacity, last_tokens+(delta*rate)) " + // 填充后可用令牌数
"end " +
测试样本
-
qps请求数为20,qps限制为5qps
-
改进前
QTP-96订阅成功
2020-10-27 10:27:45.578|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319683490222080| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":125,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-96
QTP-101订阅成功
2020-10-27 10:27:45.629|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319684022898688| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":46,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
QTP-107订阅成功
2020-10-27 10:27:45.708|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319684228419584| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":75,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-107
QTP-94订阅成功
2020-10-27 10:27:45.848|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319684551380992| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":139,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-94
QTP-26订阅成功
2020-10-27 10:27:45.898|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319685138583552| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":49,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-26
[getUser] has been protected! Time=1603765665969QTP-20
2020-10-27 10:27:45.979|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319685348298752| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":80,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-20
[getUser] has been protected! Time=1603765666051QTP-20
2020-10-27 10:27:46.052|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319685688037376| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":72,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-20
QTP-20订阅成功
2020-10-27 10:27:46.123|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319685994221568| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":70,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-20
QTP-21订阅成功
2020-10-27 10:27:46.185|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319686296211456| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":60,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-21
QTP-102订阅成功
2020-10-27 10:27:46.236|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319686552064000| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":50,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-102
QTP-96订阅成功
2020-10-27 10:27:46.304|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319686765973504| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":66,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-96
QTP-101订阅成功
2020-10-27 10:27:46.383|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319687063769088| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":75,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
[getUser] has been protected! Time=1603765666686QTP-107
2020-10-27 10:27:46.687|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319687386730496| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":302,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-107
[getUser] has been protected! Time=1603765666791QTP-94
2020-10-27 10:27:46.791|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319688657604608| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":103,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-94
[getUser] has been protected! Time=1603765666834QTP-26
2020-10-27 10:27:46.834|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319689093812224| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":42,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-26
[getUser] has been protected! Time=1603765666905QTP-20
2020-10-27 10:27:46.905|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319689311916032| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":61,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-20
[getUser] has been protected! Time=1603765666979QTP-21
2020-10-27 10:27:46.979|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319689571962880| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":73,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-21
[getUser] has been protected! Time=1603765667020QTP-102
2020-10-27 10:27:47.021|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319689882341376| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":41,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-102
QTP-96订阅成功
2020-10-27 10:27:47.079|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319690104639488| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":46,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-96
QTP-101订阅成功
2020-10-27 10:27:47.130|mework.core.web.DispatchFilter.oFilterInternal|INFO |164319690305966080| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":49,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
测试结果最终只拦住了8个线程,与预期的15个线程有较大的偏差
- 改进后
QTP-25订阅成功
2020-10-27 10:15:50.273|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316683619233792| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":47,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-25
[getUser] has been protected! Time=1603764950312QTP-95
2020-10-27 10:15:50.322|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316683812171776| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":45,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-95
[getUser] has been protected! Time=1603764950360QTP-94
2020-10-27 10:15:50.360|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316684021886976| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":33,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-94
[getUser] has been protected! Time=1603764950417QTP-104
2020-10-27 10:15:50.418|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316684248379392| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":37,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-104
[getUser] has been protected! Time=1603764950471QTP-26
2020-10-27 10:15:50.472|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316684445511680| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":43,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-26
[getUser] has been protected! Time=1603764950509QTP-92
2020-10-27 10:15:50.510|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316684651032576| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":33,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-92
[getUser] has been protected! Time=1603764950566QTP-24
2020-10-27 10:15:50.567|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316684873330688| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":37,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-24
[getUser] has been protected! Time=1603764950614QTP-109
2020-10-27 10:15:50.615|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316685070462976| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":37,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-109
[getUser] has been protected! Time=1603764950672QTP-107
2020-10-27 10:15:50.673|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316685288566784| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":43,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-107
[getUser] has been protected! Time=1603764950710QTP-25
2020-10-27 10:15:50.711|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316685489893376| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":34,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-25
[getUser] has been protected! Time=1603764950768QTP-95
2020-10-27 10:15:50.768|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316685712191488| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":38,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-95
[getUser] has been protected! Time=1603764950816QTP-94
2020-10-27 10:15:50.816|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316685909323776| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":39,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-94
[getUser] has been protected! Time=1603764950870QTP-104
2020-10-27 10:15:50.870|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316686123233280| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":42,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-104
[getUser] has been protected! Time=1603764950911QTP-26
2020-10-27 10:15:50.912|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316686332948480| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":33,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-26
[getUser] has been protected! Time=1603764950964QTP-92
2020-10-27 10:15:50.964|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316686538469376| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":37,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-92
[getUser] has been protected! Time=1603764951014QTP-24
2020-10-27 10:15:51.015|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316686760767488| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":34,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"descr\":\"FORBIDDEN\",\"msg\":\"请求过于频繁,请稍后再试!\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-24
QTP-109订阅成功
2020-10-27 10:15:51.060|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316686962094080| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":31,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-109
QTP-107订阅成功
2020-10-27 10:15:51.116|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316687184392192| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":35,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-107
QTP-25订阅成功
2020-10-27 10:15:51.165|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316687377330176| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":38,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-25
QTP-95订阅成功
2020-10-27 10:15:51.215|mework.core.web.DispatchFilter.oFilterInternal|INFO |164316687587045376| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":38,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-95
最终的限制结果是符合预期的
单机可套用模版
public static final String USER_RES = "userResource";
public static FlowRule rule = new FlowRule();
static {
rule.setResource(USER_RES);
// 限流类型,qps
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 设置阈值
rule.setCount(5);
// 限制哪个调用方
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
// 基于调用关系的流量控制
rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
// 流控策略
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
public void testSentinel(HttpEvent he) throws Throwable {
// 定义热点限流的规则,对第一个参数设置 qps 限流模式,阈值为5
FlowRule rule = new FlowRule();
rule.setResource(USER_RES);
// 限流类型,qps
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 设置阈值
rule.setCount(1);
// 限制哪个调用方
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
// 基于调用关系的流量控制
rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
// 流控策略
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
FlowRuleManager.loadRules(Collections.singletonList(rule));
Entry entry = null;
try {
// 流控
entry = SphU.entry(USER_RES);
// 业务代码
// todo
}catch(BlockException e){
// 被限流了
System.out.println("[getUser] has been protected! Time="+System.currentTimeMillis()+Thread.currentThread().getName());
throw new Exception("当前业务繁忙,请稍后再试!");
}finally {
if(entry!=null){
entry.exit();
}
}
System.out.println(Thread.currentThread().getName()+"订阅成功");
he.writeJson(RtnMonkey.OK);
}
单例可配置模版
public enum ClassFlowRule {
INSTANCE;
private FlowRule rule;
private ClassFlowRule(){
this.rule = new FlowRule();
// 限制资源,定义在TestCmd中
rule.setResource(USER_RES);
// 默认限制qps为5
rule.setCount(5);
// 限流类型,qps
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 限制哪个调用方
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
// 基于调用关系的流量控制
rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
// 流控策略
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
FlowRuleManager.loadRules(Collections.singletonList(this.rule));
}
public FlowRule getInstance(){
return rule;
}
}
@Component
public class TestCmd {
public static final String USER_RES = "userResource";
@Value("${control.sentinel.qps:50}")
private int limitQps;
public void subscribeSchedule(HttpEvent he) throws Throwable {
// 动态更新qps
ClassFlowRule.INSTANCE.getInstance().setCount(limitQps);
// 刷新规则
FlowRuleManager.loadRules(Collections.singletonList(ClassFlowRule.INSTANCE.getInstance()));
Entry entry = null;
try {
// 流控
entry = SphU.entry(USER_RES);
// 业务代码
// todo
}catch(BlockException e){
// 被限流了
System.out.println("[getUser] has been protected! Time="+System.currentTimeMillis()+Thread.currentThread().getName());
throw new Exception(RtnMonkey.FORBIDDEN, "scheduleId", "请求过于频繁,请稍后再试!");
}finally {
if(entry!=null){
entry.exit();
}
}
System.out.println(Thread.currentThread().getName()+"订阅成功");
he.writeJson(RtnMonkey.OK);
}
}
jemeter测试
测试1
- sentinel设置为5qps
- jemeter模拟请求线程数为10,请求qps为10
- 预计结果应该为失败5条,成功5条
输出结构
QTP-17订阅成功
2020-10-26 15:53:04.607|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039164806557696| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":20,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-17
QTP-17订阅成功
2020-10-26 15:53:04.609|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039164882055168| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-17
QTP-106订阅成功
2020-10-26 15:53:04.710|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039165305679872| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-106
QTP-95订阅成功
2020-10-26 15:53:04.810|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039165725110272| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-95
QTP-18订阅成功
2020-10-26 15:53:04.908|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039166140346368| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-18
[getUser] has been protected! Time=1603698785022QTP-21
2020-10-26 15:53:05.024|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039166563971072| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":15,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"msg\":\"请求过于频繁,请稍后再试!\",\"descr\":\"FORBIDDEN\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-21
[getUser] has been protected! Time=1603698785110QTP-92
2020-10-26 15:53:05.110|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039166987595776| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"msg\":\"请求过于频繁,请稍后再试!\",\"descr\":\"FORBIDDEN\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-92
[getUser] has been protected! Time=1603698785208QTP-24
2020-10-26 15:53:05.208|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039167394443264| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"msg\":\"请求过于频繁,请稍后再试!\",\"descr\":\"FORBIDDEN\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-24
[getUser] has been protected! Time=1603698785307QTP-101
2020-10-26 15:53:05.308|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039167813873664| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"msg\":\"请求过于频繁,请稍后再试!\",\"descr\":\"FORBIDDEN\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
[getUser] has been protected! Time=1603698785409QTP-17
2020-10-26 15:53:05.410|mework.core.web.DispatchFilter.oFilterInternal|INFO |164039168241692672| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":403,\"msg\":\"请求过于频繁,请稍后再试!\",\"data\":{\"field\":\"scheduleId\",\"msg\":\"请求过于频繁,请稍后再试!\",\"descr\":\"FORBIDDEN\"}}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-17
输出结果与预期相符合,其中成功的输出为QTP-xx订阅成功,被阻挡的线程为[getUser] has been protected! Time=xxxQTP-xx
测试2
- sentinel配置不变
- jmeter请求数改为1qps
- 预期请求应该全部成功
QTP-21订阅成功
2020-10-26 15:56:31.514|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040032704524288| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-21
QTP-92订阅成功
2020-10-26 15:56:32.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040036919799808| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-92
QTP-24订阅成功
2020-10-26 15:56:33.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040041114103808| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-24
QTP-101订阅成功
2020-10-26 15:56:34.517|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040045304213504| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
QTP-17订阅成功
2020-10-26 15:56:35.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040049502711808| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-17
QTP-21订阅成功
2020-10-26 15:56:36.517|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040053692821504| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-21
QTP-92订阅成功
2020-10-26 15:56:37.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040057891319808| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-92
QTP-24订阅成功
2020-10-26 15:56:38.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040062085623808| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-24
QTP-101订阅成功
2020-10-26 15:56:39.517|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040066275733504| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-101
QTP-17订阅成功
2020-10-26 15:56:40.518|mework.core.web.DispatchFilter.oFilterInternal|INFO |164040070470037504| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"{\"rtn\":0}","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-17
从请求链路入口做限流
- 通过配置拦截期,在入口做处理
<filter>
<filter-name>SentinelCommonFilter</filter-name>
<filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SentinelCommonFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
- 在配置类SentinelConfiguration中的初始化方法中定义的拦截Handle
WebCallbackManager.setUrlBlockHandler(new ApiBlockHandler());
- 在ApiBlockHandler中可以做相应的拦截后置处理,此处自定义了一个简单的TestHandler
public class TestHandler extends DefaultUrlBlockHandler {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
System.out.println("方法被拦截: "+Thread.currentThread().getName());
}
}
- 最后输出的部分结果如下所示
- 请求qps为300,共请求900次,最终成功33条,失败870条(两者不等,虽然相差很小,为什么?)
hello world
2020-10-27 16:05:37.258|mework.core.web.DispatchFilter.oFilterInternal|INFO |164404709590454272| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-441
hello world
2020-10-27 16:05:37.261|mework.core.web.DispatchFilter.oFilterInternal|INFO |164404709603037184| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-439
hello world
2020-10-27 16:05:37.264|mework.core.web.DispatchFilter.oFilterInternal|INFO |164404709611425792| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-441
hello world
2020-10-27 16:05:37.267|mework.core.web.DispatchFilter.oFilterInternal|INFO |164404709624008704| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":1,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-439
hello world
2020-10-27 16:05:37.270|mework.core.web.DispatchFilter.oFilterInternal|INFO |164404709640785920| {"bizType":"ApiAccess","userId":"-","remoteIP":"127.0.0.1","statusCode":200,"requestRunningTimeMS":0,"requestProtocol":"HTTP/1.1","requestHost":"127.0.0.1:8000","requestPort":8000,"requestUri":"/wxOnlineCamp/subscribeSchedule","requestQueryString":null,"requestContent":"","responseContent":"","cookie":{},"curl":"curl -X POST \"http://127.0.0.1:8000/wxOnlineCamp/subscribeSchedule\";"} QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
方法被拦截: QTP-439
方法被拦截: QTP-441
...
配置规则的持久化处理
- 参考下面网址,需要在源码上做相应修改,并增加一些配置,此时在展板上新增的配置,才会持久化到nacos中,本地程序关闭后,相关流控配置规则仍然是存在的
https://www.cnblogs.com/shiraishi/p/11232564.html
全链路压测
- todo
概述性文章
https://www.baidu.com/link?url=k6UT5u2GUOB5wD9xtABUefZBUqYei7HS1rrmwHLEK6VywafLBIDEtP4D0-XhUcIMMlWGJoZFwzL3kyfNSX_r5_&wd=&eqid=a815f9a6001a7357000000065f98d1b4https://www.jianshu.com/p/27060fd61f72
压测demo
https://www.cnblogs.com/imyalost/p/11811142.html美团
https://tech.meituan.com/2019/02/14/full-link-pressure-test-automation.htmljmeter
https://blog.csdn.net/wuli13141516/article/details/105396866
https://mp.weixin.qq.com/s?spm=a2c6h.12873639.0.0.529232b4oIFEr9&__biz=MzU4NzU0MDIzOQ==&mid=2247487005&idx=1&sn=9c8837261ce97f69f019fe909e09e789&chksm=fdeb3c7dca9cb56b6c142d52f08eae5143cfc38bf214c2e716f65202f83ffd9325b845da48dc&scene=21#wechat_redirectQuake 还集成了公司内部统一的监控组件,有监控机器指标的 Falcon 系统(小米开源),还有监控服务性能的 CAT系统(美团已经开源)。Quake 提供了统一的管理配置服务,让业务能在 Quake 上方便观察整个系统的健康状况。
tps
https://www.cnblogs.com/qmfsun/p/5511557.html
特点
- 在实际的业务场景中,系统服务除了受自身的影响,还依赖于其他关联系统的情况,并且影响会一直蔓延,只要有一个节点出现故障, 经过层层累加,最终造成的影响是不可能通过单机压测暴露出来的。
gatling压测
- todo
https://www.cnblogs.com/yanghj010/p/9575269.html