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

每种语言都有自己最擅长的领域,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包:

1go get git.apache.org/thrift.git/lib/go/thrift

2、产生协议库:

  这是我定义的测试用IDL,为检验兼容性,采用了多种数据结构:

01RpcService.thrift
02 
03namespace go demo.rpc
04namespace java demo.rpc
05 
06// 测试服务
07service RpcService {
08 
09    // 发起远程调用
10    list<string> funCall(1:i64 callTime, 2:string funCode, 3:map<string, string> paramMap),
11 
12}

    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、服务器端

下面,我们来写服务器端程序:

01package main
02 
03import (
04    "demo/rpc"
05    "fmt"
06    "git.apache.org/thrift.git/lib/go/thrift"
07    "os"
08)
09 
10const (
11    NetworkAddr = "127.0.0.1:19090"
12)
13 
14type RpcServiceImpl struct {
15}
16 
17func (this *RpcServiceImpl) FunCall(callTime int64, funCode string, paramMap map[string]string) (r []string, err error) {
18    fmt.Println("-->FunCall:", callTime, funCode, paramMap)
19 
20    for k, v := range paramMap {
21        r = append(r, k+v)
22    }
23    return
24}
25 
26func main() {
27    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
28    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
29    //protocolFactory := thrift.NewTCompactProtocolFactory()
30 
31    serverTransport, err := thrift.NewTServerSocket(NetworkAddr)
32    if err != nil {
33        fmt.Println("Error!", err)
34        os.Exit(1)
35    }
36 
37    handler := &RpcServiceImpl{}
38    processor := rpc.NewRpcServiceProcessor(handler)
39 
40    server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
41    fmt.Println("thrift server in", NetworkAddr)
42    server.Serve()
43}

  加空行也不过才43行,怎么样简单吧。

2、客户端程序

01package main
02 
03import (
04    "demo/rpc"
05    "fmt"
06    "git.apache.org/thrift.git/lib/go/thrift"
07    "net"
08    "os"
09    "time"
10)
11 
12func main() {
13    startTime := currentTimeMillis()
14    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
15    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
16 
17    transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "19090"))
18    if err != nil {
19        fmt.Fprintln(os.Stderr, "error resolving address:", err)
20        os.Exit(1)
21    }
22 
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)
27        os.Exit(1)
28    }
29    defer transport.Close()
30 
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)
37    }
38 
39    endTime := currentTimeMillis()
40    fmt.Println("Program exit. time->", endTime, startTime, (endTime - startTime))
41}
42 
43// 转换成毫秒
44func currentTimeMillis() int64 {
45    return time.Now().UnixNano() / 1000000
46}

  分别编译,先启动服务器端,然后在执行客户端程序。可以看到控制台正确打印出了信息,说明调用通过。

-->FunCall: 1380446325199 login map[name:qinerg passwd:123456]

三、Java版实现

  为了验证跨语言调用,下面我们分别再用java实现一下服务器端和客户端:

  生成Java版开发库:

  thrift-0.9.1.exe  -gen java RpcService.thrift

1、Java服务器版

01package demo.rpc;
02 
03import java.util.ArrayList;
04import java.util.List;
05import java.util.Map;
06import java.util.Map.Entry;
07 
08import org.apache.thrift.TException;
09import org.apache.thrift.protocol.TBinaryProtocol;
10import org.apache.thrift.protocol.TBinaryProtocol.Factory;
11import org.apache.thrift.server.TNonblockingServer;
12import org.apache.thrift.server.TServer;
13import org.apache.thrift.transport.TNonblockingServerSocket;
14import org.apache.thrift.transport.TNonblockingServerTransport;
15import org.apache.thrift.transport.TTransportException;
16 
17/**
18 * Thrift测试服务器
19 */
20public class Server implements RpcService.Iface {
21 
22    public static void main(String[] as) {
23        TNonblockingServerTransport serverTransport = null;
24        try {
25            serverTransport = new TNonblockingServerSocket(19090);
26        } catch (TTransportException e) {
27            e.printStackTrace();
28        }
29 
30        RpcService.Processor<RpcService.Iface> processor = new RpcService.Processor<RpcService.Iface>(
31                new Server());
32 
33        Factory protFactory = new TBinaryProtocol.Factory(true, true);
34        //TCompactProtocol.Factory protFactory = new TCompactProtocol.Factory();
35 
36        TNonblockingServer.Args args = new TNonblockingServer.Args(
37                serverTransport);
38        args.processor(processor);
39        args.protocolFactory(protFactory);
40        TServer server = new TNonblockingServer(args);
41        System.out.println("Start server on port 19090 ...");
42        server.serve();
43    }
44 
45    @Override
46    public List<String> funCall(long callTime, String funCode,
47            Map<String, String> paramMap) throws TException {
48        System.out.println("-->FunCall:" + callTime + " " + funCode + " "
49                + paramMap);
50        List<String> retList = new ArrayList<>();
51 
52        for (Entry<String, String> entry : paramMap.entrySet()) {
53            retList.add(entry.getKey() + entry.getValue());
54        }
55 
56        return retList;
57    }
58}

2、Java客户端版

01package demo.rpc;
02 
03import java.util.HashMap;
04import java.util.Map;
05 
06import org.apache.thrift.TException;
07import org.apache.thrift.protocol.TBinaryProtocol;
08import org.apache.thrift.transport.TFramedTransport;
09import org.apache.thrift.transport.TSocket;
10import org.apache.thrift.transport.TTransport;
11 
12/**
13 * Thrift测试客户端
14 */
15public class Client {
16 
17    public static void main(String[] args) {
18         
19        long startTime = System.currentTimeMillis();
20        try {
21            TTransport transport = new TFramedTransport(new TSocket("localhost", 19090));
22             
23            TBinaryProtocol protocol = new TBinaryProtocol(transport);
24            //TCompactProtocol protocol = new TCompactProtocol(transport);
25             
26            RpcService.Client client = new RpcService.Client(protocol);
27            transport.open();
28             
29            Map<String, String> param = new HashMap<String, String>();
30            param.put("name", "qinerg");
31            param.put("passwd", "123456");
32             
33            for (int i = 0; i < 1000; i++) {
34                System.out.println(client.funCall(System.currentTimeMillis() , "login", param));
35            }
36             
37            transport.close();
38        } catch (TException x) {
39            x.printStackTrace();
40        }
41        long endTime = System.currentTimeMillis();
42        System.out.println(" 本次调用完成时间:" + endTime + "   " + startTime + "  " + (endTime - startTime));
43    }
44}

  好了,现在启动java版服务器端,用golang版客户端调用,完全没有问题。启动golang版服务器端程序,用java版客户端调用,同样OK。

  完美实现了跨语言调用。

转载于:https://www.cnblogs.com/shihao/p/3347537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值