dubbo 调用过程分析
本章 主要介绍 dubbo 服务和客户端 调用过程。
服务端 接收请求流程
服务端配置
直接采用dubbo 源码例子 dubbo-demo-provider
配置如下:
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880" server="netty4"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" timeout="10000"/>
采用netty4 协议。
服务处理请求流程
说明 :
其实客户端响应处理流程 和 服务端 请求处理流程差不多。 都是 dubboprotocol 的requestHandler 处理。
nettyServer 和nettyClient 创建会有一个 handler 的warp 过程
public NettyServer(URL url, ChannelHandler handler) throws RemotingException {
super(url, ChannelHandlers.wrap(handler, ExecutorUtil.setThreadName(url, SERVER_THREAD_POOL_NAME)));
}
protected ChannelHandler wrapInternal(ChannelHandler handler, URL url) {
return new MultiMessageHandler(new HeartbeatHandler(ExtensionLoader.getExtensionLoader(Dispatcher.class)
.getAdaptiveExtension().dispatch(handler, url)));
}
Dispatcher 默认扩展 是 AllDispatcher。
public class AllDispatcher implements Dispatcher {
public static final String NAME = "all";
public ChannelHandler dispatch(ChannelHandler handler, URL url) {
return new AllChannelHandler(handler, url);
}
}
AllChannelHandler 包含了 线程池处理请求.
遇到的错误:
dubbo 源码 example 服务端代码测试 更改为 netty4 协议 : Unsupported server type: netty4,
Exception in thread “main” com.alibaba.dubbo.rpc.RpcException: Unsupported server type: netty4,
检查发现 dubbo-demo-provider 的pom如下:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-config-spring</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-remoting-netty</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
</dependencies>
因为是分模块引入 所以默认不存在 netty4 代码:
添加如下 依赖解决问题:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>