本文共 6509 字,大约阅读时间需要 21 分钟。
是hadoop的一个子项目,提供的功能与thrift、Protocol Buffer类似,都支持二进制高效序列化,也自带RPC机制,但是avro使用起来更简单,无需象thrift那样生成目标语言源代码,目前支持的语言有java、c#、php、c++等(详情见:),hadoop生态圈中的hive、pig已经在使用avro
avro-client模块中的pom.xml参考以下内容:
1 25 4.0.0 6 7yjmyzz.avro 8avro-client 91.0 10 1112 16 17UTF-8 132.3.2 141.7.5 1518 47 4819 24junit 20junit 214.10 22test 2325 30org.slf4j 26slf4j-simple 271.6.4 28compile 2931 35org.apache.avro 32avro 33${avro.version} 3436 40 41org.apache.avro 37avro-ipc 38${avro.version} 3942 46yjmyzz.avro 43avro-contract 441.0 4549 74 75 7650 7351 55org.apache.maven.plugins 52maven-compiler-plugin 53${compiler-plugin.version} 5456 72org.apache.avro 57avro-maven-plugin 58${avro.version} 5960 7161 70schemas 62generate-sources 63 6465 69schema 66protocol 67idl-protocol 68
一、定义文件示例
Person.avsc
{ "namespace": "yjmyzz.avro.study.dto", "type": "record", "name": "Person", "fields": [ { "name": "age", "type": "int" }, { "name": "name", "type": "string" }, { "name": "sex", "type": "boolean" }, { "name": "salary", "type": "double" }, { "name": "childrenCount", "type": "int" } ]}
QueryParameter.avsc
{ "namespace": "yjmyzz.avro.study.dto", "type": "record", "name": "QueryParameter", "fields": [ { "name": "ageStart", "type": "int" }, { "name": "ageEnd", "type": "int" } ]}
DemoService.avdl
@namespace ("yjmyzz.avro.study.service")protocol DemoService{ import schema "Person.avsc"; import schema "QueryParameter.avsc"; string ping(); arraygetPersonList(yjmyzz.avro.study.dto.QueryParameter queryParameter);}
二、服务端
DemoServiceImpl.java
package yjmyzz.avro.study;import yjmyzz.avro.study.dto.Person;import yjmyzz.avro.study.dto.QueryParameter;import yjmyzz.avro.study.service.DemoService;import java.util.ArrayList;import java.util.List;public class DemoServiceImpl implements DemoService { public String ping() { System.out.println("ping()"); return "pong"; } public ListgetPersonList(QueryParameter parameter) { //System.out.println(parameter.getAgeStart() + " - " + parameter.getAgeEnd()); List list = new ArrayList (10); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setAge(i); p.setChildrenCount(i); p.setName("test" + i); p.setSalary(10000D); p.setSex(true); list.add(p); } return list; }}
AvroServer.java
package yjmyzz.avro.study;import org.apache.avro.ipc.NettyServer;import org.apache.avro.ipc.Server;import org.apache.avro.ipc.specific.SpecificResponder;import yjmyzz.avro.study.service.DemoService;import java.net.InetSocketAddress;public class AvroServer { public static void main(String[] args) { System.out.println("Starting avro server..."); Server server = new NettyServer(new SpecificResponder(DemoService.class, new DemoServiceImpl()), new InetSocketAddress(65111)); System.out.println("Avro erver started."); }}
三、客户端
AvroClient.java
package yjmyzz.avro.study;import org.apache.avro.AvroRemoteException;import org.apache.avro.ipc.NettyTransceiver;import org.apache.avro.ipc.specific.SpecificRequestor;import yjmyzz.avro.study.dto.QueryParameter;import yjmyzz.avro.study.service.DemoService;import java.net.InetSocketAddress;public class AvroClient { public static void main(String[] args) throws Exception { NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111)); DemoService proxy = (DemoService) SpecificRequestor.getClient(DemoService.class, client); System.out.println(proxy.ping()); int max = 100000; Long start = System.currentTimeMillis(); for (int i = 0; i < max; i++) { call(proxy); } Long end = System.currentTimeMillis(); Long elapse = end - start; int perform = Double.valueOf(max / (elapse / 1000d)).intValue(); System.out.print("avro " + max + " 次RPC调用,耗时:" + elapse + "毫秒,平均" + perform + "次/秒"); // cleanup client.close(); } private static void call(DemoService proxy) throws AvroRemoteException { //client.ping(); //System.out.println("ping()=>" + client.ping()); QueryParameter parameter = new QueryParameter(); parameter.setAgeStart(5); parameter.setAgeEnd(50); proxy.getPersonList(parameter); //System.out.println(client.getPersonList(parameter)); }}
avro 100000 次RPC调用,耗时:18617毫秒,平均5371次/秒
注:虽然很多关于thrift、avro的性能评测文章提到avro性能不输于thrift,但就本文的示例而言,在同一台笔记本上,avro的性能只有thrift的约1/2.
附:文中示例代码下载
参考文章:
转载地址:http://ayato.baihongyu.com/