thrift

作者: ShutLove | 来源:发表于2017-12-22 17:31 被阅读0次

一、mac下安装
brew update
brew install thrift
执行thrift -version,显示thrift版本,则安装成功
执行thrift -help,会看到帮助文档

二、idl定义
主要关注namespace、struct结构、支持的数据类型以及service。
具体可参考https://my.oschina.net/helight/blog/195015
例子, 文件Cityinfo.thrift:

namespace go city.genCode
struct Cityinfo {
///< 城市id
1: required i32 cityid;
///< 城市名称
2: required string city_desc;
///< 区域id
3: required i32 countyid;
///< 区域名称
4: required string county_desc;
///< 国家id
5: optional i32 countryid;
///< 国家名称
6: optional string country_desc;
}
service CityService {
///< 批量根据uid查cid 最多支持50个
CityInfo GetCityInfo(1:i64 cityId);
}

namespace中go指定生成go代码,cityCell.genCode指定go的package。

三、生成代码
thrift -out dir(生成代码的存放路径) -r --gen go idl(定义idl文件路径)
-out参数可以把生成的代码放到指定的目录下,否则默认是在当前目录新建一个gen-code目录,然后按namespace在gen-code目录下生成目录和代码。

如用上面的idl,生成代码命令的例子:
thrift -out /usr/gopath/src -r --gen go ./idl/Cityinfo.thrift
生成代码路径是:/usr/gopath/src/city/genCode/Cityinfo.go,genCode下还有其他一些文件和目录。

四、客户端调用

  1. 首先需要声明并初始化transportFactory、protocolFactory和transport:
    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
    tSocket, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))
    transport, err := transportFactory.GetTransport(tSocket)
    HOST和PORT是服务端地址和端口号。

  2. 声明并初始化client
    iprot := protocolFactory.GetProtocol(transport)
    oprot := protocolFactory.GetProtocol(transport)
    client := genCode.NewCityServiceClient(thrift.NewTStandardClient(iprot, oprot))

  3. 调用服务
    cityId := 10000
    defaultCtx := context.Background()
    resp, err := client.GetCityInfo(defaultCtx, cityId)

相关文章

  • Thirft

    一、About thrift二、什么是thrift,怎么工作?三、Thrift IDL四、Thrift D...

  • Docker&k8s微服务学习实践(二)

    一、下载配置thrift 从Thrift官网 http://thrift.apache.org/ 下载thrift...

  • thrift 指南

    /usr/local/opt/thrift@0.9/bin/thrift -gen java a.thrift会在...

  • Thrift学习

    Thrift源码剖析 Thrift源码分析及一个完整的例子 CSDN Thrift源码分析 Thrift二进制序列...

  • thrift 简介

    thrift 基本概念、数据类型thrift 简介一 thrift 基本类概述、序列化协议thrift 简介二 t...

  • Thrift 异步Service的原理

    Thrift 异步Service的原理 定义一个Thrift Service Thrift Service方法会提...

  • C# 通过Thrift访问Hbase

    1、确保Hbase中已经开启Thrift服务2、Thrift官网( http://thrift.apache.or...

  • 聊一聊序列化-Thrift

    认识Thrift Thrift is an interface definition language and b...

  • Thrift入门

    原文链接:thrift入门 转载请注明出处~ Thrift简介 什么是thrift 简单来说,是Facebook公...

  • python操作hbase

    安装Thrift 安装Thrift的具体操作,请点击链接 pip install thrift安装happybas...

网友评论

    本文标题:thrift

    本文链接:https://www.haomeiwen.com/subject/ffntgxtx.html