Dubbo 是如何控制并发数和限流的?

  1. 如果每服务每方法最大可并行执行请求数大于零,那么就基于基于服务 URL + 方法维度获取一个RpcStatus实例

  2. 通过RpcStatus实例获取一个信号量,若果获取的这个信号量调用tryAcquire返回false,则抛出异常

  3. 如果没有抛异常,那么久调用RpcStatus静态方法beginCount,给这个 URL + 方法维度开始计数

  4. 调用服务

  5. 调用结束后计数调用RpcStatus静态方法endCount,计数结束

  6. 释放信号量

ExecuteLimitFilter

@Override

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

URL url = invoker.getUrl();

String methodName = invocation.getMethodName();

Semaphore executesLimit = null;

boolean acquireResult = false;

int max = url.getMethodParameter(methodName, Constants.EXECUTES_KEY, 0);

if (max > 0) {

RpcStatus count = RpcStatus.getStatus(url, invocation.getMethodName());

//            if (count.getActive() >= max) {

/**

* http://manzhizhen.iteye.com/blog/2386408

* use semaphore for concurrency control (to limit thread number)

*/

executesLimit = count.getSemaphore(max);

if(executesLimit != null && !(acquireResult = executesLimit.tryAcquire())) {

throw new RpcException("Failed to invoke method " + invocation.getMethodName() + " in provider " + url + “, cause: The service using threads greater than dubbo:service executes=\“” + max + “\” / limited.”);

}

}

long begin = System.currentTimeMillis();

boolean isSu

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值