摘自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被转换为了对应于方法和类的注释。

转载于:https://www.cnblogs.com/devos/p/3606141.html

Avro RPC 之 Protocol 定义和代码生成相关推荐

  1. 【Avro二】Avro RPC框架

    1. Avro RPC简介 1.1. RPC RPC逻辑上分为二层,一是传输层,负责网络通信:二是协议层,将数据按照一定协议格式打包和解包 从序列化方式来看,Apache Thrift 和Google ...

  2. Avro RPC的两种实现方法:静态实现和动态实现

    使用Avro可实现如下几种方式的轻量级RPC, 每种方式都可用动态编码和静态编码来实现: HTTP: HttpServer HttpTransceiver UDP DatagramServer Dat ...

  3. Flume sink=avro rpc connection error

    要求 conf 文件 a1.sources=r1 a1.sinks=k1 a1.channels=c1a1.sources.r1.type=avro a1.sources.r1.bind=master ...

  4. Apache Avro 与 Thrift 比较

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Avro ...

  5. Apache Avro 入门

    Avro是Hadoop中的一个子项目,也是Apache中一个独立的项目,Avro是一个基于二进制数据传输高性能的中间件.在Hadoop的其他项目中例如HBase(Ref)和Hive(Ref)的Clie ...

  6. Apache Avro简介,java实现官网翻译

    文章目录 Apache Avro™ Introduction Schemas Comparison with other systems JAVA简单使用 Defining a schema Seri ...

  7. Apache Avro简介

    目录 介绍 Schema 与其他系统的比较 本文翻译自:http://avro.apache.org/docs/current/index.html 更多信息请查阅:http://avro.apach ...

  8. 使用Thrift RPC编写程序

    http://dongxicheng.org/search-engine/thrift-rpc/ 1. 概述 本文以C++语言为例介绍了thrift RPC的使用方法,包括对象序列化和反序列化,数据传 ...

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

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

最新文章

  1. ReSharper 配置及用法
  2. python 内存二进制读取图片
  3. Windows 7系统垃圾清理自写程序
  4. 读取文件:TypeError: an integer is required (got type str)
  5. 机器人学习--感知环境数据集
  6. 在ABAP debugger里手动trigger DB commit
  7. 在Spring中使用JDBCJobStore配置Quartz
  8. java连接oracle数据库jdbc
  9. Linux 内存管理之 SLUB分配器(1): Object-layout
  10. PostgreSQL和MySQL
  11. 批量txt数据转换为excel
  12. Visual Basic快速入门
  13. 产品逻辑图和产品流程图的区别?
  14. html css视频播放器,jQuery和CSS 3定制HTML 5视频播放器
  15. windows保护无法启动修复服务器,win10系统使用“sfc /scannow”修复系统提示Windows资源保护无法启动修复服务怎么办...
  16. python程序设计基础之turtle库制作简单的小图片
  17. 批量付款到户接口,批量转账接口
  18. Fama-French 三因子模型介绍、修改与框架搭建
  19. (论文笔记06.High Fidelity Data Reduction for Big Data Security Dependency Analyses(CCF A)2016)
  20. iOS开发系列--数据存取

热门文章

  1. 【golang】timer 和 ticker
  2. 服务器到底是个什么东东?跟电脑有啥区别?
  3. Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码
  4. java mq编程_MQ java 基础编程
  5. 最大似然估计与极大似然估计_使用最大似然估计对NFL球队排名
  6. C#访问大华网络摄像头
  7. [多-元-智-能]理论 IQ智商 EQ情商 AQ逆商 FQ财商 HQ健商 BQ戆商 CQ创商 MQ德商 DQ胆商 MQ心商 WQ志商 SQ灵商...
  8. 学生托管班_托管班一般多少钱一个月
  9. Html网页设计-美食网站
  10. 微信开发者工具,page里面的data在js的方法里面修改