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. Python - 文档格式转换(CSV与JSON)

  2. 判断1~n有多少个1

    判断1~n有多少个1 例如:1~12中 1 2 3 4 5 6 7 8 9 10 11 12 有1,10,11,12中含有1,则共有5个1: 算法如下: #include<stdio.h> ...

  3. Linux 0.11源码阅读笔记-总结

    总结 Linux 0.11主要包含文件管理和进程管理两个部分.进程管理包括内存管理.进程管理.进程间通信模块.文件管理包含磁盘文件系统,打开文件内存数据.磁盘文件系统包括空闲磁盘块管理,文件数据块的管 ...

  4. Python简单文件读写

    ''' 用文件存储账户信息 使用列表存储多个账户信息,每个账户为一个字典对象 ''' users=[] #创建一个空列表 users.append({'id':'admin','pwd':'1235@ ...

  5. Java中最早期的集合Vector

    1.Vector类可以实现可增长的对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作. 2 ...

  6. C++篇:第四章_函数_知识点大全

    C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 五.函数 (一)函数使用规则 函数的定义不能嵌套但调用可以嵌套 在函数调用时,如 ...

  7. 4.Docker容器学习之Dockerfile入门到放弃

    原文地址: 点击直达 0x01 Dockerfile 编写 描述:Dockerfile是一个文本格式的配置文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内 ...

  8. iperf/LANSpeedTest网络传输速度测试工具

    最近公司测试限速,搜集软件发现两款,iperf,LANSpeedTest. iperf,多平台. LANSpeedTest,读写显示,操作简单. 局域网测试传输,优先考虑UDP. iperf Iper ...

  9. 【分享汇总】25个主题分享,360°领略OpenHarmony最新技术版图

    2021年10月,开放原子开源基金会旗下开源项目 OpenAtom OpenHarmony (以下简称"OpenHarmony") 应邀参加华为2021 HDC 开发者大会,并组织 ...

  10. vue3 监听路由($route)变化

      setup() {      // ...   },   watch: {     $route(m, n) {       console.log('mm', m)       console. ...