Golang通过Thrift框架完美实现跨语言调用
发布时间:2020-12-16 19:24:45 所属栏目:大数据 来源:网络整理
导读:目录[-] 一、开发前准备 1、安装golang的Thrift包: 2、产生协议库: 3、生成开发库 二、go语言实现 1、服务器端 2、客户端程序 三、Java版实现 1、Java服务器版 2、Java客户端版 每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序。 做为
目录[-] |
"net"
"os"
"time"
)
func main() {
startTime := currentTimeMillis()
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transport,err := thrift.NewTSocket(net.JoinHostPort(
"127.0.0.1"
"19090"
))
err != nil {
fmt.Fprintln(os.Stderr,monospace!important; border:0px!important; color:blue!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; background:none!important">"error resolving address:"
}
useTransport := transportFactory.GetTransport(transport)
client := rpc.NewRpcServiceClientFactory(useTransport,protocolFactory)
err := transport.Open(); err != nil {
"Error opening socket to 127.0.0.1:19090"
" "
}
defer transport.Close()
i := 0; i < 1000; i++ {
paramMap := make(map[string]string)
paramMap[
"name"
] =
"qinerg"
"passwd"
"123456"
r1,e1 := client.FunCall(currentTimeMillis(),monospace!important; border:0px!important; color:blue!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; background:none!important">"login"
endTime := currentTimeMillis()
"Program exit. time->"
// 转换成毫秒
func currentTimeMillis() int64 {
return
time
.Now().UnixNano() / 1000000
}
|
分别编译,先启动服务器端,然后在执行客户端程序。可以看到控制台正确打印出了信息,说明调用通过。
-->FunCall:1380446325199loginmap[name:qinergpasswd:123456]
三、Java版实现
为了验证跨语言调用,下面我们分别再用java实现一下服务器端和客户端:
生成Java版开发库:
thrift-0.9.1.exe -gen java RpcService.thrift
1、Java服务器版
package
demo.rpc;
import
java.util.ArrayList;
java.util.List;
java.util.Map;
java.util.Map.Entry;
org.apache.thrift.TException;
org.apache.thrift.protocol.TBinaryProtocol;
org.apache.thrift.protocol.TBinaryProtocol.Factory;
org.apache.thrift.server.TNonblockingServer;
org.apache.thrift.server.TServer;
org.apache.thrift.transport.TNonblockingServerSocket;
org.apache.thrift.transport.TNonblockingServerTransport;
org.apache.thrift.transport.TTransportException;
/**
* Thrift测试服务器
*/
public
class
Server
implements
RpcService.Iface {
static
void
main(String[] as) {
TNonblockingServerTransport serverTransport =
null
;
try
{
serverTransport =
new
TNonblockingServerSocket(
19090
);
}
catch
(TTransportException e) {
e.printStackTrace();
}
RpcService.Processor<RpcService.Iface> processor =
RpcService.Processor<RpcService.Iface>(
Server());
Factory protFactory =
TBinaryProtocol.Factory(
true
);
//TCompactProtocol.Factory protFactory = new TCompactProtocol.Factory();
TNonblockingServer.Args args =
TNonblockingServer.Args(
serverTransport);
args.processor(processor);
args.protocolFactory(protFactory);
TServer server =
TNonblockingServer(args);
System.out.println(
"Start server on port 19090 ..."
);
server.serve();
}
@Override
public
List<String> funCall(
long
callTime,String funCode,
Map<String,String> paramMap)
throws
TException {
"-->FunCall:"
+ callTime +
" "
+ funCode +
" "
+ paramMap);
List<String> retList =
ArrayList<>();
for
(Entry<String,String> entry : paramMap.entrySet()) {
retList.add(entry.getKey() + entry.getValue());
}
return
retList;
}
}
|
2、Java客户端版
java.util.HashMap;
java.util.Map;
org.apache.thrift.TException;
org.apache.thrift.protocol.TBinaryProtocol;
org.apache.thrift.transport.TFramedTransport;
org.apache.thrift.transport.TSocket;
org.apache.thrift.transport.TTransport;
/**
* Thrift测试客户端
*/
Client {
main(String[] args) {
startTime = System.currentTimeMillis();
{
TTransport transport =
TFramedTransport(
TSocket(
"localhost"
));
TBinaryProtocol protocol =
TBinaryProtocol(transport);
//TCompactProtocol protocol = new TCompactProtocol(transport);
RpcService.Client client =
RpcService.Client(protocol);
transport.open();
HashMap<String,String>();
param.put(
"name"
"qinerg"
);
"passwd"
"123456"
);
(
int
i =
0
; i <
1000
; i++) {
System.out.println(client.funCall(System.currentTimeMillis(),monospace!important; border:0px!important; color:blue!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; background:none!important">"login"
transport.close();
(TException x) {
x.printStackTrace();
}
endTime = System.currentTimeMillis();
" 本次调用完成时间:"
+ endTime +
+ startTime +
+ (endTime - startTime));
}
}
|
好了,现在启动java版服务器端,用golang版客户端调用,完全没有问题。启动golang版服务器端程序,用java版客户端调用,同样OK。
完美实现了跨语言调用。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!