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

猜你喜欢

转载自m635674608.iteye.com/blog/2306042