RocketMQ源码 Namesrv-RemotingServer网络通信服务器

前言

RocketMQ在启动 Namesrv 的过程中会初始化 NamesrvController 核心组件,NamesrvController初始化过程中 又引用了 RemotingServer 网络通信服务器组件,它负责对管理的Borker、Producer、Consumer建立netty长连接(Channel),并注册处理请求的Netty请求处理组件,且可以对Borker、Client发起同步、异步、Oneway形式的RPC请求。

源码版本:4.9.3

接口核心数据行为

RemotingServer网络通信服务器接口行为:


public interface RemotingServer extends RemotingService {
    /**
     * 针对一个请求,注册一个请求处理器组件,同时可以基于线程池进行异步处理
     * @param requestCode
     * @param processor
     * @param executor
     */
    void registerProcessor(final int requestCode, final NettyRequestProcessor processor,
        final ExecutorService executor);

    /**
     * 注册一个针对所有请求的通用请求处理器组件,同时可以基于线程次异步执行
     * @param processor
     * @param executor
     */
    void registerDefaultProcessor(final NettyRequestProcessor processor, final ExecutorService executor);

    /**
     * 监听本地处理端口
     * @return
     */
    int localListenPort();

    /**
     * 获取某个请求,对应的请求处理器组件和执行线程池
     * @param requestCode
     * @return
     */
    Pair<NettyRequestProcessor, ExecutorService> getProcessorPair(final int requestCode);

    /**
     * 针对一个netty长连接,发起一次同步RPC请求协议,同时可以设置超时时间
     * @param channel
     * @param request
     * @param timeoutMillis
     * @return
     * @throws InterruptedException
     * @throws RemotingSendRequestException
     * @throws RemotingTimeoutException
     */
    RemotingCommand invokeSync(final Channel channel, final RemotingCommand request,
        final long timeoutMillis) throws InterruptedException, RemotingSendRequestException,
        RemotingTimeoutException;

    /**
     * 针对一个Netty长连接(Channel),发起一次异步请求协议,可以直接返回结果,
     * 也可以设置Invoke Callback回调函数,同时可以设置超时时间
     * @param channel
     * @param request
     * @param timeoutMillis
     * @param invokeCallback
     * @throws InterruptedException
     * @throws RemotingTooMuchRequestException
     * @throws RemotingTimeoutException
     * @throws RemotingSendRequestException
     */
    void invokeAsync(final Channel channel, final RemotingCommand request, final long timeoutMillis,
        final InvokeCallback invokeCallback) throws InterruptedException,
        RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException;

    /**
     * 针对netty长连接,发起一次RPC请求(单向请求,不关心发送结果是否成功),同时可以设置超时时间
     * @param channel
     * @param request
     * @param timeoutMillis
     * @throws InterruptedException
     * @throws RemotingTooMuchRequestException
     * @throws RemotingTimeoutException
     * @throws RemotingSendRequestException
     */
    void invokeOneway(final Channel channel, final RemotingCommand request, final long timeoutMillis)
        throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException,
        RemotingSendRequestException;

}

NettyRequestProcessor请求处理接口核心行为:

public interface NettyRequestProcessor {
    /**
     * 处理网络通信请求
     * @param ctx netty长连接处理上线文
     * @param request 网络通信请求协议
     * @return
     * @throws Exception
     */
    RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request)
        throws Exception;

    boolean rejectRequest();

}

RemotingCommand 网络通信协议:

public class RemotingCommand {
    private int code; //请求编号
    private LanguageCode language = LanguageCode.JAVA; //编程语言
    private int version = 0; //请求版本号
    private int opaque = requestId.getAndIncrement(); //请求自增id
    private int flag = 0; //标记
    private String remark; //备注
    private HashMap<String, String> extFields; //扩展字段
    private transient CommandCustomHeader customHeader; //自定义消息头

    private SerializeType serializeTypeCurrentRPC = serializeTypeConfigInThisServer; //序列化类型,默认JSON

    private transient byte[] body; //消息体,通信内容会以字节数组的形式存储在body中
}

接口实现类

后续补充。。。

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值