Thrift manual

来源:互联网 发布:类似精易编程助手 编辑:程序博客网 时间:2024/06/01 08:00

Thrift manual

我的github pages 地址:https://alexanderwangsgithub.github.io/

  • Thrift manual
    • Install
    • Grammer
      • namespace
      • Primary
      • Collection
      • Enum
      • Struct
      • Service
      • Exception
    • Generate
    • Tutorial

Install

brew install thrift

Grammer

namespace

As same as “package” in Java.
namespace java resource.thrift.sdk.hermes

结合mv,管理路径。

Primary

  • byte: 有符号字节
  • i16: 16位有符号整数
  • i32: 32位有符号整数
  • i64: 64位有符号整数
  • double: 64位浮点数
  • string: 字符串

Collection

Element in collection can be any types except service.

  • list
  • set
  • map

Enum

enum THermesTemplateVerifyStatus {    PENDING = 0    SUCCESS = 1    FAIL = 2}

Struct

struct THermesUserReply {    1: required i64 id    2: required string phone_number    3: required Timestamp reply_time    4: required string message}

会生成一个同名class(Java Bean)
bool/byte/i16/i32/i64/double/enum类型的处理逻辑
[required属性字段]转为原生类型:boolean/byte/short/int/long/double/int
[optional属性字段]转为装箱类型:Boolean/Byte/Short/Integer/Long/Double/Integer
[default属性字段]转为装箱类型:Boolean/Byte/Short/Integer/Long/Double/Integer
如服务端实现中有特殊逻辑,可以在和服务方确认逻辑后手动修改类型
Thrift中每个字段都会生成一个private字段,一个public getter,一个public setter
生成的每个字段都会打上@Index(index)注解
字段不支持默认值

Service

service HermesService {    bool ping()        throws (1: HermesUserException user_exception,                2: HermesSystemException system_exception,                3: HermesUnknownException unknown_exception),

Exception

exception HermesUnknownException {    1: required HermesErrorCode error_code,    2: required string error_name,    3: required string message,}

Generate

thrift -gen java hermes.thrift

Tutorial

tutorial

基础使用教程

基础使用教2

Java版的各种Thrift server实现的比较

0 0