thrift 从 0.9.1版本开始,可以完美支持 go 语言,可以完美的实现跨语言的 rpc 调用了。下面以 go 和 java 语言相互调用为例。

  • 编辑协议文件,go 语言示例
/** example.thrift */
namespace go example service transdata {
bool sendMsg(1: string msgJson),
}
  • 下载thrift,用于生成协议库文件

下载地址 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.11.0/thrift-0.11.0.exe

生成库文件 thrift.0.11.0.exe -r --gen go example.thrift

  • go 语言客户端和服务端示例代码
/** thrift_example.go */
package thrift
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"os"
"context"
"log"
"net"
"awesome-go/src/service/thrift/gen-go/example"
)
const (
HOST = "localhost"
PORT = "19090"
) type TransdataImpl struct {
} func (trandata *TransdataImpl) SendMsg(ctx context.Context, msgJson string) (r bool, err error){
fmt.Println("-->SendMsg Call:", msgJson)
return true, nil
} func Server() {
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
//protocolFactory := thrift.NewTCompactProtocolFactory() serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort(HOST, PORT))
if err != nil {
fmt.Println("Error!", err)
os.Exit(1)
} handler := &TransdataImpl{}
processor := example.NewTransdataProcessor(handler) server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
fmt.Println("thrift server in", net.JoinHostPort(HOST, PORT))
server.Serve()
} func Client() {
tSocket, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))
if err != nil {
log.Fatalln("tSocket error:", err)
}
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
transport, _ := transportFactory.GetTransport(tSocket)
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() client := example.NewTransdataClientFactory(transport, protocolFactory) if err := transport.Open(); err != nil {
log.Fatalln("Error opening:", HOST + ":" + PORT)
}
defer transport.Close() d, err := client.SendMsg(nil,"test string")
fmt.Println(d)
}
  • go 语言测试代码
/** thrift_example_test.go */
package thrift import "testing" func ClientTest(t *testing.T) {
Client()
} func ServerTest(t testing.T) {
Server()
}
  • java 协议文件示例
namespace go service.thrift

service Transdata {
bool sendMsg(1: string msgJson),
}
  • 生成java 协议库文件

生成库文件 thrift.0.11.0.exe -r --gen java example.thrift

  • java 服务端示例
package service.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransportException; /**
* @author zhengqian
*/
public class Server { public static void StartSimpleServer(Transdata.Processor<ExampleHandler> processor) {
TNonblockingServerTransport serverTransport = null;
try {
serverTransport = new TNonblockingServerSocket(19090);
} catch (TTransportException e) {
e.printStackTrace();
} Factory protFactory = new TBinaryProtocol.Factory(true, true);
//TCompactProtocol.Factory protFactory = new TCompactProtocol.Factory(); TNonblockingServer.Args args = new TNonblockingServer.Args(
serverTransport);
args.processor(processor);
args.protocolFactory(protFactory);
TServer server = new TNonblockingServer(args);
System.out.println("Start server on port 19090 ...");
server.serve();
} public static void main(String[] args) {
StartSimpleServer(new Transdata.Processor<ExampleHandler>(new ExampleHandler()));
}
}
  • java 客户端代码示例
package service.thrift;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException; /**
* @author zhengqian
*/
public class Client {
public static void main(String[] args) { try {
TTransport transport = new TFramedTransport(new TSocket("localhost", 19090));
TProtocol protocol = new TBinaryProtocol(transport);
Transdata.Client client = new Transdata.Client(protocol); transport.open();
System.out.println(client.sendMsg("test string"));
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException x) {
x.printStackTrace();
}
}
}
  • 两个语言版本的 server / client 随意组合,都可以相互调用。
  • 补充,如果两端使用的 thrift 版本不一致,也可以调用,测试一端使用 0.9.3 版本,一端使用 0.11.0 版本,可正常相互调用。不用担心旧服务不兼容的问题。

go thrift 开发的更多相关文章

  1. windows配置thrift开发环境

    1)安装thrift:到thrift官网下载exe文件,然后将文件重命名为thrift.exe,拷贝到c:\windows目录下(或者任何目录下),然后就可以在dos环境下使用了 c:\windows ...

  2. erlang+thrift配合开发

    I  think, thrift is a  tcp/ip based Client-Server architecture multi-languages supported RPC framewo ...

  3. 和 Thrift 的一场美丽邂逅

    一. 与 Thrift 的初识 也许大多数人接触 Thrift 是从序列化开始的.每次搜索 “java序列化” + “方式”.“对比” 或 “性能” 等关键字时,搜索引擎总是会返回一大堆有关各种序列化 ...

  4. Golang通过Thrift框架完美实现跨语言调用

    每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序. 做为服务器端程序,需要考虑性能同时也要考虑与各种语言之间方便的通讯.采用http协议简单,但性能不高.采用TCP通讯,则需要 ...

  5. thrift学习笔记

    Thrift学习笔记 一:thrift介绍 Thrift是facebook开发的用来处理各不同系统之间数据通讯的rpc服务框架,后来成为apche的开源项目.thrift支持多种程序语言,包括Java ...

  6. RPC框架Thrift例子-PHP调用C++后端程序

    更新 2016-02-22: Response对象不用主动创建. 前言 前段时间用了一下Facebook的开源RPC框架Thrift,做PHP客户端调用C++后端程序,真心觉得Thrift不错! 本文 ...

  7. 在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused

    在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused [原创]在RHEL上安装Thrift(支持C++)的若干问题    2010年12月1 ...

  8. Thrift学习

    Thrift学习 一:thrift介绍 Thrift是facebook开发的用来处理各不同系统之间数据通讯的rpc服务框架,后来成为apche的开源项目.thrift支持多种程序语言,包括Java,P ...

  9. Thrift教程初级篇——thrift安装环境变量配置第一个实例

    前言: 因为项目需要跨语言,c++客户端,web服务端,远程调用等需求,所以用到了RPC框架Thrift,刚开始有点虚,第一次接触RPC框架,后来没想到Thrift开发方便上手快,而且性能和稳定性也不 ...

随机推荐

  1. Zookeeper 下 Server 工作状态 ?

    服务器具有四种状态,分别是 LOOKING.FOLLOWING.LEADING.OBSERVING. 1.LOOKING:寻找 Leader 状态.当服务器处于该状态时,它会认为当前集群中 没有 Le ...

  2. Collection 和 Collections 的区别?

    Collection 是一个接口,它是 Set.List 等容器的父接口:Collections 是个一个 工具类,提供了一系列的静态方法来辅助容器操作,这些方法包括对容器的搜索. 排序.线程安全化等 ...

  3. 解释 Java 堆空间及 GC?

    当通过 Java 命令启动 Java 进程的时候,会为它分配内存.内存的一部分用于 创建堆空间,当程序中创建对象的时候,就从对空间中分配内存.GC 是 JVM 内 部的一个进程,回收无效对象的内存用于 ...

  4. Nuxt.js的踩坑指南(常见问题汇总)

    本文会不定期更新在nuxt.js中遇到的问题进行汇总.转发请注明出处,尊重作者,谢谢! 强烈推荐作者文档版踩坑指南,点击跳转踩坑指南 在Nuxt的官方文档中,中文文档和英文文档都存在着不小的差异. 1 ...

  5. 开源HTML5游戏引擎Kiwi.js 1.0正式发布

    Kiwi.js是由GameLab开发的一款全新的开源HTML5 JavaScript游戏引擎.在经过一年多的开发和测试之后,终于在日前正式发布了Kiwi.js 1.0版本. 其创始人Dan Milwa ...

  6. 夏日葵电商:连锁零售店小程序o2o系统解决方案

    公众平台"附近小程序"功能上线后,一个主体账号可以同时绑定N+个门店,这对连锁零售店铺来说是重磅福利呀,无论你是通过搜索还是线下扫码进入小程序,线上与线下都完全贯通了,线上多种入口 ...

  7. c++类调用的一个小问题

    先看这两段代码: #include <iostream> #include <vector> #include <algorithm> using namespac ...

  8. 微信小程序列表拖动排序Demo

    wxml页面编写 <view class="container"> <view bindtap="box" class="box&q ...

  9. An=n的前n项和的前n项和

    #include<iostream> using namespace std; int main() { int n,a=0,b=0; cin>>n; for(int i=1; ...

  10. HttpRequest.Path与HttpRequest.PathBase区别

    源自stackoverflow