首页 技术 正文
技术 2022年11月16日
0 收藏 540 点赞 4,498 浏览 5369 个字

avro是hadoop的一个子项目,提供的功能与thrift、Protocol Buffer类似,都支持二进制高效序列化,也自带RPC机制,但是avro使用起来更简单,无需象thrift那样生成目标语言源代码,目前支持的语言有java、c#、php、c++等(详情见:https://cwiki.apache.org/confluence/display/AVRO/Supported+Languages),hadoop生态圈中的hive、pig已经在使用avro

rpc框架之avro 学习 1 – hello world

avro-client模块中的pom.xml参考以下内容:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>yjmyzz.avro</groupId>
<artifactId>avro-client</artifactId>
<version>1.0</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler-plugin.version>2.3.2</compiler-plugin.version>
<avro.version>1.7.5</avro.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${avro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro-ipc</artifactId>
<version>${avro.version}</version>
</dependency> <dependency>
<groupId>yjmyzz.avro</groupId>
<artifactId>avro-contract</artifactId>
<version>1.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase> <goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

一、定义文件示例

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();
array<yjmyzz.avro.study.dto.Person> getPersonList(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 List<Person> getPersonList(QueryParameter parameter) {
//System.out.println(parameter.getAgeStart() + " - " + parameter.getAgeEnd()); List<Person> list = new ArrayList<Person>(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://code.taobao.org/svn/avro-rpc-demo/trunk/

参考文章:
https://github.com/phunt/avro-rpc-quickstart

http://avro.apache.org/docs/current/spec.html#Protocol+Declaration

http://avro.apache.org/docs/current/idl.html

http://avro.apache.org/docs/current/gettingstartedjava.html

Apache Avro 与 Thrift 比较

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300