编写thrift文件

 1.类型

thrift java类型
i16 short
i32 int
i64 long
stirng String
double double
list List

 

 

 

 

 

 

2.常量定义关键字:const

3.定义实体类关键字:struct

4.定义service关键字:service

5.namespace:

  eg:namespace java com.XXX.XXX

6.引入其他thrift文件

  1)include 其他文件

  2)typedef 文件里的类

例子:

1.Common.thrift

namespace java com.sankuai.mall.wms.tservice.common

/**
* 未知错误
**/
const i32 Unknown_Error = -1

/** 成功 */
const i32 SUCCESS = 0

/**
* 无效参数
**/
const i32 BAD_PARAMETERS = 2
/**
* 无此方法
**/
const i32 METHOD_NOT_FOUND = 3
/**
* 签名验证失败
**/
const i32 SIGNATURE_VERIFICATION_FAILED = 4
/**
* 未登录
**/
const i32 UNAUTHORIZED = 5
/**
* 账号密码错误
**/
const i32 WRONG_PASSWOR = 6
/**
* 请求来源非法
**/
const i32 INVALID_REFERER = 7
/**
* 无效时间
**/
const i32 INVALID_TIME = 8
/**
* 非法访问
**/
const i32 ACCESS_DENIED = 9
/**
* 服务内部异常
**/
const i32 INTERNAL_ERROR = 10
/**
* 请求超出接口限额
**/
const i32 TOO_MANY_REQUESTS = 11

/**
* 业务异常
**/
const i32 BUSINESS_ERROR = 110



struct Paging {
1: i32 offset = 0
2: i32 limit = 10
3: optional bool pagination = true
4: optional bool isTotal = true
}

/**
* 接口异常
**/
exception TException {
/** 错误码 */
1:i32 code
/** 错误消息 */
2:string msg
}

2.TestTService.thrift

namespace java com.sankuai.mall.wms.tservice.test

include "Common.thrift"

typedef Common.Paging Paging
typedef Common.TException TException


//参数
struct TestTRequest{
    1:i64 id//id
    2:Paging paging;//分页参数
}
//返回值
struct TestTResponse{ 1:i64 id;//id 2:string name; 3:i32 age; 4:double weight;//体重 } //test thrift服务 service TestTService{ TestTResponse test(1:TestTRequest request)throws (1:TException e) }

 

posted @ 2017-08-04 14:56  刘尊礼  阅读(182)  评论(0)    收藏  举报