seata协议解析

代码入口

io.seata.core.rpc.netty.NettyClientBootstrap#start

bootstrap.handler(
            new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast(
                        new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(),
                            nettyClientConfig.getChannelMaxWriteIdleSeconds(),
                            nettyClientConfig.getChannelMaxAllIdleSeconds()))
                        .addLast(new ProtocolV1Decoder())
                        .addLast(new ProtocolV1Encoder());
                    if (channelHandlers != null) {
                        addChannelPipelineLast(ch, channelHandlers);
                    }
                }
            });

io.seata.core.rpc.netty.v1.ProtocolV1Encoder#encode

io.seata.core.protocol.RpcMessage

协议规则

2个字节的模数([-38,-38])| 1个字节的版本号 | 6个字节的长度 | 1个字节的messageType | 一个字节的codec值| 一个字节的compressor | 4个字节的messagId值 | header数据值(2个字节的key长度 | value数据值) | 业务数据值

codec: 序列化类型:如 seata,kryo;protobuf;fst

compressor: 压缩类型:如 NONE ,GZIP,BZIP2,LZ4

6个字节的长度:

4个字节(全部数据的长度): 默认长度16+headLength+bodyLength

2个字节的(headLength),默认长度16

业务数据序列化

io.seata.serializer.seata.SeataSerializer#serialize

2个字节(typecode的值)| body

typecode: 消息类型序编码器 如(RegisterTMRequestCodec,BranchRollbackRequestCodec)

body:

2个字节(xid的长度)| xid的数据| 8个字节(branchId值)| 一个byte(分支事务类型ordinal值)|2个字节(ResourceId的长度)| ResourceId的值|4个字节(applicationData的长度)| applicationData的值|

事务类型: AT,TCC,XA,SAGA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章