Thrift接口描述语言语法

1、数据类型

         基本类型:

                 bool:布尔值,对应java的boolean

                 byte:8 位有符号整数,对应java的byte

                 i16:16 位有符号整数,对应java的short

                 i32:32 位有符号整数,对应java的int

                 i64:64 位有符号整数,对应java的long

                 double:64 位浮点数,对应java的double

                 string:字符串,对应java的String

                 binary:字节数组,对应java的type[]

 

         结构体类型:

                 struct:定义公共的对象,类似于 C 语言中的结构体定义,在 Java 中是一个 JavaBean

                  struct Request {

                           1: string username;

                           2: string password;

                  }

 

          枚举类型:

                  enum Action{

                           Idle,

                           Attack,

                           Run

                  }

         容器类型:

                  list:对应 Java 的 ArrayList

                  set:对应 Java 的 HashSet

                  map:对应 Java 的 HashMap

         异常类型:

                  exception:对应 Java 的 Exception

                  exception RequestException {

                           1:required i32 code;

                           2:optional string reason;

                  }

         服务类型:

                  service:对应Java中的Interface

                  service HelloWordService {

                           string doAction(1:string name, 2:i32 age);

                  }

                  服务继承用extends关键字:

                  service HelloService extends BaseService{

                      

                  }

2、类型定义

         typedef i32 Integer

         typedef i64 Long

3、常量定义

         const string SERVER_IP = "127.0.0.1"

         const i32 SERVER_PORT = 5900

         const map<string, string> MAP_CONST = {"hello": "world", "goodnight": "moon"}

4、命名空间

         namespace 语言 路径

         namespace java com.seasy.thrift

5、文件包含

         include "global.thrift"

entity.thrift:
	namespace java com.seasy.entity
	struct RegisterEntity {
		1: string ip
		2: i32 port
		3: string description
	}

register.thrift:
	include "entity.thrift"
	namespace java com.seasy.service	  
	service RegisterService{   
		bool doRegister(1:entity.RegisterEntity regEntity) 
	}

         注意:要使用entity.thrift中的结构,一定要加entity前缀,也就是文件名做为类型名的前缀。如范例中的 entity.RegisterEntity 

6、注释

         #  这行是注释

         // 这行是注释

         /*

                  这行是注释

                  这行是注释

         */

7、必选与可选

         struct People{

                  1:required string name;

                  2:optional i32 age;

         }

猜你喜欢

转载自chenjumin.iteye.com/blog/2339082