spring cloud网关(zuul)使用RateLimiter限流,使用jMeter性能测试高并发
生活随笔
收集整理的這篇文章主要介紹了
spring cloud网关(zuul)使用RateLimiter限流,使用jMeter性能测试高并发
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原理:使用令牌桶。
固定時間內(nèi)產(chǎn)生一定數(shù)量的令牌,比如設(shè)置1秒產(chǎn)生50個令牌,但是1秒內(nèi)出現(xiàn)了100個用戶并發(fā)訪問,此時只有50個用戶能拿到令牌,剩余50直接阻擋,被限流。
核心代碼,zuu編寫PRE過濾器。
@Component public class LimitFilter extends ZuulFilter {private static final RateLimiter RATE_LIMITER = RateLimiter.create(1);@Overridepublic String filterType() {return FilterConstants.PRE_TYPE;}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() throws ZuulException {RequestContext currentContext = RequestContext.getCurrentContext();if(RATE_LIMITER.tryAcquire()){System.out.println("通過");}else{System.out.println("限流了");// currentContext.setSendZuulResponse(false);//currentContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());}return null;} }使用谷歌guava包在固定時間內(nèi)產(chǎn)生固定數(shù)據(jù)的令牌。比如:
RateLimiter RATE_LIMITER = RateLimiter.create(2);
就是在1秒內(nèi)產(chǎn)生2個令牌。
使用Jmeter性能測試:
先創(chuàng)建測試計劃,選擇Threds(uder)->Thread Group?
然后在Thread Group 中增加Sampler->HTTP Request
設(shè)置每秒用戶并發(fā)數(shù)量。
點擊上面的綠色Start按鈕即可執(zhí)行并發(fā)請求。
總結(jié)
以上是生活随笔為你收集整理的spring cloud网关(zuul)使用RateLimiter限流,使用jMeter性能测试高并发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springCloud Zuul 网关f
- 下一篇: spring cloud微服务间限流,使