`
m635674608
  • 浏览: 5062492 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

avro入门之rpc

 
阅读更多

官方给出的rpc资料太少了,这个链接https://github.com/phunt/avro-rpc-quickstart上有相关的说明

现在对在java下使用总结如下:

 

 

 

我这里没有使用maven,直接在项目中加入使用到的jar包有:avro-1.7.7.jar、avro-tools-1.7.7.jar、 jackson-core-asl-1.8.8.jar、jackson-mapper-asl-1.8.8.jar
当然,如果你需要,你也可以在Avro源码中进行编译,获取avro-1.7.7.jar和avro-tools-1.7.7.jar

Avro协议是以JSON结构性描述文本。协议定义了基本的通信的数据类型,名称。并且还包含可调用的方法等。首先定义协议文件

 

[java] view plain copy在CODE上查看代码片派生到我的代码片
  1. {  
  2.     "namespace":"avro",  
  3.     "doc":"This is a message.",  
  4.     "protocol":"messageProtocol",  
  5.     "name":"HelloWorld",  
  6.     "types":[  
  7.         {  
  8.             "name":"nameMessage",  
  9.             "type":"record",  
  10.             "fields":[ {"name":"name""type":"string"} ]  
  11.         }  
  12.     ],  
  13.     "messages":{  
  14.         "sayHello":{  
  15.             "doc":"say Hello to manbers",  
  16.             "request":[ { "name":"name""type":"string" } ],  
  17.             "response":"nameMessage"  
  18.         }  
  19.     }  
  20. }  

保存在d盘 a.avro

然后编写服务端代码:

 

[java] view plain copy在CODE上查看代码片派生到我的代码片
  1. import java.io.File;  
  2.   
  3. import org.apache.avro.Protocol;  
  4. import org.apache.avro.Protocol.Message;  
  5. import org.apache.avro.generic.GenericData;  
  6. import org.apache.avro.generic.GenericRecord;  
  7. import org.apache.avro.ipc.HttpServer;  
  8. import org.apache.avro.ipc.Server;  
  9. import org.apache.avro.ipc.generic.GenericResponder;  
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12.   
  13. public class AvroHttpServer extends GenericResponder {  
  14.   
  15.     private static Log log = LogFactory.getLog(AvroHttpServer.class);  
  16.   
  17.     public AvroHttpServer(Protocol protocol) {  
  18.         super(protocol);  
  19.     }  
  20.   
  21.     public Object respond(Message message, Object request) throws Exception {  
  22.         GenericRecord req = (GenericRecord) request;  
  23.         GenericRecord reMessage = null;  
  24.         if (message.getName().equals("sayHello")) {  
  25.             Object name = req.get("name");  
  26.             //  do something...  
  27.             //取得返回值的类型  
  28.             reMessage = new GenericData.Record(super.getLocal().getType("nameMessage"));   
  29.             //直接构造回复  
  30.             reMessage.put("name""Hello, " + name.toString());  
  31.             log.info(reMessage);  
  32.         }  
  33.         return reMessage;  
  34.     }  
  35.   
  36.     public static void main(String[] args) throws Exception {  
  37.         int port = 8088;  
  38.         try {  
  39.             Server server = new HttpServer(  
  40.                     new AvroHttpServer(Protocol.parse(  
  41. //                            new File("helloword.json"))),  
  42.                             new File("d:/a.avro"))),  
  43.                     port);  
  44.             server.start();  
  45.             server.join();  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50. }  

接下来编写客户端代码:

 

 

[java] view plain copy在CODE上查看代码片派生到我的代码片
  1. import java.io.File;  
  2. import java.net.URL;  
  3.   
  4. import org.apache.avro.Protocol;  
  5. import org.apache.avro.generic.GenericData;  
  6. import org.apache.avro.generic.GenericRecord;  
  7. import org.apache.avro.ipc.HttpTransceiver;  
  8. import org.apache.avro.ipc.Transceiver;  
  9. import org.apache.avro.ipc.generic.GenericRequestor;  
  10. import org.junit.Before;  
  11. import org.junit.Test;  
  12.   
  13. public class b {  
  14.     private Protocol protocol;  
  15.   
  16.     private GenericRequestor requestor = null;  
  17.   
  18.     @Before  
  19.     public void setUp() throws Exception {  
  20.         protocol = Protocol.parse(new File("d:/a.avro"));  
  21.         Transceiver t = new HttpTransceiver(new URL("http://localhost:8088"));  //这里如果要在两台机器上运行记得把localhost改成服务端的ip  
  22.         requestor = new GenericRequestor(protocol, t);  
  23.     }  
  24.   
  25.     @Test  
  26.     public void testSendMessage() throws Exception {  
  27.         GenericRecord requestData = new GenericData.Record(protocol.getType("nameMessage"));  
  28.         // initiate the request data  
  29.         requestData.put("name""zhenqin");  
  30.   
  31.         System.out.println(requestData);  
  32.         Object result = requestor.request("sayHello", requestData);  
  33.         if (result instanceof GenericData.Record) {  
  34.             GenericData.Record record = (GenericData.Record) result;  
  35.             System.out.println(record.get("name"));  
  36.         }  
  37.         System.out.println(result);  
  38.     }  
  39.   
  40. }  

上面先运行服务端在运行客户端,可以看到客户端收到消息。

[java] view plain copy在CODE上查看代码片派生到我的代码片
  1. {"name""zhenqin"}  
  2. Hello, zhenqin  
  3. {"name""Hello, zhenqin"}  

以上参考http://my.oschina.net/zhzhenqin/blog/151040

至于用Python编写跨语言的客户端,具体内容还有待研究>>>>>>>>>重新弄了下python客户端的测试:

 

服务端继续使用java,客户端运行环境如下:python2.7,avro的python安装包在这里。下载解压,然后cd到该目录下执行

 

python setup.py install安装完毕

 

接下来只需要执行客户端代码如下:

 

 

 

[java] view plain copy在CODE上查看代码片派生到我的代码片
  1. #!encoding:utf-8  
  2.   
  3. import json  
  4. import avro.protocol as proto  
  5. import avro.ipc as ipc  
  6. import avro.io as avroio  
  7. import avro.schema as schema  
  8.   
  9. __author__ = 'zhenqin'  
  10.   
  11. PROTOCOL = proto.parse(open(r'd:/a.avro','r').read())  
  12.   
  13. def testPro():  
  14.     client = ipc.HTTPTransceiver("192.168.1.98"8088)  
  15.     requestor = ipc.Requestor(PROTOCOL, client)  
  16.   
  17.     message = dict()  
  18.     message["name"] = "ZhenQin"  
  19.   
  20.     v = requestor.request('sayHello', message)  
  21.     print("Result: " + str(v))  
  22.   
  23.     # cleanup  
  24.     client.close()  
  25.   
  26. if __name__ == '__main__':  
  27.     testPro()  


就可以看到客户端的运行结果,以上主要参考了http://my.oschina.net/zhzhenqin/blog/151040。另外http://www.iteblog.com/archives/1008http://www.bianceng.cn/Servers/web/201411/46469.htm也对于入门有帮助

 

http://blog.csdn.net/shb19891/article/details/41778631?utm_source=tuicool&utm_medium=referral

分享到:
评论

相关推荐

    avro-rpc程序示例

    **Avro RPC快速入门** 在"avro-rpc-quickstart-master"这个压缩包中,包含了Avro RPC的快速启动示例,包括客户端和服务器端的源代码。以下是快速启动的步骤: 1. **构建环境**: 确保已经安装了Java开发工具(JDK)...

    avro-rpc-quickstart:Apache Avro RPC快速入门

    作者: (在上关注我)概括,如果您愿意的话...介绍该项目中包含的示例应用程序模拟了一个远程服务Mail,其中Avro RPC用于使用该服务发送消息。 本文档详细介绍了如何使用Maven构建和运行示例。 Avro jar文件(以及它们

    avro-rpc-demo:avro rpc的演示代码

    “ avro-rpc-demo”是Java实现的avro rpc的演示代码。 作者:zhexin Pan日期:20151103 简介此项目包括Java中的三个实现演示。 第一个是官方网站的快速入门演示,它是数据序列化和反序列化的两种实现,分别称为...

    基于netty的spring入门程序

    该入门示例程序是异步、基于事件驱动的网络通讯框架,对于java开发...②经典的 Hadoop 的高性能通信和序列化组件 Avro 的 RPC 框架,默认采用 Netty 进行跨界点通信。它的 NettyService 基于 Netty 框架二次封装实现

    Hadoop-0.20.0-HDFS+MapReduce+Hive+HBase十分钟快速入门

    2. **Avro**:由Doug Cutting领导的RPC项目,类似于Google的Protocol Buffers和Facebook的Thrift,用于优化Hadoop内部的RPC通信,提高数据交换的速度和效率。 3. **Chukwa**:这是一个由Yahoo贡献的基于Hadoop的大...

    Cloudera基础培训材料.pdf

    接下来是Apache Avro部分,它是一种数据序列化系统,提供了丰富的数据结构类型、快速的编码和解码性能以及远程过程调用(RPC)功能。培训材料将介绍Avro的基本概念和使用场景。 基本模式示例部分,将通过具体的代码...

    Flume1.5.0入门:安装、部署、及flume的案例

    Flume 的实际应用案例中,例如 Avro 案例,Avro Source 可以通过 AVRO RPC 机制接收并发送文件数据。另一个案例是 Spool Source,它可以监控指定目录下的新文件,将文件内容读取并传输。在配置好相应的 Agent 配置...

Global site tag (gtag.js) - Google Analytics