RPC简述及简单实现

2016-04-26  本文已影响0人  8b9a60922af5

本文主要介绍RPC的原理并实现一个简单的例子。

RPC主要原理

RPC简单实现

本节将给出一个简单的RPC实现,网络传输是通过裸的socket编程,序列化是用的原始的序列化方式完成。高性能的网络传输方式和序列化方式在其他文章中整理。

处理代码如下:

public class ServerTask implements Runnable{
    private final Socket socket;
    private final Object service;

    public ServerTask(Socket socket, Object service){
        this.socket = socket;
        this.service = service;
    }
    @Override
    public void run(){
        try {
            //处理连接,等待IO有数据
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            //当有数据时,获取服务方法名称、方法参数、方法参数类型
            try {
                String methodName = objectInputStream.readUTF();
                Class<?>[] parameterTypes = (Class<?>[])(objectInputStream.readObject());
                Object[] arguments = (Object[])objectInputStream.readObject();

                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                try {
                    Method method = service.getClass().getMethod(methodName, parameterTypes);
                    Object result = method.invoke(service,arguments);
                    objectOutputStream.writeObject(result);
                }catch (Throwable e){
                    e.printStackTrace();
                }finally {
                    objectOutputStream.close();
                }
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }finally {
                objectInputStream.close();
            }
        }catch (IOException e){

        }finally {
            try {
                socket.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
 public static <T> T refer(Class<T> interfaceClass, final String host, final int port ){
        if (interfaceClass == null){
            throw new IllegalArgumentException("interfaceClass is null");
        }
        if (StringUtils.isEmpty(host)){
            throw new IllegalArgumentException("host is null");
        }
        if (port < 0 || port > 65535){
            throw new IllegalArgumentException("port is invalid, port is:" + port);
        }

        return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[]{interfaceClass}, new RpcInvoker(host, port));
    }
public class RpcInvoker implements InvocationHandler{

    private String host;

    private int port;

    public RpcInvoker(String host, int port){
        this.host = host;
        this.port = port;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable{
        Socket socket = new Socket(host, port);
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeUTF(method.getName());
            objectOutputStream.writeObject(method.getParameterTypes());
            objectOutputStream.writeObject(args);
            //等待服务端返回数据
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            try {
                Object result = objectInputStream.readObject();
                if (result instanceof Throwable){
                    throw (Throwable) result;
                }
                return result;
            }catch (Throwable e){
                e.printStackTrace();
            }finally {
                objectInputStream.close();
            }
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            socket.close();
        }
        return null;
    }
}
上一篇下一篇

猜你喜欢

热点阅读