Thrift入门基础知识-thrift文件(IDL)说明和生成目标语言源代码

上一篇 介绍过Thrift的类型,那这一篇来说说Thrift的IDL接口文件(The Thrift interface definition language)的结构。Thrift IDL文件由Thrift代码生成工具处理,生成各种目标语言的代码,以支持IDL文件中定义的结构和服务实现跨语言服务调用。

0、Comments - 注释

Thrift支持shell风格、C/C++/Java风格的注释。示例:

# shell风格注释.

/*
 * 多行注释
 * 类似C/C++/Java
 */

// C++/Java 当行注释

1、Description - 描述

Thrift文件的描述跟一般语言一样在文件顶部通过注释来表述服务的内容等等信息

2、Document - 文档

Thrift文档部分包括0个或者多个Header部分和Definition部分,下面先说Header部分

- Header - 头部分

Header部分包括Thrift include、C++ include和namespace定义

  • Thrift Include:将所有声明包含的Thrift文档都包含到当前Thrift中来,语法:

inlucde "文件名"

//示例
inlucde "other.thrift" 
  • C++ Include:将用户自己的C++头文件包含到当前Thrift中来,语法:

cpp_include "头文件"

//示例
cpp_include "string" 
  • Namespace:定义名称空间/包名/模块等等,可以使用编程语言名称规定某一特定语言的namespace,用*表示所有未匹配到的语言的namespace,语法:

namespace [语言名称] 标识符

//示例
namespace java thrift.test
namespace php ThriftTest
namespace cpp thrift.test
namespace * thrifttest

- Definition - 定义部分

定义部分包括 Const 、 Typedef 、 Enum 、 Senum 、 Struct 、 Union 、 Exception 、 Service

  • Const:定义常量,Thrift允许使用JSON来定义复杂类型和struct类型,语法:

const 字段类型 名称标识 = 值 | 列表

//示例
const i32 INT_CONST = 1234
const map<string,string>  MAP_CONST = {"test":"hello world"}
  • Typedef:Thrift 支持C/C++风格的类型自定义,语法:

typedef 原类型 自定义类型

typedef i32 MyInteger 
  • Enum:Thrift枚举类型只支持单个32位int类型数据,第一个元素如果没有给值那么默认是0,之后的元素如果没有给值,则是在前一个元素基础上加1,语法:

enum 名称标识 {
字段标识 = int常量
}

//示例
enum Numberz
{
  ONE = 1,
  TWO, //值是2,前一个元素加1
  THREE,  //值是3
  FIVE = 5,
  SIX,
  EIGHT = 8
}
  • Struct:这个在前一篇文章中有介绍, 类似于C的struct,是一系列相关数据的封装,在OOP语言中会转换为类(class),struct的每个元素包括一个唯一的数字标识、一个数据类型、一个名称和一个可选的默认值。语法:

struct 名称标识 {
数字标识: (required|optional)? 类型 名称标识 (= 值)?
}

//示例
struct Xtruct{
  1:  string string_thing,
  4:  required  i8  byte_thing,
  16: optional string optional_thing = "default value"
}

关于字段的必选和可选的说明:
required:
1、写:必须字段始终写入,并且应该设置这些字段。
2、读:必须字段始终读取,并且它们将包含在输入流中
3、默认值:始终写入
注意:如果一个必须字段在读的时候丢失,则会抛出异常或返回错误,所以在版本控制的时候,要严格控制字段的必选和可选,必须字段如果被删或者改为可选,那将会造成版本不兼容。

optional:
1、写:可选字段仅在设置时写入
2、读:可选字段可能是也可能不是输入流的一部分
3、默认值:在设置了isset标志时写入
Thrift使用所谓的“isset”标志来指示是否设置了特定的可选字段, 仅设置了此标志的字段会写入,相反,仅在从输入流中读取字段值时才设置该标志。

default:
1、写:理论上总是写入,但是有一些特例
2、读:跟optional一样
3、默认值:可能不会写入
默认类型是required和optional的结合,可选输入(读),必须输出(写)。

  • Union:类似于C++的union,成员默认全部是optional类型,语法

union 名称标识 {
数字标识: (required|optional)? 类型 名称标识 (= 值)?
}

//示例
union SomeUnion {
  2: string string_thing,
  3: i32 i32_thing
}
  • Exception:异常跟struct类似,会跟目标语言本地异常集成,语法:

exception 名称标识 {
数字标识: (required|optional)? 类型 名称标识 (= 值)?
}

//示例
exception Xception {
  1: i32 errorCode,
  2: string message
}
  • Service:service是Thrift 服务器提供的一系列功能列表接口,在客户端就是调用这些接口来完成操作,语法:

service 名称标识 (extends 名称标识)? {
Function*
}
其中Function语法为:
oneway? 返回类型 名称标识(字段列表) throws(字段列表)?

//示例
service ThriftTest {
  void         testVoid(),
  bool         testBool(1: bool thing),
  map<i32,i32> testMap(1: map<i32,i32> thing),
  Xtruct       testStruct(1: Xtruct thing),
  list<i32>    testList(1: list<i32> thing),
  void testException(1: string arg) throws(1: Xception err1),
  oneway void  testOneway(1:i32 secondsToSleep)
}

最后来一个官方示例:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * fb303.thrift
 */

namespace java com.facebook.fb303
namespace cpp facebook.fb303
namespace perl Facebook.FB303
namespace netcore Facebook.FB303.Test

/**
 * Common status reporting mechanism across all services
 */
enum fb_status {
  DEAD = 0,
  STARTING = 1,
  ALIVE = 2,
  STOPPING = 3,
  STOPPED = 4,
  WARNING = 5,
}

/**
 * Standard base service
 */
service FacebookService {

  /**
   * Returns a descriptive name of the service
   */
  string getName(),

  /**
   * Returns the version of the service
   */
  string getVersion(),

  /**
   * Gets the status of this service
   */
  fb_status getStatus(),

  /**
   * User friendly description of status, such as why the service is in
   * the dead or warning state, or what is being started or stopped.
   */
  string getStatusDetails(),

  /**
   * Gets the counters for this service
   */
  map<string, i64> getCounters(),

  /**
   * Gets the value of a single counter
   */
  i64 getCounter(1: string key),

  /**
   * Sets an option
   */
  void setOption(1: string key, 2: string value),

  /**
   * Gets an option
   */
  string getOption(1: string key),

  /**
   * Gets all options
   */
  map<string, string> getOptions(),

  /**
   * Returns a CPU profile over the given time interval (client and server
   * must agree on the profile format).
   */
  string getCpuProfile(1: i32 profileDurationInSec),

  /**
   * Returns the unix time that the server has been running since
   */
  i64 aliveSince(),

  /**
   * Tell the server to reload its configuration, reopen log files, etc
   */
  oneway void reinitialize(),

  /**
   * Suggest a shutdown to the server
   */
  oneway void shutdown(),

}

3、生成目标语言代码

下载官方代码生成工具,windows下载地址https://mirrors.tuna.tsinghua.edu.cn/apache/thrift/,其他平台下载官方源码包编译即可。
生成代码使用命令:

thrift --gen <language> <Thrift filename>

如果有thrift文件中有包含其他thrift,可以使用递归生成命令:

thrift -r --gen <language> <Thrift filename>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • Thrift 架构 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入...
    仇仇趣生活阅读 2,943评论 0 0
  • 一. 与 Thrift 的初识 也许大多数人接触 Thrift 是从序列化开始的。每次搜索 “java序列化” +...
    java菜阅读 1,250评论 0 6
  • 1. RPC 1.1 简介 RPC 的主要功能目标是让构建分布式计算(应用)更容易,在提供强大的远程调用能力时不损...
    wy_sure阅读 6,892评论 0 1
  • 原文链接:thrift入门 转载请注明出处~ Thrift简介 什么是thrift 简单来说,是Facebook公...
    是夏莞也是CiCi阅读 28,058评论 1 42
  • 抓住重点,意义,亮点,好处
    zhaodongbo阅读 161评论 0 0