摘自http://avro.apache.org/docs/current/spec.html#Protocol+Declaration,1.7.6版

Protocol Declaration

Avro protocols describe RPC interfaces. Like schemas, they are defined with JSON text.

A protocol is a JSON object with the following attributes:

  • protocol, a string, the name of the protocol (required);
  • namespace, an optional string that qualifies the name;
  • doc, an optional string describing this protocol;
  • types, an optional list of definitions of named types (records, enums, fixed and errors). An error definition is just like a record definition except it uses "error" instead of "record". Note that forward references to named types are not permitted.
  • messages, an optional JSON object whose keys are message names and whose values are objects whose attributes are described below. No two messages may have the same name.

The name and namespace qualification rules defined for schema objects apply to protocols as well.

Avro protocol 描述了RPC接口,和schema一样,用JSON定义。

一个protocol是一个JSON对象,有以下的属性:

  • protocol,必需,这个protocol的名字
  • namespace, 可选,用来qualify protocol的名字
  • doc, 可选,描述这个protocol
  • types, 可选,定义named types的列表(records, enums, fixed, errors)。error的定义和record一样,除了它使用error,而不是record. 注意named type不能进行forward reference. (注:就是定义了一些类型,然后给每个类型起个名字)
  • messages, 可选,是一个JSON对象。它的key是message name, values是JSON 对象, 下边会讲这个JSON对象的属性。message不能有相同的名字。

用于schema object的命名规则和namespace qualification规则同样适用于protocol

Messages

A message has attributes:

  • doc, an optional description of the message,
  • request, a list of named, typed parameter schemas (this has the same form as the fields of a record declaration);
  • response schema;
  • an optional union of declared error schemas. The effective union has "string" prepended to the declared union, to permit transmission of undeclared "system" errors. For example, if the declared error union is ["AccessError"], then the effective union is ["string", "AccessError"]. When no errors are declared, the effective error union is ["string"]. Errors are serialized using the effective union; however, a protocol's JSON declaration contains only the declared union.
  • an optional one-way boolean parameter.

A request parameter list is processed equivalently to an anonymous record. Since record field lists may vary between reader and writer, request parameters may also differ between the caller and responder, and such differences are resolved in the same manner as record field differences.

The one-way parameter may only be true when the response type is "null" and no errors are listed.

message有以下属性:

  • doc, 可选,对消息的说明
  • request,  一个参数列表,其中的参数拥有名字和类型(和record中的field的定义格式一样)  。(注:即需要说明参数的名字和类型)
     "request": [{"name": "greeting", "type": "Greeting" }]
  • response的schema 
    "response": "Greeting",
  • error, 用一个declared union来描述,可选。之所以叫"delcared" union,是因为实际上隐式地附加了一个String到这个union中,这样没有声明的"system error" 也可以传输了。例如,declared error union是["AccessError"],effective union就是["String", "AccessError"]。如果没有声明error, effective error union就是["string"]。错误 被用effective union序列化。但是这个protocol的JSON形式的定义里就只用写declared union了。
  • one-way 布尔类型参数,可选

请求的参数列表就和匿名record一样处理。因为record field列表在reader和writer间可以不同,request 参数在caller和responder之间也可以不同,这样的不同和record field不同采用同样的方式处理。

当response 类型是null,并且没有列出error时,one-way parameter只能是true.

Sample Protocol

For example, one may define a simple HelloWorld protocol with:

{
"namespace": "com.acme",
"protocol": "HelloWorld",
"doc": "Protocol Greetings", "types": [
{"name": "Greeting", "type": "record", "fields": [
{"name": "message", "type": "string"}]},
{"name": "Curse", "type": "error", "fields": [
{"name": "message", "type": "string"}]}
], "messages": {
"hello": {
"doc": "Say hello.",
"request": [{"name": "greeting", "type": "Greeting" }],
"response": "Greeting",
"errors": ["Curse"]
}
}
}

总结:搞了这么多,这个protocol的定义实际上就是定义了一些用于RPC的方法,不过在定义方法时要声明方法的参数名、参数类型、返回类型、异常啥的。这些方法要定义于一个接口,所以还要说明这个接口的名字(在上边的例子中是HelloWorld).

生成对应的java代码

把上边的protocol写上一个HelloWorld.avpr中,然后执行java -jar avro-tools- 1.7.6.jar compile protocol HelloWorld.avpr /target/java 就会得到三个文件

  • Curse.java. public class Curse extends org.apache.avro.specific.SpecificExceptionBase implements org.apache.avro.specific.SpecificRecord 这个就是上边定义的error,这个类是一个异常。
  • Greeting.java.public class Greeting extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord 这个就是参数类型,被生成为一个类,它是一个record类型。
  • HelloWorld.java
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package com.acme; @SuppressWarnings("all")
/** Protocol Greetings */
@org.apache.avro.specific.AvroGenerated
public interface HelloWorld {
public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse("{\"protocol\":\"HelloWorld\",\"namespace\":\"com.acme\",\"doc\":\"Protocol Greetings\",\"types\":[{\"type\":\"record\",\"name\":\"Greeting\",\"fields\":[{\"name\":\"message\",\"type\":\"string\"}]},{\"type\":\"error\",\"name\":\"Curse\",\"fields\":[{\"name\":\"message\",\"type\":\"string\"}]}],\"messages\":{\"hello\":{\"doc\":\"Say hello.\",\"request\":[{\"name\":\"greeting\",\"type\":\"Greeting\"}],\"response\":\"Greeting\",\"errors\":[\"Curse\"]}}}");
/** Say hello. */
com.acme.Greeting hello(com.acme.Greeting greeting) throws org.apache.avro.AvroRemoteException, com.acme.Curse; @SuppressWarnings("all")
/** Protocol Greetings */
public interface Callback extends HelloWorld {
public static final org.apache.avro.Protocol PROTOCOL = com.acme.HelloWorld.PROTOCOL;
/** Say hello. */
void hello(com.acme.Greeting greeting, org.apache.avro.ipc.Callback<com.acme.Greeting> callback) throws java.io.IOException;
}
}

  它是一个接口,定义了一个叫hello的方法,就是前边给message起的名字。它的参数和返回类型、抛出的异常都符合对应procotol的声明。doc被转换为了对应于方法和类的注释。

Avro RPC 之 Protocol 定义和代码生成的更多相关文章

  1. rpc框架之avro 学习 1 - hello world

    avro是hadoop的一个子项目,提供的功能与thrift.Protocol Buffer类似,都支持二进制高效序列化,也自带RPC机制,但是avro使用起来更简单,无需象thrift那样生成目标语 ...

  2. Avro实现RPC

    场景:一个客户端,一个服务端(创建两个avro工程).客户端向服务端发送数据,服务端根据算法算出结果,返回给客户端. Http主外,RPC主内.(解决分布式环境下,节点间的数据通信或远程过程调用) 实 ...

  3. [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等

    原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...

  4. Apache Avro 与 Thrift 比较

    http://www.tbdata.org/archives/1307 Avro和Thrift都是跨语言,基于二进制的高性能的通讯中间件. 它们都提供了数据序列化的功能和RPC服务. 总体功能上类似, ...

  5. RPC框架基本原理(三):调用链路分析

    本文主要阐述下RPC调用过程中的寻址,序列化,以及服务端调用问题. 寻址 随机寻址 从可用列表中,随机选择地址 一致性寻址 可用服务地址一致性hash管理:根据可服务的地址,构造treemap,计算c ...

  6. 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等

    作者:大数据女神-诺蓝(微信公号:dashujunvshen).本文是36大数据专稿,转载必须标明来源36大数据. 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要 ...

  7. 谈谈如何使用Netty开发实现高性能的RPC服务器

    RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络,从远程计算机程序上请求服务,而不必了解底层网络技术的协议.说的再直白一点,就是客户端在不必知道 ...

  8. RPC介绍

    转载http://blog.csdn.net/mindfloating/article/details/39474123/ 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 ...

  9. Netty开发实现高性能的RPC服务器

    Netty开发实现高性能的RPC服务器 RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络,从远程计算机程序上请求服务,而不必了解底层网络技术的协 ...

随机推荐

  1. 新年第一次分享sqlserver技术

    走向DBA[MSSQL篇] - 从SQL语句的角度提高数据库的访问性能 最近公司来一个非常虎的DBA,10几年的经验,这里就称之为蔡老师吧,在征得我们蔡老同意的前提下 ,我们来分享一下蔡老给我们带来的 ...

  2. Office升级到2013版后无法登录微软账号问题

    自打office从2010版升级到2013版,就再也无法登录微软账号了.每次点击登录,弹出来的框就显示:this feature has been disabled by your administr ...

  3. stl的实现原理简单讲解,通熟易懂

    总结 需要经常随机访问请用vector 2.list list就是双向链表,元素也是在堆中存放,每个元素都是放在一块内存中,它的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随机存 ...

  4. 【转帖】error C2296: “^”: 非法,左操作数包含“double”类型

    想要实现 ,写的C++程序为 double x; x=2^3; 结果程序总是出现这样的错误:error C2296: “^”: 非法,左操作数包含“double”类型 后来才发现操作符“^”,在C++ ...

  5. Linux 配置

    零:个性化 主题:Radiance 主题颜色: gtk-color-scheme = "base_color:#CCE8CF\nfg_color:#006400\ntooltip_fg_co ...

  6. [java学习笔记]java语言核心----面向对象之this关键字

    一.this关键字 体现:当成员变量和函数的局部变量重名时,可以使用this关键字来区别:在构造函数中调用其它构造函数 原理:         代表的是当前对象.         this就是所在函数 ...

  7. 【Qt】Qt环境搭建(Visual Studio)【转】

    简述 经常有人问我编写Qt程序时使用什么IDE,其实这个真的很难回答(各有所长),只能说看个人爱好了,因为我两个都用,而且两个都很喜欢(比较多情吧O(∩_∩)O~)! 下面将进行Qt Creator与 ...

  8. CSS实现不固定宽度和高度的自动居中

    有时候我们需要实现下面这种效果: 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大 ...

  9. Python串行运算、并行运算、多线程、多进程对比实验

    转自:http://www.redicecn.com/html/Python/20111223/355.html Python发挥不了多核处理器的性能(据说是受限于GIL,被锁住只能用一个CPU核心, ...

  10. php截取字符串的实例代码(支持utf-8)

    分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...