摘自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. response小结(一)——用response向客户端输出中文数据(乱码问题分析)

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象.request和response对象既然代表请求和响应,那我们要 ...

  2. python学习day4--python基础--购物小程序

    '''购物小程序:用户启动时先输入工资用户启动程序后打印商品列表允许用户选择购买商品允许用户不断购买各种商品购买时检测余额是否够,如果够直接扣款,否则打印余额不足允许用户主动退出程序,退出时打印已购商 ...

  3. SignalR 2.0入门

    下载已完成的项目 本教程展示如何使用那么 SignalR 创建一个实时聊天应用程序.你会那么 SignalR 添加一个空的 ASP.NET web 应用程序,创建一个 HTML 页面发送并显示消息. ...

  4. 【分享】.Net有哪些大型项目、大型网站的案例?

    .Net开发的部分知名网站案例:http://www.godaddy.com  全球最大域名注册商http://www.ips.com  环迅支付,国内最早的在线支付平台http://www.icbc ...

  5. Access和Sql区别

    假设表game有一字段为gameYuiJian为bit字段(SQL SERVER 20005)和"是/否"字段(ACCSS数据库),在编写脚本文件时,如下才能正确执行 SQL st ...

  6. 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用物理引擎的开发过程,熟悉这些API的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...

  7. iOS动画——弹窗动画(pop动画)

    用pop动画简单实现弹窗的缩放和渐变,感觉这个动画常用,就写一下博客 pop动画是Facebook推出的动画引擎,请自行到GitHub上搜索下载拖拽导入xcode项目中. 更多pop动画使用和原理可网 ...

  8. OC9_字符串的内存管理

    // // main.m // OC9_字符串的内存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangx ...

  9. js及jQuery实现checkbox的全选、反选和全不选

    html代码: <label><input type="checkbox" id="all"/>全选</label> < ...

  10. java集合 collection-list-ArrayList 去除ArrayList集合中的重复元素。

    import java.util.*; /* 去除ArrayList集合中的重复元素. */ class ArrayListTest { public static void sop(Object o ...