摘自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. SignalR 2.0 系列:SignalR的服务器广播

    英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP.NET SignalR 2.0系列的翻译,这里是第八篇:SignalR的服务器广 ...

  2. JavaBean在JSP中显示时间

    创建DateTimeBean的类,将其放置于org.caiduping.bean的包中,实现时间,星期的封装. package org.caiduping.bean; import java.text ...

  3. ios开发入门篇(三):UITableView简介

    最近做项目又开始用到了uitableview,温习之余,在这里把uitableview的用法分享一下,有不对的地方欢迎大家提出来. 废话不多说,先创建一个工程,由于Xcode6,去除了创建工程时的空项 ...

  4. xml的生成与解析_老师笔记

    使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...

  5. hibernate的运行流程

    首先了解什么是对象关系映射,ORM(Object/Relationship Mapping):对象关系映射.对象关系映射是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.是通过使用描述对象 ...

  6. md5值计算

    1.md5(Message Digest 5th/消息概要加密算法 第5版) REFER: MD5 On wikipedia 2.应用范围 ① 验证下载文件的完整性 ② 3.关于MD5的几个问题 ①只 ...

  7. (POJ 3694) Network 求桥个数

    题目链接:http://poj.org/problem?id=3694Description A network administrator manages a large network. The ...

  8. Asp.Net Web API VS Asp.Net MVC

    http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...

  9. mac安装memcache

    1.wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz 2.tar zxvf memcache-2.2 ...

  10. PHP curl 模拟登录

    //提交数据,生成cookie,将cookie保存在临时目录下//在指定目录中建立一个具有唯一文件名的文件.如果该目录不存在,tempnam() 会在系统临时目录中生成一个文件,并返回其文件名 $co ...