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

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 比较

rpc框架之avro 学习 1 - hello world的更多相关文章

  1. rpc框架之 avro 学习 2 - 高效的序列化

    同一类框架,后出现的总会吸收之前框架的优点,然后加以改进,avro在序列化方面相对thrift就是一个很好的例子.借用Apache Avro 与 Thrift 比较 一文中的几张图来说明一下,avro ...

  2. rpc框架之gRPC 学习 - hello world

    grpc是google在github于2015年开源的一款RPC框架,虽然protobuf很早google就开源了,但是google一直没推出正式的开源框架,导致github上基于protobuf的r ...

  3. rpc框架: thrift/avro/protobuf 之maven插件生成java类

    thrift.avro.probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都 ...

  4. rpc框架之 thrift 学习 1 - 安装 及 hello world

    thrift是一个facebook开源的高效RPC框架,其主要特点是跨语言及二进制高效传输(当然,除了二进制,也支持json等常用序列化机制),官网地址:http://thrift.apache.or ...

  5. rpc框架之 thrift 学习 2 - 基本概念

    thrift的基本构架: 上图源自:http://jnb.ociweb.com/jnb/jnbJun2009.html 底层Underlying I/O以上的部分,都是由thrift编译器生成的代码, ...

  6. 服务化实战之 dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    转自: http://blog.csdn.net/liubenlong007/article/details/54692241 概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺 ...

  7. dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...

  8. RPC框架学习+小Demo实例

    一.什么是RPC协议? 全称:远程过程调度协议 效果:使消费者向调用本地方法一样调用远程服务方法,对使用者透明 目前常用:Dubbo.Thirft.Sofa.... 功能: 建立远程通信(socket ...

  9. 简易RPC框架-学习使用

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. jquery叠加页片自动切换特效

    查看效果:http://keleyi.com/keleyi/phtml/jqtexiao/34.htm 下面是HTML代码: <!DOCTYPE html> <html xmlns= ...

  2. 【转】js 中导出excel 较长数字串会变为科学计数法

    [转]js 中导出excel 较长数字串会变成科学计数法 在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串 ...

  3. 1-1 console的用法

    console里面具体提供了哪些方法可以供我们平时调试时使用. 目前控制台方法和属性有: ["$$", "$x", "dir", " ...

  4. Android开发7:简单的数据存储(使用SharedPreferences)和文件操作

    前言 啦啦啦~大家好,又见面啦~ 本篇博文讲和大家一起完成一个需要注册.登录的备忘录的,一起学习 SharedPreferences 的基本使用,学习 Android 中常见的文件操作方法,复习 An ...

  5. SharePoint 2013 工作流之使用Designer配置示例篇

    在SharePoint 2013中,支持SharePoint Designer 2013(以下简称SPD)配置简单的工作流,完成我们的业务需要.下面,我们就举一个小例子,实现SPD配置工作流. 1. ...

  6. SharePoint 2013 工作流之年假审批Designer配置篇

    本文介绍SharePoint 2013 使用Designer工具,设计年假审批工作流,由于流程所用的条件和操作都比较简单,所以演示为主,最后附流程图和流程的文本图,有兴趣的可以参照实验.如果对于Des ...

  7. ButterKnife基本使用

    ButterKnife基本使用 Butter Knife处理字段和方法绑定.   重要更新: 目前(2016.4.29), ButterKnife的最新版本是8.0.1. Demo项目已更新: htt ...

  8. [转]File Descriptor泄漏导致Crash: Too many open files

    在实际的Android开发过程中,我们遇到了一些奇奇怪怪的Crash,通过sigaction再配合libcorkscrew以及一些第三方的Crash Reporter都捕获不到发生Crash的具体信息 ...

  9. IOS开发基础知识--碎片36

    1:tabBarController跳转到另一个一级页面 当我们用tabBarController时,若已经到其中一个TabBar的子页,又要跳转到某一个一级的页面时,可以这样写 //这样就可以避免跳 ...

  10. 设置Hyper V

    1.打开服务器管理器 2.添加角色和功能 3.安装类型 -> 基于角色或基于功能的安装 4.服务器选择 -> 下一步 5.服务器角色 勾选"Hyper V"