每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序。
做为服务器端程序,需要考虑性能同时也要考虑与各种语言之间方便的通讯。采用http协议简单,但性能不高。采用TCP通讯,则需要考虑封包、解包、粘包等等很多因素,而且想写个高效的TCP服务,也很难。
其实,对于此类需求,采用RPC(Remote Procedure Call Protocol)编程最靠谱。使用 RPC 编程被认为是在分布式环境中运行的客户机和服务器应用程序之间进行可靠通信的最强大、最高效的方法之一。
Golang内置了对RPC支持,但只能适用于go语言程序之间调用。如果go语言能使用Thrift开发,那么就如虎添翼了。可惜,thrift虽然很早就提供了golang的代码,但一直都存在各种问题无法正确执行……直到0.9.1版本的发布!
最近,Apache Thrift 0.9.1正式发布了。新版的Thrift终于对Golang提供了完美的支持。经过实验,服务器端、客户端已经完美支持跨语言调用,且性能、尤其是内存占用上,编译型语言的特点展现出来,比java版的实现强了很多。
下面,我们采用golang实现一个Thrift的Server端和Client端程序。
一、开发前准备
1、安装golang的Thrift包:
1 | go get git.apache.org/thrift.git/lib/go/thrift |
2、产生协议库:
这是我定义的测试用IDL,为检验兼容性,采用了多种数据结构:
04 | namespace java demo.rpc |
10 | list<string> funCall(1:i64 callTime, 2:string funCode, 3:map<string, string> paramMap), |
3、生成开发库
下载开发库编译器 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.1/thrift-0.9.1.exe
thrift-0.9.1.exe -gen go RpcService.thrift
自动生成出的源码结构如下:

其中 constants.go、rpc_service.go、ttypes.go 是协议库,编写程序需要用到。rpc_service-remote.go 是自动生成的例程,可以不用。
二、go语言实现
1、服务器端
下面,我们来写服务器端程序:
06 | "git.apache.org/thrift.git/lib/go/thrift" |
11 | NetworkAddr = "127.0.0.1:19090" |
14 | type RpcServiceImpl struct { |
17 | func ( this *RpcServiceImpl) FunCall(callTime int64, funCode string, paramMap map[string]string) (r []string, err error) { |
18 | fmt.Println( "-->FunCall:" , callTime, funCode, paramMap) |
20 | for k, v := range paramMap { |
27 | transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()) |
28 | protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() |
31 | serverTransport, err := thrift.NewTServerSocket(NetworkAddr) |
33 | fmt.Println( "Error!" , err) |
37 | handler := &RpcServiceImpl{} |
38 | processor := rpc.NewRpcServiceProcessor(handler) |
40 | server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) |
41 | fmt.Println( "thrift server in" , NetworkAddr) |
加空行也不过才43行,怎么样简单吧。
2、客户端程序
06 | "git.apache.org/thrift.git/lib/go/thrift" |
13 | startTime := currentTimeMillis() |
14 | transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()) |
15 | protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() |
17 | transport, err := thrift.NewTSocket(net.JoinHostPort( "127.0.0.1" , "19090" )) |
19 | fmt.Fprintln(os.Stderr, "error resolving address:" , err) |
23 | useTransport := transportFactory.GetTransport(transport) |
24 | client := rpc.NewRpcServiceClientFactory(useTransport, protocolFactory) |
25 | if err := transport.Open(); err != nil { |
26 | fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:19090" , " " , err) |
29 | defer transport.Close() |
31 | for i := 0; i < 1000; i++ { |
32 | paramMap := make(map[string]string) |
33 | paramMap[ "name" ] = "qinerg" |
34 | paramMap[ "passwd" ] = "123456" |
35 | r1, e1 := client.FunCall(currentTimeMillis(), "login" , paramMap) |
36 | fmt.Println(i, "Call->" , r1, e1) |
39 | endTime := currentTimeMillis() |
40 | fmt.Println( "Program exit. time->" , endTime, startTime, (endTime - startTime)) |
44 | func currentTimeMillis() int64 { |
45 | return time .Now().UnixNano() / 1000000 |
分别编译,先启动服务器端,然后在执行客户端程序。可以看到控制台正确打印出了信息,说明调用通过。
-->FunCall: 1380446325199 login map[name:qinerg passwd:123456]
三、Java版实现
为了验证跨语言调用,下面我们分别再用java实现一下服务器端和客户端:
生成Java版开发库:
thrift-0.9.1.exe -gen java RpcService.thrift
1、Java服务器版
03 | import java.util.ArrayList; |
06 | import java.util.Map.Entry; |
08 | import org.apache.thrift.TException; |
09 | import org.apache.thrift.protocol.TBinaryProtocol; |
10 | import org.apache.thrift.protocol.TBinaryProtocol.Factory; |
11 | import org.apache.thrift.server.TNonblockingServer; |
12 | import org.apache.thrift.server.TServer; |
13 | import org.apache.thrift.transport.TNonblockingServerSocket; |
14 | import org.apache.thrift.transport.TNonblockingServerTransport; |
15 | import org.apache.thrift.transport.TTransportException; |
20 | public class Server implements RpcService.Iface { |
22 | public static void main(String[] as) { |
23 | TNonblockingServerTransport serverTransport = null ; |
25 | serverTransport = new TNonblockingServerSocket( 19090 ); |
26 | } catch (TTransportException e) { |
30 | RpcService.Processor<RpcService.Iface> processor = new RpcService.Processor<RpcService.Iface>( |
33 | Factory protFactory = new TBinaryProtocol.Factory( true , true ); |
36 | TNonblockingServer.Args args = new TNonblockingServer.Args( |
38 | args.processor(processor); |
39 | args.protocolFactory(protFactory); |
40 | TServer server = new TNonblockingServer(args); |
41 | System.out.println( "Start server on port 19090 ..." ); |
46 | public List<String> funCall( long callTime, String funCode, |
47 | Map<String, String> paramMap) throws TException { |
48 | System.out.println( "-->FunCall:" + callTime + " " + funCode + " " |
50 | List<String> retList = new ArrayList<>(); |
52 | for (Entry<String, String> entry : paramMap.entrySet()) { |
53 | retList.add(entry.getKey() + entry.getValue()); |
2、Java客户端版
03 | import java.util.HashMap; |
06 | import org.apache.thrift.TException; |
07 | import org.apache.thrift.protocol.TBinaryProtocol; |
08 | import org.apache.thrift.transport.TFramedTransport; |
09 | import org.apache.thrift.transport.TSocket; |
10 | import org.apache.thrift.transport.TTransport; |
17 | public static void main(String[] args) { |
19 | long startTime = System.currentTimeMillis(); |
21 | TTransport transport = new TFramedTransport( new TSocket( "localhost" , 19090 )); |
23 | TBinaryProtocol protocol = new TBinaryProtocol(transport); |
26 | RpcService.Client client = new RpcService.Client(protocol); |
29 | Map<String, String> param = new HashMap<String, String>(); |
30 | param.put( "name" , "qinerg" ); |
31 | param.put( "passwd" , "123456" ); |
33 | for ( int i = 0 ; i < 1000 ; i++) { |
34 | System.out.println(client.funCall(System.currentTimeMillis() , "login" , param)); |
38 | } catch (TException x) { |
41 | long endTime = System.currentTimeMillis(); |
42 | System.out.println( " 本次调用完成时间:" + endTime + " " + startTime + " " + (endTime - startTime)); |
好了,现在启动java版服务器端,用golang版客户端调用,完全没有问题。启动golang版服务器端程序,用java版客户端调用,同样OK。
完美实现了跨语言调用。