【Dubbo】消费端服务并发控制

本文主要介绍了Dubbo 2.7.8版本中,如何实现消费端服务的并发控制。通过RpcStatus类和ActiveLimitFilter过滤器,对服务接口的并发调用进行限制。在消费端,当RPC请求前,ActiveLimitFilter会检查并发控制条件,若不满足则挂起当前线程,待满足条件后唤醒或超时抛出异常。同时,源码分析了RpcStatus类的调用统计信息和ActiveLimitFilter的工作机制。
摘要由CSDN通过智能技术生成

一. 概述

版本:2.7.8

解决问题

  • 对服务方法或服务接口中所有服务方法并发调用请求进行限制

调用时机

  • 消费端通过 org.apache.dubbo.rpc.filter.ActiveLimitFilter 进行限制
  • 消费端在使用 DubboInvoker 的 invoke() 方法真正向远端发起RPC请求前,先调用 ActiveLimitFilter 的 invoke() 方法限制

二. 源码分析

RpcStatus类

源码说明:

  1. 该类表示提供端服务接口(包括接口中所有服务方法)、消费端服务接口(包括接口中所有服务方法)的当前调用次数、总数、失败数、调用间隔等状态信息
  2. 代码中有详细注释,重点关注beginCount方法、endCount方法
  3. SERVICE_STATISTICS/METHOD_STATISTICS是静态变量,相当于缓存

package org.apache.dubbo.rpc;

public class RpcStatus {
   

    /**
     * key 为服务接口(url.toIdentityString()),value 为该服务接口中所有方法的 RpcStatus 状态
     * 静态变量:所有服务接口共用(缓存)
     */
    private static final ConcurrentMap<String, RpcStatus> SERVICE_STATISTICS = new ConcurrentHashMap<>();

    /**
     * key 为服务接口(url.toIdentityString()),value 为 ConcurrentMap<String,RpcStatus> 对象,其中key为具体的服务方法,value为服务方法的 RpcStatus 状态
     * 静态变量:所有服务接口共用(缓存)
     */
    private static final ConcurrentMap<String, ConcurrentMap<String, RpcStatus>> METHOD_STATISTICS = new ConcurrentHashMap<>();

    private final ConcurrentMap<String, Object> values = new ConcurrentHashMap<>();

    // 服务方法正在执行中的数量
    private final AtomicInteger active = new AtomicInteger();
    
    // 服务方法调用的总数量
    private final AtomicLong total = new AtomicLong();
    
    // 服务方法调用失败的数量
    private final AtomicInteger failed = new AtomicInteger();
    
    private final AtomicLong totalElapsed = new AtomicLong();
    private final AtomicLong failedElapsed = new AtomicLong();
    private final AtomicLong maxElapsed = new AtomicLong();
    private final AtomicLong failedMaxElapsed = new AtomicLong();
    private final AtomicLong succeededMaxElapsed = new AtomicLong();

    /**
     * 根据 URL 返回服务接口的 RpcStatus 状态
     */
    public static RpcStatus getStatus(URL url) {
   
        String uri = url.toIdentityString();
        return SERVICE_STATISTICS.computeIfAbsent(uri, key -> new RpcStatus());
    }
    
    /**
     * 根据 URL 返回接口中服务方法的 RpcStatus 状态
     */
    public static RpcStatus getStatus(URL url, String methodName) {
   

        String uri = url.toIdentityString();
        ConcurrentMap<String, RpcStatus> map = METHOD_STATISTICS.computeIfAbsent(uri, k -> new ConcurrentHashMap<>(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值