avro入門之rpc,
關於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編寫跨語言的用戶端,具體內容還有待研究