Dubbo源码解析(一)请求调度器 Dispatcher

本文详细介绍了Dubbo中的调度器Dispatcher,包括all、direct、message、execution和connection五种策略。分析了各种策略下消息如何被派发到线程池或在IO线程上执行,以及线程池的类型如fixed、cached、limited和eager的工作原理。同时,文章指出了execution策略在官方文档中的描述错误,并给出了相关源码验证。
摘要由CSDN通过智能技术生成

调度器 Dispatcher

  1. 调度策略

    • all 所有消息都派发到线程池,包括请求,响应,连接事件,断开事件,心跳等。
    • direct 所有消息都不派发到线程池,全部在 IO 线程上直接执行。
    • message 只有请求响应消息派发到线程池,其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
    • execution 只请求消息派发到线程池,不含响应,响应和其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
    • connection 在 IO 线程上,将连接断开事件放入队列,有序逐个执行,其它消息派发到线程池。
  2. 线程池 ThreadPool

    • fixed 固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省)
    • cached 缓存线程池,空闲一分钟自动删除,需要时重建。
    • limited 可伸缩线程池,但池中的线程数只会增长不会收缩。只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。
    • eager 优先创建Worker线程池。在任务数量大于corePoolSize但是小于maximumPoolSize时,优先创建Worker来处理任务。当任务数量大于maximumPoolSize时,将任务放入阻塞队列中。阻塞队列充满时抛出RejectedExecutionException。(相比于cached:cached在任务数量超过maximumPoolSize时直接抛出异常而不是将任务放入阻塞队列)

从上面可以看到所有的请求、响应、连接、断开都经过Dispatcher之手。

那我想着具体看下Dispatcher的源码吧

@SPI(AllDispatcher.NAME)
public interface Dispatcher {
   

    /**
     * dispatch the message to threadpool.
     *
     * @param handler
     * @param url
     * @return channel handler
     */
    @Adaptive({Constants.DISPATCHER_KEY, "dispather", "channel.handler"})
    // The last two parameters are reserved for compatibility with the old configuration
    ChannelHandler dispatch(ChannelHandler handler, URL url);

}

哇哇哇哇
这个SPI是什么东西啊?

Marker for extension interface 扩展接口标识

哦,扩展点,暂且认为是Bean这种东西吧
这个Adaptive又是什么东西?

Decide which target extension to be injected. 决定注入那一个扩展点

哦 果然和@AutoWired差不多的意思

我想看有多少个实现,干了什么,先看下类图吧

这里写图片描述

每一种Dispatcher的策略都有一个Dispatcher和一个对应的ChannelHandler

all

回顾一下all的策略

所有消息都派发到线程池,包括请求,响应,连接事件,断开事件,心跳等。

看一下AllDispatcher的实现

public class AllDispatcher implements Dispatcher {
   

    public static final String NAME = "all";

    public ChannelHandler dispatch(ChannelHandler handler, URL url) {
        return new AllChannelHandler(handler, url);
    }

}

So Simple,Why,比如派发策略all应该整个逻辑都在AllChannelHandler这个逻辑里面


public class AllChannelHandler extends WrappedChannelHandler {
   

    public AllChannelHandler(ChannelHandler handler, URL url) {
        super(handler, url);
    }

    public void connected(Channel channel) throws RemotingException {
        ExecutorService cexecutor = getExecutorService();
        try {
            cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.CONNECTED));
        } catch (Throwable t) {
            throw new ExecutionException("connect event", channel, getClass() + " error when process connected event .", t);
        }
    }

    public void disconnected(Channel channel) throws RemotingException {
        ExecutorService cexecutor = getExecutorService();
        try {
            cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.DISCONNECTED));
        } catch (Throwable t) {
            throw new ExecutionException("disconnect event", channel, getClass() + " error when process disconnected event .", t);
        }
    }

    public void received(Channel channel, Object message) throws RemotingException {
        ExecutorService cexecutor = getExecutorService();
        try {
            cexecutor.execute(
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值