官方给出的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结构性描述文本。协议定义了基本的通信的数据类型,名称。并且还包含可调用的方法等。首先定义协议文件
- {
- "namespace":"avro",
- "doc":"This is a message.",
- "protocol":"messageProtocol",
- "name":"HelloWorld",
- "types":[
- {
- "name":"nameMessage",
- "type":"record",
- "fields":[ {"name":"name", "type":"string"} ]
- }
- ],
- "messages":{
- "sayHello":{
- "doc":"say Hello to manbers",
- "request":[ { "name":"name", "type":"string" } ],
- "response":"nameMessage"
- }
- }
- }
保存在d盘 a.avro
然后编写服务端代码:
- import java.io.File;
- import org.apache.avro.Protocol;
- import org.apache.avro.Protocol.Message;
- import org.apache.avro.generic.GenericData;
- import org.apache.avro.generic.GenericRecord;
- import org.apache.avro.ipc.HttpServer;
- import org.apache.avro.ipc.Server;
- import org.apache.avro.ipc.generic.GenericResponder;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class AvroHttpServer extends GenericResponder {
- private static Log log = LogFactory.getLog(AvroHttpServer.class);
- public AvroHttpServer(Protocol protocol) {
- super(protocol);
- }
- public Object respond(Message message, Object request) throws Exception {
- GenericRecord req = (GenericRecord) request;
- GenericRecord reMessage = null;
- if (message.getName().equals("sayHello")) {
- Object name = req.get("name");
- // do something...
- //取得返回值的类型
- reMessage = new GenericData.Record(super.getLocal().getType("nameMessage"));
- //直接构造回复
- reMessage.put("name", "Hello, " + name.toString());
- log.info(reMessage);
- }
- return reMessage;
- }
- public static void main(String[] args) throws Exception {
- int port = 8088;
- try {
- Server server = new HttpServer(
- new AvroHttpServer(Protocol.parse(
- // new File("helloword.json"))),
- new File("d:/a.avro"))),
- port);
- server.start();
- server.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
接下来编写客户端代码:
- import java.io.File;
- import java.net.URL;
- import org.apache.avro.Protocol;
- import org.apache.avro.generic.GenericData;
- import org.apache.avro.generic.GenericRecord;
- import org.apache.avro.ipc.HttpTransceiver;
- import org.apache.avro.ipc.Transceiver;
- import org.apache.avro.ipc.generic.GenericRequestor;
- import org.junit.Before;
- import org.junit.Test;
- public class b {
- private Protocol protocol;
- private GenericRequestor requestor = null;
- @Before
- public void setUp() throws Exception {
- protocol = Protocol.parse(new File("d:/a.avro"));
- Transceiver t = new HttpTransceiver(new URL("http://localhost:8088")); //这里如果要在两台机器上运行记得把localhost改成服务端的ip
- requestor = new GenericRequestor(protocol, t);
- }
- @Test
- public void testSendMessage() throws Exception {
- GenericRecord requestData = new GenericData.Record(protocol.getType("nameMessage"));
- // initiate the request data
- requestData.put("name", "zhenqin");
- System.out.println(requestData);
- Object result = requestor.request("sayHello", requestData);
- if (result instanceof GenericData.Record) {
- GenericData.Record record = (GenericData.Record) result;
- System.out.println(record.get("name"));
- }
- System.out.println(result);
- }
- }
上面先运行服务端在运行客户端,可以看到客户端收到消息。
- {"name": "zhenqin"}
- Hello, zhenqin
- {"name": "Hello, zhenqin"}
以上参考http://my.oschina.net/zhzhenqin/blog/151040
至于用Python编写跨语言的客户端,具体内容还有待研究>>>>>>>>>重新弄了下python客户端的测试:
服务端继续使用java,客户端运行环境如下:python2.7,avro的python安装包在这里。下载解压,然后cd到该目录下执行
python setup.py install安装完毕
接下来只需要执行客户端代码如下:
- #!encoding:utf-8
- import json
- import avro.protocol as proto
- import avro.ipc as ipc
- import avro.io as avroio
- import avro.schema as schema
- __author__ = 'zhenqin'
- PROTOCOL = proto.parse(open(r'd:/a.avro','r').read())
- def testPro():
- client = ipc.HTTPTransceiver("192.168.1.98", 8088)
- requestor = ipc.Requestor(PROTOCOL, client)
- message = dict()
- message["name"] = "ZhenQin"
- v = requestor.request('sayHello', message)
- print("Result: " + str(v))
- # cleanup
- client.close()
- if __name__ == '__main__':
- testPro()
就可以看到客户端的运行结果,以上主要参考了http://my.oschina.net/zhzhenqin/blog/151040。另外http://www.iteblog.com/archives/1008和http://www.bianceng.cn/Servers/web/201411/46469.htm也对于入门有帮助
http://blog.csdn.net/shb19891/article/details/41778631?utm_source=tuicool&utm_medium=referral