Thrift RPC 系列教程(1)——Thrift语言

栏目: 服务器 · 发布时间: 5年前

内容简介:Thrift 这门编程语言提供了如下几种基础的数据类型:一般来说,我们也就常用那几种,就像在其他日常编程语言中一样。比如我,基本就是这『三板斧』:再让我们来看看,Thrift提供了哪些容器类型:

Thrift 这门编程语言提供了如下几种基础的数据类型:

  • bool: A boolean value (true or false)
  • byte: An 8-bit signed integer
  • i16: A 16-bit signed integer
  • i32: A 32-bit signed integer
  • i64: A 64-bit signed integer
  • double: A 64-bit floating point number
  • string: A text string encoded using UTF-8 encoding

一般来说,我们也就常用那几种,就像在其他日常编程语言中一样。比如我,基本就是这『三板斧』:

  • bool
  • i32 ( 现在逐步常用 i64 了,因为性能啥的,我基本不是第一时间关注的)
  • double
  • string

复杂数据类型(容器)

再让我们来看看,Thrift提供了哪些容器类型:

  • list: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
  • set: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated similar to a List
  • map: A map of strictly unique keys to values. Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. While defaults are provided, the type mappings are not explicitly fixed. Custom code generator directives have been added to allow substitution of custom types in various destination languages

简直了C++ STL 一毛一样,命名都差不多。唯独 list 这种数据结构,其实是『动态数组』,单从名字上看,很容易让人联系到链表,这在其他的编程语言中,也有这个现象,比如 Python 中的也叫做 list 。

class,即struct

稍微正常一点的语言,对 OOP 的支持,自然是必不可少的,我觉得,最好直接提供 class 这个关键字,尽量有清晰的语义。

但是 Thrift 只有一个 struct,基本上和 C 的struct,一样,也是功能少得可怜,不过考虑到它仅仅是一个中间语言,自然是情有可原的。

我们来看一下,一个写得好的 struct,应该如何定义,做到既清晰又完备的:

struct Person {
    1: required string name; // 必须字段,很明确
    2: required i64 age;
    3: optional string addr; // 可选字段
    4: optional string defaultValue = "DEFAULT"; // 默认字段
    5: string otherValue; // 不是很明确!
}
复制代码

interface,即service

在『面向接口编程』的原则下,『接口』是一个很重要的因素。有的人称之为函数,有的人称为方法,本文我们统称为『方法』。

在Thrift中,定义接口是一件很简单的事情( 摘自官网的一个示例 ):

// 接口, 还可以继承, 也许我们有时候可以搞个 『BaseService』 之类的,不过我很少用到。
service Calculator extends shared.SharedService {  
	
 // 正常方法,和C++这类传统语言,基本一模一样。
   void ping(),

   i32 add(1:i32 num1, 2:i32 num2),

   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
	
 // 特殊方法,基本很少用到了,在我有限的经历中,只使用过一次,读者没必要关注它
   oneway void zip()

}
复制代码

异常,即exception

关于异常,在Thrift中就像定义 struct 一样,因为exception从概念上讲,也是一种class,所谓『万事万物皆对象』嘛。不过现在我们用『exception』这个关键字,也正好符合我前文所讲的,清晰的语义。让我们看看Thrift中的异常是如何定义的:

exception InvalidOperation {
  1: i32 whatOp,
  2: string why
}
复制代码

枚举

枚举这个东西,真的是太重要了,和前面的exception类似,它也不过是一种class而已。不过Thrift中只支持枚举 int 值,比较遗憾,其实很多时候,对枚举的要求,我们是很丰富的,比如支持 枚举 string。Thrift中枚举如下:

enum Operation { // 功能着实比较孱弱
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}
复制代码

如果喜欢我的文章,请关注我的公众号:『浮生若梦的编程』。

也可以关注我的简书专栏:『浮生若梦的编程』。

或者加入我的知识星球,『浮生若梦的编程』,获取更多干货。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming Computer Vision with Python

Programming Computer Vision with Python

Jan Erik Solem / O'Reilly Media / 2012-6-22 / USD 39.99

If you want a basic understanding of computer vision's underlying theory and algorithms, this hands-on introduction is the ideal place to start. As a student, researcher, hacker, or enthusiast, you'll......一起来看看 《Programming Computer Vision with Python》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具