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

230108_50_RPC底层原理

时间:2023-01-08 21:22:07浏览次数:38  
标签:230108 bytes 50 RPC io new import 序列化 com

  • Stub还有很多需要优化的地方,目前只是实现了一个最基本的代理。网络传输都是通过序列化和反序列化进行的,目前java自带的Serializable接口效率比较低,因此可以对rpc的序列化方式进行优化,目前已经有许多成熟的RPC序列化框架:

  • RPC序列化框架

    java.io.Serializable
    Hessian
    google protobuf
    facebook Thrift
    kyro
    fst
    json序列化框架:1.Jackson;2.googel Gson;3.Ali FastJson
    xmlrpc(xstream)
  • Hessian序列化框架分析

package com.bill.rpc08;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/8 - 01 - 08 - 18:52
 * @Description: com.bill.rpc08
 * @version: 1.0
 */
public class HelloHessian {
    public static void main(String[] args) throws IOException {
        User u = new User(1,"bill");
        byte[] bytes = seriable(u);
        System.out.println(bytes.length);
        User u1 = (User) deseriable(bytes);
        System.out.println(u1);
    }
	// 序列化,创建一个输出字节流,将对象转换为二进制
    public static byte[] seriable(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;
    }
    // 反序列化,创建一个输入字节流,将二进制转换为对象
    public static Object deseriable(byte[] bytes) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Hessian2Input input = new Hessian2Input(bais);
        Object o = input.readObject();
        
        bais.close();
        input.close();
        return o;
    }
}
  • RPC通信协议
http:传文本
http2.0:可以传二进制
TCP:同步/异步;阻塞/非阻塞
WebService

标签:230108,bytes,50,RPC,io,new,import,序列化,com
From: https://www.cnblogs.com/wcwblog/p/17035384.html

相关文章