thrift实践

thrift exercises

  1. how to define a DTO?

<code>
struct CourseDTO {
1:i64 id,
2:string title,
3:string teacherName,
4:i64 beginTime
}
</code>

  1. how to define enum?
    <code>
    // 定义枚举
    enum STUDENT_SEXY {
    MAN = 1,
    FEMALE = 2
    }
    </code>

  2. how to refer an class?

<code>
include "../dto/StudentDTO.thrift"
service CourseRemoteService {
list<StudentDTO.CourseDTO> queryAllCourse();
}
</code>

引用其他文件定义的thrift struct,可以使用include的关键字,然后 通过include的文件名和struct名来引用

  1. how to use container?
    <code>
    struct StudentDTO {
    1:i64 id,
    2:string name,
    3:STUDENT_SEXY sexy,
    4:list<CourseDTO> courses //使用容器
    }
    </code>
  2. how to name a package (namespace)?
    <code>namespace java com.xxxx.crm.demo.dto</code>
  3. how to control the version ?
  • Don’t change the numeric tags for any existing fields.
  • Any new fields that you add should be optional. This means that any messages serialized by code using your "old" message format can be parsed by your new generated code, as they won’t be missing any required elements. You should set up sensible default values for these elements so that new code can properly interact with messages generated by old code. Similarly, messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing. However, the unknown fields are not discarded, and if the message is later serialized, the unknown fields are serialized along with it — so if the message is passed on to new code, the new fields are still available.
  • Non-required fields can be removed, as long as the tag number is not used again in your updated message type (it may be better to rename the field instead, perhaps adding the prefix "OBSOLETE_", so that future users of your .thrift can’t accidentally reuse the number).
  • Changing a default value is generally OK, as long as you remember that default values are never sent over the wire. Thus, if a program receives a message in which a particular field isn’t set, the program will see the default value as it was defined in that program’s version of the protocol. It will NOT see the default value that was defined in the sender’s code.
numeric tags : <code>1:i64 id</code>中1的值

reference

  1. thrift-missing-guide
  2. Thrift: Scalable Cross-Language Services Implementation
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容