Avro Getting started with RPC

Source: Internet
Author: User
Tags log log

About Avro RPC get started using, the official information is too little, this link https://github.com/phunt/avro-rpc-quickstart have specific instructions

The following summary is now available for use in Java:

Reference: http://www.iteblog.com/archives/1008

http://my.oschina.net/zhzhenqin/blog/151040

Http://www.bianceng.cn/Servers/web/201411/46469.htm

I do not use Maven here, directly in the project to add the use of the jar package is: 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
Of course, if you need, you can also compile in the Avro source code, get Avro-1.7.7.jar and Avro-tools-1.7.7.jar

The AVRO protocol is a JSON-structured descriptive text. The protocol defines the data type, name of the basic communication. It also contains callable methods, and so on. Define the protocol file first

{    "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 "}}    }
Save on D-plate A.avro

Then write the service-side code:

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 = L    Ogfactory.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 ...//get the type of the return value Remessage = new Genericdata.record (super.getlocal (). GetType ("namemess             Age "));directly constructs the reply 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 (); }    }}
Next write the client code:

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"));    Here if you want to run on two machines remember to change localhost to the server IP requestor = new Genericrequestor (protocol, T); } @Test public void Testsendmessage () throws Exception {Genericrecord requestData = new Genericdata.record (p        Rotocol.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); }}
Before running the client on the server, you can see that the client received the message.
{"Name": "Zhenqin"} Hello, zhenqin{"name": "Hello, Zhenqin"}
Above reference http://my.oschina.net/zhzhenqin/blog/151040

As for writing a cross-language client with Python, the details are still to be researched.

Avro Getting started with RPC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.