简易RPC

Posted adeveloper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易RPC相关的知识,希望对你有一定的参考价值。

暴露服务:

技术分享图片
package com.saiarea;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

public class RpcService {
    public static void export(int port) throws Exception {
        if (port <= 0 || port > 65535)
            throw new IllegalArgumentException("Invalid port " + port);
        System.out.println("Export service on port " + port);
        ServerSocket server = new ServerSocket(port);
        for(;;) {
            try {
                final Socket socket = server.accept();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            try {
                                ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
                                try {
                                    String className = input.readUTF();
                                    String methodName = input.readUTF();
                                    Class<?>[] parameterTypes = (Class<?>[])input.readObject();
                                    Object[] arguments = (Object[])input.readObject();
                                    ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
                                    try {
                                        Object service = Class.forName(className+"Impl").newInstance();
                                        Method method = service.getClass().getMethod(methodName, parameterTypes);
                                        Object result = method.invoke(service, arguments);
                                        output.writeObject(result);
                                    } catch (Throwable t) {
                                        output.writeObject(t);
                                    } finally {
                                        output.close();
                                    }
                                } finally {
                                    input.close();
                                }
                            } finally {
                                socket.close();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception{
        Test test = new TestImpl();
        RpcService.export(8081);
    }
}
View Code

 

 

引用服务

技术分享图片
 1 package com.saiarea;
 2 
 3 import java.io.ObjectInputStream;
 4 import java.io.ObjectOutputStream;
 5 import java.lang.reflect.InvocationHandler;
 6 import java.lang.reflect.Method;
 7 import java.lang.reflect.Proxy;
 8 import java.net.Socket;
 9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.List;
12 
13 public class RpcRequestService {
14     static String host = "";
15     static int port = 0;
16 
17     public static <T> T proxyFactory(final Class<T> myClass) throws Exception {
18         List<Class> interfacesList = new ArrayList(myClass.getInterfaces().length + 1);
19         interfacesList.addAll(Arrays.asList(myClass.getInterfaces()));
20         interfacesList.add(myClass);
21         Class[] classes = new Class[myClass.getInterfaces().length + 1];
22         interfacesList.toArray(classes);
23         Object proxy = Proxy.newProxyInstance(RpcRequestService.class.getClassLoader(), classes, new InvocationHandler() {
24             public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
25                 Socket socket = new Socket(host, port);
26                 try {
27                     ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
28                     try {
29                         output.writeUTF(myClass.getName());
30                         output.writeUTF(method.getName());
31                         output.writeObject(method.getParameterTypes());
32                         output.writeObject(arguments);
33                         ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
34                         try {
35                             Object result = input.readObject();
36                             if (result instanceof Throwable) {
37                                 throw (Throwable) result;
38                             }
39                             return result;
40                         } finally {
41                             input.close();
42                         }
43                     } finally {
44                         output.close();
45                     }
46                 } finally {
47                     socket.close();
48                 }
49             }
50         });
51         System.out.println("test:" + proxy);
52         return (T) proxy;
53     }
54 
55     public static void main(String[] args) throws Exception {
56         host = "127.0.0.1";
57         port = 8081;
58         Test service = proxyFactory(Test.class);
59         Long hello = service.testRpc("测试动态加载代理");
60         System.out.println("我收到了," + hello);
61     }
62 }
View Code

 

以上是关于简易RPC的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo源码学习系列 手写简易RPC

简易RPC注解化

简易RPC

Netty RPC的简易DEMO

简易RPC框架-上下文

实现一个简易的RPC