Java实现简单的RPC
2019-05-30
字数统计:
1.2k字
|
阅读时长:
5分
RPC解释
RPC:Remote Procedure Call(远程过程调用)
客户端获取服务端的服务(不同的计算机)
RPC所需要的技术
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class Client { @SuppressWarnings ("unchecked" ) public static <T> T getRemoteProxyObj (Class serviceInterface, InetSocketAddress addr) { return (T)Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[] {serviceInterface}, new InvocationHandler() { @Override public Object invoke (Object proxy, Method method, Object[] args) throws Throwable { Object result = null ; ObjectInputStream input = null ; ObjectOutputStream output = null ; try { Socket socket = new Socket(); socket.connect(addr); output = new ObjectOutputStream( socket.getOutputStream()); output.writeUTF(serviceInterface.getName()); output.writeUTF(method.getName()); output.writeObject(method.getParameterTypes()); output.writeObject(args); input = new ObjectInputStream(socket.getInputStream()); result = input.readObject(); }catch (Exception e) { e.printStackTrace(); }finally { try { if (null != output) { output.close(); } if (null != input) { input.close(); } }catch (Exception e) { e.printStackTrace(); } } return result; } }) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 public class ServerCenter implements Server { private static HashMap<String, Class> serviceRegiser = new HashMap<>(); private static int port; public ServerCenter () { } public ServerCenter (int port) { this .port = port; } @Override public void start () { ServerSocket server = null ; Socket socket = null ; ObjectInputStream input = null ; ObjectOutputStream output = null ; try { server = new ServerSocket(); server.bind(new InetSocketAddress(port)); socket = server.accept(); input = new ObjectInputStream(socket.getInputStream()); String serviceName = input.readUTF(); String methodName = input.readUTF(); Class[] parameterTypes = (Class[])input.readObject(); Object[] arguments = (Object[])input.readObject(); Class serviceClass = serviceRegiser.get(serviceName); Method method = serviceClass.getMethod(methodName, parameterTypes); Object result = method.invoke(serviceClass.newInstance(), arguments); output = new ObjectOutputStream(socket.getOutputStream()); output.writeObject(result); }catch (Exception e) { e.printStackTrace(); }finally { try { if (null != output) { output.close(); } if (null != input) { input.close(); } }catch (Exception e) { e.printStackTrace(); } } } @Override public void stop () { } @Override public void register (Class service, Class serviceImpl) { serviceRegiser.put(service.getName(), serviceImpl); } }
优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 public class ServerCenter implements Server { private static HashMap<String, Class> serviceRegiser = new HashMap<>(); private static int port; private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private static boolean isRunning = false ; public ServerCenter () { } public ServerCenter (int port) { this .port = port; } @Override public void start () { ServerSocket server = null ; try { server = new ServerSocket(); server.bind(new InetSocketAddress(port)); } catch (IOException e1) { e1.printStackTrace(); } Socket socket = null ; System.out.println("启动服务..." ); isRunning = true ; while (true ) { try { socket = server.accept(); } catch (IOException e) { e.printStackTrace(); } executor.execute(new ServiceTask(socket)); } } @Override public void stop () { System.out.println("关闭服务..." ); isRunning = false ; executor.shutdown(); } @Override public void register (Class service, Class serviceImpl) { serviceRegiser.put(service.getName(), serviceImpl); } private static class ServiceTask implements Runnable { Socket socket = new Socket(); public ServiceTask () {} public ServiceTask (Socket socket) { this .socket = socket; } @Override public void run () { ObjectInputStream input = null ; ObjectOutputStream output = null ; try { input = new ObjectInputStream(socket.getInputStream()); String serviceName = input.readUTF(); String methodName = input.readUTF(); Class[] parameterTypes = (Class[])input.readObject(); Object[] arguments = (Object[])input.readObject(); Class serviceClass = serviceRegiser.get(serviceName); Method method = serviceClass.getMethod(methodName, parameterTypes); Object result = method.invoke(serviceClass.newInstance(), arguments); output = new ObjectOutputStream(socket.getOutputStream()); output.writeObject(result); }catch (Exception e) { e.printStackTrace(); }finally { try { if (null != output) { output.close(); } if (null != input) { input.close(); } }catch (Exception e) { e.printStackTrace(); } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class RPCServerTest { public static void main (String[] args) { new Thread(new Runnable() { @Override public void run () { Server server = new ServerCenter(9999 ); server.register(HelloService.class, HelloServiceImpl.class); server.start(); } }).start();; } }
1 2 3 4 5 6 7 8 9 10 11 public class RPCClientTest { public static void main (String[] args) throws ClassNotFoundException { HelloService service = Client.getRemoteProxyObj(Class.forName("com.kexing.rpc.service.HelloService" ), new InetSocketAddress("127.0.0.1" , 9999 )); System.out.println(service.sayHi("zs" )); } }
最后更新时间:2019-05-30 16:50:04
谢谢阅读本文,如果对文章或者网站有什么建议,请发邮件到:1799623289@qq.com