手写简单版本的RPC

一、实现思路
基于socket与serverSocket网络编程
二、实现方案
1、rpc-server:
(1)、rpc-server-api
a.创建 ISayHellow

public interface ISayHellow {
    void sayHellow();
}

b.创建 IStudyHard

public interface IStudyHard {
    void studyHard();
}

c.创建 RpcRequest

public class RpcRequest implements Serializable{
    private String className;

    private String methodName;

    private Object[] params;

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }
}

(2)、rpc-server-provider
a.创建SayHellowImpl

public class SayHellowImpl implements ISayHellow {
    @Override
    public void sayHellow() {
        System.out.println("[rpc调用]:gpp,hellow!");
    }
}

b.创建StudyHardImpl

public class StudyHardImpl implements IStudyHard {
    @Override
    public void studyHard() {
        System.out.println("[rpc调用]:study study up!");
    }
}

c.创建 RpcHandler

public class RpcHandler  {
    void doRpcRequest() throws IOException {
        System.out.println("rpc调用开始-------");
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(80);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true){
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            ObjectInputStream objectInputStream = null;
            ObjectOutputStream objectOutputStream = null;
            try {
                objectInputStream = new ObjectInputStream(socket.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            RpcRequest rpcRequest = null;
            try {
                rpcRequest = (RpcRequest) objectInputStream.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            String className = rpcRequest.getClassName();
            String methodName = rpcRequest.getMethodName();
            Object[] params = rpcRequest.getParams();

            Class <?> aClass = null;
            try {
                aClass = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Class[] types = new Class[params.length];
            for (int i = 0; i <params.length ; i++) {
                types[i] = params[i].getClass();
            }
            Method method = null;
            try {
                method = aClass.getMethod(methodName, types);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            try {
                Object result = null;
                try {
                    result = method.invoke(SayHellowImpl.class.newInstance(), params);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
                objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                objectOutputStream.writeObject(result);
                objectOutputStream.flush();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }finally {
                if(objectInputStream != null){
                    objectInputStream.close();
                }
                if(objectOutputStream!= null){
                    objectOutputStream.close();
                }
                System.out.println("rpc调用结束------");
            }


        }
        

    }
}

d.创建服务端启动类 ServerApp

public class ServerApp 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        RpcHandler rpcHandler = new RpcHandler();
        try {
            rpcHandler.doRpcRequest();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、rpc-client:

a. 创建 代理类 RpcProxy

public class RpcProxy implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParams(args);


        Socket socket = null;

        Object result=null;

        try {
            while(true){
                socket = new Socket("localhost",8089);
                if(null != socket) break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        ObjectOutputStream objectOutputStream = null;

        ObjectInputStream objectInputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            objectOutputStream.writeObject(rpcRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            objectOutputStream.writeObject(rpcRequest);
            objectOutputStream.flush();
             objectInputStream = new ObjectInputStream(socket.getInputStream());
            try {
                 result = objectInputStream.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != objectInputStream){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != objectOutputStream){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

b.创建 访问代理客户端 RpcProxyClient

public class RpcProxyClient {

    public <T> T clientProxy2(final Class<T> interfaceCls){
        return (T)Proxy.newProxyInstance(interfaceCls.getClassLoader(),new Class<?>[]{interfaceCls},new RpcProxy());
    }
}

c.创建客户端启动类 ClientApp

public class ClientApp
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        ISayHellow iSayHellow = new RpcProxyClient().clientProxy2(ISayHellow.class);
        iSayHellow.sayHellow();
    }
}

三.工程代码结构图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值