Rpc for getting started with avro,

Source: Internet
Author: User

Rpc for getting started with avro,

For more information about avro rpc, visit https://github.com/phunt/avro-rpc-quickstart.

The usage in java is summarized as follows:

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

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

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

I am not using maven here, and the jar packages I add to use directly in the project are: 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 it, you can also compile in Avro source code to get avro-1.7.7.jar and avro-tools-1.7.7.jar

The Avro protocol is a JSON Structured description text. The Protocol defines the data type and name of the basic communication. It also contains callable methods. First define the Protocol File

{    "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 to drive D a. avro

Then write the server 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 = 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... // type of the returned value reMessage = new GenericData. record (super. getLocal (). getType ("nameMessage"); // directly constructs a 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:/. 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:/. avro "); Transceiver t = new HttpTransceiver (new URL (" http: // localhost: 8088 ")); // if you want to run it on two machines, change localhost to the server ip address 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 );}}
Run the server first and run the client. You can see that the client receives the message.
{"name": "zhenqin"}Hello, zhenqin{"name": "Hello, zhenqin"}
Above reference http://my.oschina.net/zhzhenqin/blog/151040

For cross-language clients written in python, the specific content remains to be studied.

Related Article

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.