抄底dubbo底层的数据包格式

 

先来看看一段dubbo调用代码:

  ProviderdemoService= (Provider) context.getBean("demoService1");           
  Test t=new  Test();
  t.s="hello world";
  t.n=1111;
  t.l=2222222222l;
  String message = demoService.build(t);

  dubbo框架底层编码是在DubboCodec
  @Override
   protected void encodeRequestData(Channel channel, ObjectOutput out,Object datathrows IOException {
        RpcInvocation inv = (RpcInvocation) data; 
        out.writeUTF(inv.getAttachment(Constants.DUBBO_VERSION_KEY,DUBBO_VERSION));
        out.writeUTF(inv.getAttachment(Constants.PATH_KEY));
        out.writeUTF(inv.getAttachment(Constants.VERSION_KEY)); 

        out.writeUTF(inv.getMethodName());
        out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes()));
        Object[] args = inv.getArguments();
        if (args != null)
            for (inti = 0; i < args.length; i++) {
                out.writeObject(encodeInvocationArgument(channel, inv, i));
            }
        out.writeObject(inv.getAttachments());
   }

Debug下inv的参数看看

RpcInvocation [methodName=build, parameterTypes=[classcom.test.impl.Test], arguments=[com.test.impl.Test@7b0fae4e],attachments={path=com.test.Provider, interface=com.test.Provider,version=0.0.0, timeout=5000}]

包数据=dubbo版本号+接口名称+接口版本号+方法名+参数类型+参数值+ attachments 

通过网络抓包,左边的是二进制数据,右边是抓包工具解析出来的数据,windows抓包工具使用wireshark,linux使用tcpdump命令


字符串 “com.test.Provider”用二进制是怎么表示的?dubbo支持多种编解码方式,默认是Hessian2编解码,那就要看Hessian2ObjectOutput.writeUTF的实现。

大概逻辑如下:

String类型的byte编码=length(int类型)+String转为byte

com.test.Provider的长度为17,转为十六进制是11,

‘c’转为byte为十六进制53,同样其他字符逐个转为byte排在后面 

11  63 6f  6d  2e  74  65  73  74  2e  50  72  6f  76  69  64  65  72

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值