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 版本,可正常相互调用。不用担心旧服务不兼容的问题。

最新文章

  1. [django]表格的添加与删除实例(可以借鉴参考)
  2. Redis教程(十三):管线详解
  3. 常用的一些SQL语句整理,也许有你想要的。
  4. 高性能MySQL笔记:第1章 MySQL架构
  5. C#中WinForm程序退出方法技巧总结(转)
  6. Markdown 语法和 MWeb 写作使用说明
  7. basic use of sidekiq
  8. php 依赖注入
  9. Duilib中Webbrowser事件完善使其支持判断页面加载完毕
  10. EL表达式介绍
  11. suse linux中apache+php服务器安装
  12. jquery.ajax中的ifModified参数的误解
  13. vs git .ignore
  14. HTML标签与表格
  15. Socket的粘包处理
  16. 关于 Rijndael 加密
  17. Asp.Net Core微服务再体验
  18. Java_Runtime&amp;Process&amp;ProcessBuilder
  19. Linux之初识磁盘
  20. A^B Mod C

热门文章

  1. Java 中,DOM 和 SAX 解析器有什么不同?
  2. 什么是 Spring beans?
  3. HMS Core定位服务在生活服务类App中可以自动填写收货地址啦
  4. java弹框
  5. ajax解析json对象集合
  6. Numpy使用Matplotlib实现可视化绘图
  7. 从零开始开发一款H5小游戏(三) 攻守阵营,赋予粒子新的生命
  8. PAT A1035 Password
  9. Linux环境下Eclipse中快捷键不起作用
  10. Hadoop真分布式实现SSH免密登录