`
无量
  • 浏览: 1142120 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Rpc转

 
阅读更多
转于自己在公司的Blog: 
http://pt.alibaba-inc.com/wp/experience_1330/simple-rpc-framework.html 

因为要给百技上实训课,让新同学们自行实现一个简易RPC框架,在准备PPT时,就想写个示例,发现原来一个RPC框架只要一个类,10来分钟就可以写完了,虽然简陋,也晒晒: 

Java代码  收藏代码
  1. /* 
  2.  * Copyright 2011 Alibaba.com All right reserved. This software is the 
  3.  * confidential and proprietary information of Alibaba.com ("Confidential 
  4.  * Information"). You shall not disclose such Confidential Information and shall 
  5.  * use it only in accordance with the terms of the license agreement you entered 
  6.  * into with Alibaba.com. 
  7.  */  
  8. package com.alibaba.study.rpc.framework;  
  9.   
  10. import java.io.ObjectInputStream;  
  11. import java.io.ObjectOutputStream;  
  12. import java.lang.reflect.InvocationHandler;  
  13. import java.lang.reflect.Method;  
  14. import java.lang.reflect.Proxy;  
  15. import java.net.ServerSocket;  
  16. import java.net.Socket;  
  17.   
  18. /** 
  19.  * RpcFramework 
  20.  *  
  21.  * @author william.liangf 
  22.  */  
  23. public class RpcFramework {  
  24.   
  25.     /** 
  26.      * 暴露服务 
  27.      *  
  28.      * @param service 服务实现 
  29.      * @param port 服务端口 
  30.      * @throws Exception 
  31.      */  
  32.     public static void export(final Object service, int port) throws Exception {  
  33.         if (service == null)  
  34.             throw new IllegalArgumentException("service instance == null");  
  35.         if (port <= 0 || port > 65535)  
  36.             throw new IllegalArgumentException("Invalid port " + port);  
  37.         System.out.println("Export service " + service.getClass().getName() + " on port " + port);  
  38.         ServerSocket server = new ServerSocket(port);  
  39.         for(;;) {  
  40.             try {  
  41.                 final Socket socket = server.accept();  
  42.                 new Thread(new Runnable() {  
  43.                     @Override  
  44.                     public void run() {  
  45.                         try {  
  46.                             try {  
  47.                                 ObjectInputStream input = new ObjectInputStream(socket.getInputStream());  
  48.                                 try {  
  49.                                     String methodName = input.readUTF();  
  50.                                     Class<?>[] parameterTypes = (Class<?>[])input.readObject();  
  51.                                     Object[] arguments = (Object[])input.readObject();  
  52.                                     ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());  
  53.                                     try {  
  54.                                         Method method = service.getClass().getMethod(methodName, parameterTypes);  
  55.                                         Object result = method.invoke(service, arguments);  
  56.                                         output.writeObject(result);  
  57.                                     } catch (Throwable t) {  
  58.                                         output.writeObject(t);  
  59.                                     } finally {  
  60.                                         output.close();  
  61.                                     }  
  62.                                 } finally {  
  63.                                     input.close();  
  64.                                 }  
  65.                             } finally {  
  66.                                 socket.close();  
  67.                             }  
  68.                         } catch (Exception e) {  
  69.                             e.printStackTrace();  
  70.                         }  
  71.                     }  
  72.                 }).start();  
  73.             } catch (Exception e) {  
  74.                 e.printStackTrace();  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     /** 
  80.      * 引用服务 
  81.      *  
  82.      * @param <T> 接口泛型 
  83.      * @param interfaceClass 接口类型 
  84.      * @param host 服务器主机名 
  85.      * @param port 服务器端口 
  86.      * @return 远程服务 
  87.      * @throws Exception 
  88.      */  
  89.     @SuppressWarnings("unchecked")  
  90.     public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {  
  91.         if (interfaceClass == null)  
  92.             throw new IllegalArgumentException("Interface class == null");  
  93.         if (! interfaceClass.isInterface())  
  94.             throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");  
  95.         if (host == null || host.length() == 0)  
  96.             throw new IllegalArgumentException("Host == null!");  
  97.         if (port <= 0 || port > 65535)  
  98.             throw new IllegalArgumentException("Invalid port " + port);  
  99.         System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);  
  100.         return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new InvocationHandler() {  
  101.             public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {  
  102.                 Socket socket = new Socket(host, port);  
  103.                 try {  
  104.                     ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());  
  105.                     try {  
  106.                         output.writeUTF(method.getName());  
  107.                         output.writeObject(method.getParameterTypes());  
  108.                         output.writeObject(arguments);  
  109.                         ObjectInputStream input = new ObjectInputStream(socket.getInputStream());  
  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.                     } finally {  
  120.                         output.close();  
  121.                     }  
  122.                 } finally {  
  123.                     socket.close();  
  124.                 }  
  125.             }  
  126.         });  
  127.     }  
  128.   
  129. }  


用起来也像模像样: 

(1) 定义服务接口 
Java代码  收藏代码
  1. /* 
  2.  * Copyright 2011 Alibaba.com All right reserved. This software is the 
  3.  * confidential and proprietary information of Alibaba.com ("Confidential 
  4.  * Information"). You shall not disclose such Confidential Information and shall 
  5.  * use it only in accordance with the terms of the license agreement you entered 
  6.  * into with Alibaba.com. 
  7.  */  
  8. package com.alibaba.study.rpc.test;  
  9.   
  10. /** 
  11.  * HelloService 
  12.  *  
  13.  * @author william.liangf 
  14.  */  
  15. public interface HelloService {  
  16.   
  17.     String hello(String name);  
  18.   
  19. }  


(2) 实现服务 
Java代码  收藏代码
  1. /* 
  2.  * Copyright 2011 Alibaba.com All right reserved. This software is the 
  3.  * confidential and proprietary information of Alibaba.com ("Confidential 
  4.  * Information"). You shall not disclose such Confidential Information and shall 
  5.  * use it only in accordance with the terms of the license agreement you entered 
  6.  * into with Alibaba.com. 
  7.  */  
  8. package com.alibaba.study.rpc.test;  
  9.   
  10. /** 
  11.  * HelloServiceImpl 
  12.  *  
  13.  * @author william.liangf 
  14.  */  
  15. public class HelloServiceImpl implements HelloService {  
  16.   
  17.     public String hello(String name) {  
  18.         return "Hello " + name;  
  19.     }  
  20.   
  21. }  


(3) 暴露服务 
Java代码  收藏代码
  1. /* 
  2.  * Copyright 2011 Alibaba.com All right reserved. This software is the 
  3.  * confidential and proprietary information of Alibaba.com ("Confidential 
  4.  * Information"). You shall not disclose such Confidential Information and shall 
  5.  * use it only in accordance with the terms of the license agreement you entered 
  6.  * into with Alibaba.com. 
  7.  */  
  8. package com.alibaba.study.rpc.test;  
  9.   
  10. import com.alibaba.study.rpc.framework.RpcFramework;  
  11.   
  12. /** 
  13.  * RpcProvider 
  14.  *  
  15.  * @author william.liangf 
  16.  */  
  17. public class RpcProvider {  
  18.   
  19.     public static void main(String[] args) throws Exception {  
  20.         HelloService service = new HelloServiceImpl();  
  21.         RpcFramework.export(service, 1234);  
  22.     }  
  23.   
  24. }  


(4) 引用服务 
Java代码  收藏代码
  1. /* 
  2.  * Copyright 2011 Alibaba.com All right reserved. This software is the 
  3.  * confidential and proprietary information of Alibaba.com ("Confidential 
  4.  * Information"). You shall not disclose such Confidential Information and shall 
  5.  * use it only in accordance with the terms of the license agreement you entered 
  6.  * into with Alibaba.com. 
  7.  */  
  8. package com.alibaba.study.rpc.test;  
  9.   
  10. import com.alibaba.study.rpc.framework.RpcFramework;  
  11.   
  12. /** 
  13.  * RpcConsumer 
  14.  *  
  15.  * @author william.liangf 
  16.  */  
  17. public class RpcConsumer {  
  18.       
  19.     public static void main(String[] args) throws Exception {  
  20.         HelloService service = RpcFramework.refer(HelloService.class"127.0.0.1"1234);  
  21.         for (int i = 0; i < Integer.MAX_VALUE; i ++) {  
  22.             String hello = service.hello("World" + i);  
  23.             System.out.println(hello);  
  24.             Thread.sleep(1000);  
  25.         }  
  26.     }  
  27.       
  28. }  
分享到:
评论

相关推荐

    ONCRPC.rar_ONCRPC_code rpc_onc_onc rpc

    这个"ONCRPC.rar_ONCRPC_code rpc_onc_onc rpc"文件包含的是关于ONC RPC协议的实现代码,主要针对的是JAVA平台,旨在实现不同编程语言之间的RPC调用。 在RPC(Remote Procedure Call)机制中,客户端可以透明地调用...

    手写rpc rpc简单源码 rpc源码学习 rpc过程了解 rpc通信原理

    RPC(Remote Procedure Call)远程过程调用是一种计算机通信协议,允许程序在一台计算机上执行另一台计算机上的程序,而无需了解底层网络协议的细节。它为开发者提供了一种透明调用远程服务的方式,使得分布式系统...

    用RPC机制把本地调用转换成远程调用

    ### 使用RPC机制将本地调用转换为远程调用 #### 概述 远程过程调用(RPC, Remote Procedure Call)是一种通信协议,它允许在一台计算机上的程序调用另一台计算机上的子程序或函数,而无需程序员明确了解底层通信...

    rpc远程调用库C语言实现

    RPC(Remote Procedure Call)是一种进程间通信技术,允许在一台计算机上的程序调用另一台计算机上的程序,使得分布式系统能够像调用本地函数一样调用远程服务。在本主题中,我们将深入探讨如何使用C语言实现RPC,并...

    Java RPC调用示例

    Java RPC(Remote Procedure Call)调用是分布式系统中常见的通信方式,它允许一个程序在不关心远程系统具体实现的情况下调用另一个网络上的程序。在这个Java RPC调用示例中,我们将探讨RPC的基本概念、实现机制以及...

    高性能RPC框架 nfs-rpc

    RPC(Remote Procedure Call)是一种进程间通信机制,它允许一个程序调用另一个位于不同系统上的程序,就像调用本地函数一样。NFS-RPC(Network File System - Remote Procedure Call)是基于RPC的一种特定应用,...

    RPC.rar_C++ 远程调用_VC6.0 开发RPC_windows RPC_远程过程调用

    RPC,即Remote Procedure Call(远程过程调用),是计算机网络编程中的一个重要概念,它允许一个程序在不理解底层网络协议的情况下,调用另一个网络上不同机器上的程序。在这个"RPC.rar"压缩包中,主要围绕C++语言在...

    影像RPC和GCP校正

    在IT行业中,"影像RPC和GCP校正"是一个重要的图像处理领域,主要涉及遥感图像的几何校正。遥感图像(Remote Sensing Image,简称RSI)由于拍摄角度、大气条件、传感器特性等因素,往往存在几何畸变,需要进行校正以...

    RPC的简单实现

    RPC(Remote Procedure Call)是一种计算机通信协议,允许一个程序在某个网络中的一个计算机上执行远程操作,就像它在本地执行一样。本篇文章将探讨RPC的基本原理、实现方式以及其在IT领域的应用。 **1. RPC的基本...

    实现一个简单的RPC框架

    RPC(Remote Procedure Call)是一种进程间通信的技术,它允许程序在不同的网络节点上进行通信,就像调用本地函数一样调用远程系统上的函数或方法。本篇将详细讲解如何使用socket、反射和序列化等技术来实现一个简单...

    基于严格成像模型的遥感影像RPC参数求解.pdf

    基于严格成像模型的遥感影像RPC参数求解 本文主要讨论基于严格成像模型的遥感影像RPC参数求解问题。RPC参数是遥感影像几何校正的关键参数,通过严格成像模型可以推导出RPC参数。文中首先介绍了基于严格成像模型的...

    RPC 实例,RPC实例

    RPC,即Remote Procedure Call(远程过程调用),是分布式系统中一种常见的通信机制,它允许一个程序在不关心网络细节的情况下调用另一个网络上程序的功能,就像调用本地函数一样简单。RPC使得开发者可以像处理本地...

    RPC(远程过程调用)

    RPC(Remote Procedure Call)是一种计算机通信协议,它允许程序在分布式环境中的一个系统上执行另一系统上的函数或方法,就像是本地调用一样。这个过程涉及到了客户端、服务器端和服务调用的封装,使得开发者无需...

    ENVI自定义RPC文件图像正射校正及数据

    2. **创建RPC模型**:在ENVI主界面中,选择“Tools” -&gt; “RPC Correction”,进入RPC校正对话框。点击“Load”按钮,导入RPC文件。ENVI会自动读取并建立RPC模型。 3. **设置输出参数**:在对话框中,指定输出文件...

    NettyRPC-master

    NettyRPC-master是一个基于Netty框架实现的远程过程调用(RPC)系统。Netty是一个高性能、异步事件驱动的网络应用框架,适用于开发可维护的高性能协议服务器和客户端。RPC(Remote Procedure Call)是一种允许程序在...

    WSDL样式详解,soap中Rpc和Document的区别

    SOAP绑定有两大数据样式:RPC(Remote Procedure Call)和Document。 1. RPC样式: - **RPC/Encoded**:此样式将方法名映射为SOAP消息中的根元素,同时在SOAP消息中编码参数类型信息。WSDL示例中展示了`...

    php rpc远程过程调用

    远程过程调用(RPC)是一种计算机通信协议,它允许一个程序调用另一个在不同地址空间(可能在同一台机器上,也可能在远程网络上的另一台机器上)运行的程序。PHP RPC是PHP实现的RPC框架,它简化了在分布式系统中进行...

    RPC入门学习笔记

    RPC(Remote Procedure Call)远程过程调用是一种网络通信协议,允许一台计算机上的程序调用另一台计算机上的程序,就像调用本地函数一样简单。在分布式系统中,RPC扮演着核心角色,使得各服务之间能够方便地进行...

    RPC8211FS RGMII/SGMII 1000M Ethernet PHY

    RPC8211FS是一款高性能的以太网PHY收发器,专为10Base-T、100Base-TX和1000Base-T的网络连接设计,完全符合IEEE 802.3标准。这款器件集成了所有必要的物理层功能,允许通过CAT.5非屏蔽双绞线(UTP)进行高速数据传输。...

    rpc调用的一个demo

    RPC(Remote Procedure Call)是一种进程间通信方式,允许一台计算机上的程序调用另一台计算机上的程序,就像调用本地函数一样。在这个“rpc调用的一个demo”中,我们将会探讨RPC的基本原理,以及如何实现一个简单的...

Global site tag (gtag.js) - Google Analytics