dubbo线程模型

博客提及查看Java线程池的包结构,列举了FixedThreadPool、CachedThreadPool等线程池,还给出了转载来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看下包结构

@SPI("fixed")
public interface ThreadPool {

    /**
     //使用 URL.threadpool 属性加载对应的实现
     */
    @Adaptive({"threadpool"})
    Executor getExecutor(URL url);

}
复制代码

FixedThreadPool

public class FixedThreadPool implements ThreadPool {

    @Override
    public Executor getExecutor(URL url) {
        //线程名
        String name = url.getParameter("threadname", "Dubbo");
        // 线程数
        int threads = url.getParameter("threads", 200);
        // 队列数
        int queues = url.getParameter("queues", 0);
        // 创建执行器
        return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
                queues == 0 ? new SynchronousQueue<Runnable>() :
                        (queues < 0 ? new LinkedBlockingQueue<Runnable>()
                                : new LinkedBlockingQueue<Runnable>(queues)),
                new NamedInternalThreadFactory(name, true), new AbortPolicyWithReport(name, url));
    }
    //根据不同的队列数,使用不同的队列实现
    //queues == 0 , SynchronousQueue
    //queues < 0 , LinkedBlockingQueue
    //queues > 0 ,带队列数的 LinkedBlockingQueue 
复制代码

CachedThreadPool

 缓存线程池,空闲一定时长,自动删除,需要时重建
 */
public class CachedThreadPool implements ThreadPool {

    @Override
    public Executor getExecutor(URL url) {
        // 线程池名
        String name = url.getParameter("threadname", "Dubbo");
        //核心线程数
        int cores = url.getParameter("corethreads", 0);
        // 最大线程数
        int threads = url.getParameter("threads", Integer.MAX_VALUE);
        // 队列数
        int queues = url.getParameter("queues", 0);
        // 线程存活时长
        int alive = url.getParameter("alive", 60 * 1000);
        
        return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS,
                queues == 0 ? new SynchronousQueue<Runnable>() :
                        (queues < 0 ? new LinkedBlockingQueue<Runnable>()
                                : new LinkedBlockingQueue<Runnable>(queues)),
                new NamedInternalThreadFactory(name, true), new AbortPolicyWithReport(name, url));
    }
}
复制代码

//LimitedThreadPool

 可伸缩线程池,但池中的线程数只会增长不会收缩。只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。
 */
public class LimitedThreadPool implements ThreadPool {

    @Override
    public Executor getExecutor(URL url) {
        // 线程名
        String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
        // 核心线程数
        int cores = url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS);
        // 最大线程数
        int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
        // 队列数
        int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
        //和 CachedThreadPool 实现是基本一致的,差异点在 alive == Integer.MAX_VALUE ,空闲时间无限大,即不会自动删除。
        return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS,
                queues == 0 ? new SynchronousQueue<Runnable>() :
                        (queues < 0 ? new LinkedBlockingQueue<Runnable>()
                                : new LinkedBlockingQueue<Runnable>(queues)),
                new NamedInternalThreadFactory(name, true), new AbortPolicyWithReport(name, url));
    }

}
复制代码

转载于:https://juejin.im/post/5bf2b7fde51d450a964236c4

### Dubbo 客户端线程模型架构与工作原理 Dubbo客户端的线程模型设计旨在高效处理远程服务调用并管理并发请求。通过自适应扩展机制,可以根据URL参数动态调整线程池配置[^1]。 #### 线程池初始化 当启动Dubbo消费者应用时,会依据配置文件或默认设置创建线程池实例。此过程涉及解析`dubbo.consumer.threadpool`属性及其关联参数,如核心线程数(`corethreads`)、最大线程数(`maxthreads`)等。这些设定直接影响后续RPC调用期间资源分配策略。 ```properties # 配置示例 dubbo.consumer.threadpool=fixed dubbo.consumer.corethreads=200 dubbo.consumer.maxthreads=1000 ``` #### 请求提交流程 每当发起一次远端方法调用(DemoService.sayHello()),实际执行路径如下: - **代理对象**接收到API接口调用后封装成Invocation对象; - 调度器根据负载均衡算法挑选合适的服务提供方节点; - 将准备好的消息体发送至目标地址前,先经过过滤链(Filter Chain)做预处理操作; - 最终由Transporter负责建立物理连接并向对方传递二进制流数据; 在此过程中,所有异步任务均交予预先构建好的ExecutorService来承担,确保主线程不会因等待响应而阻塞过久。 #### 响应回调机制 一旦网络层确认已成功送达指令包,则立即触发监听事件通知业务逻辑继续推进。此时如果启用了Future模式,则允许应用程序提前返回临时结果给前端展示,待真正完成后再更新最终状态。 对于同步场景,默认情况下将一直挂起直到获得确切答复为止。不过得益于灵活可配的超时控制选项,可以有效防止长时间无回应造成的死锁现象发生。 ```java // Java代码片段示意如何使用CompletableFuture优雅地处理异步回调 public CompletableFuture<String> asyncSayHello(String name){ return RpcContext.getContext().getCompletableFuture(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值