Docker(4)——RPC过程简单实现

来源:互联网 发布:琉璃神社新域名 2017 编辑:程序博客网 时间:2024/06/08 09:37

1、主要代码

[java] view plain copy
  1. package com.github.distribute.rpc;  
  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. public class RpcFramework {  
  12.   
  13.     public static void export(final Object service, final Class interfaceClazz, int port) throws Exception {  
  14.         if (service == null) {  
  15.             throw new IllegalAccessException("service instance == null");  
  16.         }  
  17.         if (port < 0 || port > 65535) {  
  18.             throw new IllegalAccessException("Invalid port " + port);  
  19.         }  
  20.         System.out.println("Export service " + service.getClass().getName() + " on port " + port);  
  21.   
  22.         ServerSocket server = new ServerSocket(port);  
  23.         //循环等待,每来一个请求开启一条线程进行处理  
  24.         for (;;) {  
  25.             final Socket socket = server.accept();//tcp阻塞等待  
  26.             try {  
  27.                 new Thread(new Runnable() {  
  28.   
  29.                     @Override  
  30.                     public void run() {  
  31.                         try {  
  32.                             try {  
  33.                                 ObjectInputStream input = new ObjectInputStream(socket.getInputStream());  
  34.   
  35.                                 try {  
  36.                                     String interfaceName = input.readUTF();  
  37.                                     String methodName = input.readUTF();  
  38.                                     Class<?>[] parameterTypes = (Class<?>[]) input.readObject();  
  39.                                     Object[] arguments = (Object[]) input.readObject();  
  40.                                     ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());  
  41.   
  42.                                     try {  
  43.                                         if (!interfaceName.equals(interfaceClazz.getName())) {  
  44.                                             throw new IllegalAccessException("Interface wrong, export:" + interfaceClazz  
  45.                                                     + " refer:" + interfaceName);  
  46.                                         }  
  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.                                 }  catch (Throwable t) {  
  56.                                     t.printStackTrace();  
  57.                                 } finally {  
  58.                                     input.close();  
  59.                                 }  
  60.                             }   
  61.                               
  62.                             finally {  
  63.                                 socket.close();  
  64.                             }  
  65.                         } catch (Exception e) {  
  66.                             e.printStackTrace();  
  67.                         }  
  68.                     }  
  69.                 }).start();  
  70.             } catch (Exception e) {  
  71.                 e.printStackTrace();  
  72.             }  
  73.   
  74.         }  
  75.     }  
  76.   
  77.     @SuppressWarnings("unchecked")  
  78.     public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {  
  79.         if (interfaceClass == null) {  
  80.             throw new IllegalAccessException("Interface class == null");  
  81.         }  
  82.         if (!interfaceClass.isInterface()) {  
  83.             throw new IllegalAccessException(interfaceClass.getName() + " must be interface");  
  84.         }  
  85.         if (host == null || host.length() == 0) {  
  86.             throw new IllegalAccessException("host == null");  
  87.         }  
  88.         if (port <= 0 || port > 65535) {  
  89.             throw new IllegalAccessException("Invalid port " + port);  
  90.         }  
  91.         System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);  
  92.         return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass },  
  93.                 new InvocationHandler() {  
  94.   
  95.                     @Override  
  96.                     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  97.                         // TODO Auto-generated method stub  
  98.                         Socket socket = new Socket(host, port);  
  99.                         try {  
  100.                             ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());  
  101.   
  102.                             try {  
  103.                                 output.writeUTF(interfaceClass.getName());  
  104.                                 output.writeUTF(method.getName());  
  105.                                 output.writeObject(method.getParameterTypes());  
  106.                                 output.writeObject(args);  
  107.   
  108.                                 ObjectInputStream input = new ObjectInputStream(socket.getInputStream());  
  109.   
  110.                                 try {  
  111.                                     Object result = input.readObject();  
  112.                                     if (result instanceof Throwable) {  
  113.                                         throw (Throwable) result;  
  114.                                     }  
  115.                                     return result;  
  116.                                 } finally {  
  117.                                     input.close();  
  118.                                 }  
  119.   
  120.                             } finally {  
  121.                                 output.close();  
  122.                             }  
  123.   
  124.                         } finally {  
  125.                             socket.close();  
  126.                         }  
  127.                     }  
  128.                 });  
  129.     }  
  130.   
  131. }  

2、模拟接口

接口类

[java] view plain copy
  1. package com.github.distribute.rpc;  
  2.   
  3. public interface  HelloService {  
  4.       
  5.     String hello();    
  6.         
  7.     String hello(String name);  
  8.   
  9. }  
实现类

[java] view plain copy
  1. package com.github.distribute.rpc;  
  2.   
  3.   
  4. public class HelloServiceImpl implements HelloService {    
  5.     
  6.     @Override    
  7.     public String hello() {    
  8.         return "Hello";    
  9.     }    
  10.     
  11.     @Override    
  12.     public String hello(String name) {    
  13.         return "Hello," + name;    
  14.     }    
  15.     
  16. }    

3、接口提供者

[java] view plain copy
  1. package com.github.distribute.rpc;  
  2.   
  3. public class RpcProvider {  
  4.       
  5.       public static void main(String[] args) throws Exception {    
  6.             HelloService service = new HelloServiceImpl();    
  7.             RpcFramework.export(service, HelloService.class9000);    
  8.         }  
  9.   
  10. }  
结果:


4、消费者

[java] view plain copy
  1. package com.github.distribute.rpc;  
  2.   
  3. public class RpcConsumer {  
  4.     public static void main(String[] args) throws Exception {  
  5.         HelloService service = RpcFramework.refer(HelloService.class"127.0.0.1"9000);  
  6.         String result = service.hello("rod");  
  7.         System.out.println(result);  
  8.     }  
  9.   
  10. }  

结果:


原创粉丝点击