学完那么多章了,终于到remoting了,相信大家也很熟悉了,再学了netty和rocketmq之后,dubbo的remoting 也很明显了
先看下dubbo的介绍
Transporter
看看是怎么定义交换的,两个接口 bind 根据URL和ChannelHandler 生成Server connect 根据URL和ChannelHandler
@SPI("netty")
public interface Transporter {
/**
* Bind a server.
*
* @see com.alibaba.dubbo.remoting.Transporters#bind(URL, Receiver, ChannelHandler)
* @param url server url
* @param handler
* @return server
* @throws RemotingException
*/
@Adaptive({Constants.SERVER_KEY, Constants.TRANSPORTER_KEY})
Server bind(URL url, ChannelHandler handler) throws RemotingException;
/**
* Connect to a server.
*
* @see com.alibaba.dubbo.remoting.Transporters#connect(URL, Receiver, ChannelListener)
* @param url server url
* @param handler
* @return client
* @throws RemotingException
*/
@Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY})
Client connect(URL url, ChannelHandler handler) throws RemotingException;
}
Endpoint
Client和Server都继承与Endpoint
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.remoting;
import java.net.InetSocketAddress;
import com.alibaba.dubbo.common.URL;
/**
* Endpoint. (API/SPI, Prototype, ThreadSafe)
*
* @see com.alibaba.dubbo.remoting.Channel
* @see com.alibaba.dubbo.remoting.Client
* @see com.alibaba.dubbo.remoting.Server
* @author william.liangf
*/
public interface Endpoint {
/**
* get url.
*
* @return url
*/
URL getUrl();
/**
* get channel handler.
*
* @return channel handler
*/
ChannelHandler getChannelHandler();
/**
* get local address.
*
* @return local address.
*/
InetSocketAddress getLocalAddress();
/**
* send message.
*
* @param message
* @throws RemotingException
*/
void send(Object message) throws RemotingException;
/**
* send message.
*
* @param message
* @param sent 是否已发送完成
*/
void send(Object message, boolean sent) throws RemotingException;
/**
* close the channel.
*/
void close();
/**
* Graceful close the channel.
*/
void close(int timeout);
/**
* is closed.
*
* @return closed
*/
boolean isClosed();
}
ChannelHandler
消息事件操作,里面都是对channel的操作
@SPI
public interface ChannelHandler {
/**
* on channel connected.
*
* @param channel channel.
*/
void connected(Channel channel) throws RemotingException;
/**
* on channel disconnected.
*
* @param channel channel.
*/
void disconnected(Channel channel) throws RemotingException;
/**
* on message sent.
*
* @param channel channel.
* @param message message.
*/
void sent(Channel channel, Object message) throws RemotingException;
/**
* on message received.
*
* @param channel channel.
* @param message message.
*/
void received(Channel channel, Object message) throws RemotingException;
/**
* on exception caught.
*
* @param channel channel.
* @param exception exception.
*/
void caught(Channel channel, Throwable exception) throws RemotingException;
}
Codec2
序列化接口@SPI
public interface Codec2 {
@Adaptive({Constants.CODEC_KEY})
void encode(Channel channel, ChannelBuffer buffer, Object message) throws IOException;
@Adaptive({Constants.CODEC_KEY})
Object decode(Channel channel, ChannelBuffer buffer) throws IOException;
enum DecodeResult {
NEED_MORE_INPUT, SKIP_SOME_INPUT
}
}
Dispatcher
定义channelHandler对Channel的操作,那些走线程池
@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"}) // 后两个参数为兼容旧配置
ChannelHandler dispatch(ChannelHandler handler, URL url);
}