首页 > 其他分享 >230109_50_RPC底层原理

230109_50_RPC底层原理

时间:2023-01-09 23:45:15浏览次数:45  
标签:230109 bytes 50 baos RPC bais close new 序列化

  • Hessian与jdk序列化方法对比,hessian的序列化长度更短
package com.bill.rpc09;

import com.bill.rpc.common.User;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

import java.io.*;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/9 - 01 - 09 - 23:11
 * @Description: com.bill.rpc09
 * @version: 1.0
 */
public class HessianVSJDK {
    public static void main(String[] args) throws IOException {
        User u = new User(1,"zhangsan");
        System.out.println("hessian:"+hessianSeriable(u).length);
        System.out.println("jdk:"+jdkSeriable(u).length);
    }
	
    // hessian序列化
    public static byte[] hessianSeriable(Object o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(baos);
        output.writeObject(o);
        output.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        output.close();
        return bytes;
    }
    
    // hessian反序列化
    public static Object hessianDeseriable(byte[] bytes) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Hessian2Input input = new Hessian2Input(bais);
        Object o = input.readObject();
        bais.close();
        input.close();
        return o;
    }

    // jdk seriable序列化
    public static byte[] jdkSeriable(Object o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        oos.close();
        return bytes;
    }

    // jdk seriable反序列化
    public static Object jdkDeseriable(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object o = ois.readObject();
        bais.close();
        ois.close();
        return o;
    }
}
  • RPC通信协议

| http:传文本 |
| http2.0:可以传二进制 |
| TCP:同步/异步;阻塞/非阻塞 |
| WebService |

标签:230109,bytes,50,baos,RPC,bais,close,new,序列化
From: https://www.cnblogs.com/wcwblog/p/17038896.html

相关文章