查阅RPC与HTTP区别的时候, 无意间发现一篇博客,内容是一个简易的RPC服务框架, 仔细一看, 不得了,博主竟然就是阿里dubbo的作者.
原文链接在此:
http://javatar.iteye.com/blog/1123915?page=2#comments;
在这里冒昧粘下代码:
1 package com.alibaba.study.rpc.framework; 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.ServerSocket; 9 import java.net.Socket; 10 11 /** 12 * RpcFramework 13 * 14 * @author william.liangf 15 */ 16 public class RpcFramework { 17 18 /** 19 * 暴露服务 20 * 21 * @param service 服务实现 22 * @param port 服务端口 23 * @throws Exception 24 */ 25 public static void export(final Object service, int port) throws Exception { 26 if (service == null) 27 throw new IllegalArgumentException("service instance == null"); 28 if (port <= 0 || port > 65535) 29 throw new IllegalArgumentException("Invalid port " + port); 30 System.out.println("Export service " + service.getClass().getName() + " on port " + port); 31 ServerSocket server = new ServerSocket(port); 32 for(;;) { 33 try { 34 final Socket socket = server.accept(); 35 new Thread(new Runnable() { 36 @Override 37 public void run() { 38 try { 39 try { 40 ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); 41 try { 42 String methodName = input.readUTF(); 43 Class<?>[] parameterTypes = (Class<?>[])input.readObject(); 44 Object[] arguments = (Object[])input.readObject(); 45 ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream()); 46 try { 47 Method method = service.getClass().getMethod(methodName, parameterTypes); 48 Object result = method.invoke(service, arguments); 49 output.writeObject(result); 50 } catch (Throwable t) { 51 output.writeObject(t); 52 } finally { 53 output.close(); 54 } 55 } finally { 56 input.close(); 57 } 58 } finally { 59 socket.close(); 60 } 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 }).start(); 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 /** 73 * 引用服务 74 * 75 * @param <T> 接口泛型 76 * @param interfaceClass 接口类型 77 * @param host 服务器主机名 78 * @param port 服务器端口 79 * @return 远程服务 80 * @throws Exception 81 */ 82 @SuppressWarnings("unchecked") 83 public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception { 84 if (interfaceClass == null) 85 throw new IllegalArgumentException("Interface class == null"); 86 if (! interfaceClass.isInterface()) 87 throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!"); 88 if (host == null || host.length() == 0) 89 throw new IllegalArgumentException("Host == null!"); 90 if (port <= 0 || port > 65535) 91 throw new IllegalArgumentException("Invalid port " + port); 92 System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port); 93 return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new InvocationHandler() { 94 public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { 95 Socket socket = new Socket(host, port); 96 try { 97 ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream()); 98 try { 99 output.writeUTF(method.getName()); 100 output.writeObject(method.getParameterTypes()); 101 output.writeObject(arguments); 102 ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); 103 try { 104 Object result = input.readObject(); 105 if (result instanceof Throwable) { 106 throw (Throwable) result; 107 } 108 return result; 109 } finally { 110 input.close(); 111 } 112 } finally { 113 output.close(); 114 } 115 } finally { 116 socket.close(); 117 } 118 } 119 }); 120 } 121 122 }