标签:style blog http io ar color os 使用 sp
关于avro的rpc入门使用,官方给的资料实在是太少了,这个链接https://github.com/phunt/avro-rpc-quickstart有具体说明
现在对在java下使用总结如下:
参考:http://www.iteblog.com/archives/1008
http://my.oschina.net/zhzhenqin/blog/151040
http://www.bianceng.cn/Servers/web/201411/46469.htm
我这里没有使用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编写跨语言的客户端,具体内容还有待研究
标签:style blog http io ar color os 使用 sp
原文地址:http://blog.csdn.net/shb19891/article/details/41778631