每種語(yǔ)言都有自己最擅長(zhǎng)的領(lǐng)域,Golang 最適合的領(lǐng)域就是服務(wù)器端程序。
做為服務(wù)器端程序环葵,需要考慮性能同時(shí)也要考慮與各種語(yǔ)言之間方便的通訊。采用http協(xié)議簡(jiǎn)單宝冕,但性能不高张遭。采用TCP通訊,則需要考慮封包地梨、解包菊卷、粘包等等很多因素,而且想寫個(gè)高效的TCP服務(wù)宝剖,也很難洁闰。
其實(shí),對(duì)于此類需求万细,采用RPC(Remote Procedure Call Protocol)編程最靠譜扑眉。使用 RPC 編程被認(rèn)為是在分布式環(huán)境中運(yùn)行的客戶機(jī)和服務(wù)器應(yīng)用程序之間進(jìn)行可靠通信的最強(qiáng)大、最高效的方法之一赖钞。
Golang內(nèi)置了對(duì)RPC支持腰素,但只能適用于go語(yǔ)言程序之間調(diào)用,且貌似序列化仁烹、反序列化性能不高耸弄。如果go語(yǔ)言能使用Thrift開發(fā),那么就如虎添翼了卓缰〖瞥剩可惜砰诵,thrift雖然很早就包含了golang的代碼,但一直都存在各種問題無(wú)法正確執(zhí)行捌显,以至于GitHub上有許多大牛小牛自行實(shí)現(xiàn)的Thrift代碼茁彭,但依然各種問題……直到0.9.1版本的發(fā)布!
是的扶歪,最近理肺,Apache Thrift 0.9.1正式發(fā)布了。新版的Thrift終于對(duì)Golang提供了完美的支持善镰。經(jīng)過(guò)實(shí)驗(yàn)妹萨,服務(wù)器端、客戶端已經(jīng)完美支持跨語(yǔ)言調(diào)用炫欺,且性能乎完、尤其是內(nèi)存占用上,編譯型語(yǔ)言的特點(diǎn)展現(xiàn)出來(lái)品洛,比java版的實(shí)現(xiàn)強(qiáng)了很多树姨。
下面,我們采用golang實(shí)現(xiàn)一個(gè)Thrift的Server端和Client端程序桥状。
一帽揪、開發(fā)前準(zhǔn)備
1、安裝golang的Thrift包:
go get git.apache.org/thrift.git/lib/go/thrift
2辅斟、產(chǎn)生協(xié)議庫(kù):
這是我定義的測(cè)試用IDL转晰,為檢驗(yàn)兼容性,采用了多種數(shù)據(jù)結(jié)構(gòu):
RpcService.thrift
namespace go demo.rpc
namespace java demo.rpc
// 測(cè)試服務(wù)
service RpcService {
// 發(fā)起遠(yuǎn)程調(diào)用
list<string> funCall(1:i64 callTime, 2:string funCode, 3:map<string, string> paramMap),
}
3砾肺、生成開發(fā)庫(kù)
下載開發(fā)庫(kù)編譯器 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
自動(dòng)生成出的源碼結(jié)構(gòu)如下:
其中 constants.go挽霉、rpc_service.go、ttypes.go 是協(xié)議庫(kù)变汪,編寫程序需要用到。rpc_service-remote.go 是自動(dòng)生成的例程蚁趁,可以不用裙盾。
二、go語(yǔ)言實(shí)現(xiàn)
1他嫡、服務(wù)器端
下面番官,我們來(lái)寫服務(wù)器端程序:
package main
import (
"demo/rpc"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"os"
)
const (
NetworkAddr = "127.0.0.1:19090"
)
type RpcServiceImpl struct {
}
func (this *RpcServiceImpl) FunCall(callTime int64, funCode string, paramMap map[string]string) (r []string, err error) {
fmt.Println("-->FunCall:", callTime, funCode, paramMap)
for k, v := range paramMap {
r = append(r, k+v)
}
return
}
func main() {
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
//protocolFactory := thrift.NewTCompactProtocolFactory()
serverTransport, err := thrift.NewTServerSocket(NetworkAddr)
if err != nil {
fmt.Println("Error!", err)
os.Exit(1)
}
handler := &RpcServiceImpl{}
processor := rpc.NewRpcServiceProcessor(handler)
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
fmt.Println("thrift server in", NetworkAddr)
server.Serve()
}
加空行也不過(guò)才43行,怎么樣簡(jiǎn)單吧钢属。
2徘熔、客戶端程序
package main
import (
"demo/rpc"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"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"))
if err != nil {
fmt.Fprintln(os.Stderr, "error resolving address:", err)
os.Exit(1)
}
useTransport := transportFactory.GetTransport(transport)
client := rpc.NewRpcServiceClientFactory(useTransport, protocolFactory)
if err := transport.Open(); err != nil {
fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:19090", " ", err)
os.Exit(1)
}
defer transport.Close()
for i := 0; i < 1000; i++ {
paramMap := make(map[string]string)
paramMap["name"] = "qinerg"
paramMap["passwd"] = "123456"
r1, e1 := client.FunCall(currentTimeMillis(), "login", paramMap)
fmt.Println(i, "Call->", r1, e1)
}
endTime := currentTimeMillis()
fmt.Println("Program exit. time->", endTime, startTime, (endTime - startTime))
}
// 轉(zhuǎn)換成毫秒
func currentTimeMillis() int64 {
return time.Now().UnixNano() / 1000000
}
分別編譯,先啟動(dòng)服務(wù)器端淆党,然后在執(zhí)行客戶端程序酷师⊙攘梗可以看到控制臺(tái)正確打印出了信息,說(shuō)明調(diào)用通過(guò)山孔。
-->FunCall: 1380446325199 login map[name:qinerg passwd:123456]
三懂讯、Java版實(shí)現(xiàn)
為了驗(yàn)證跨語(yǔ)言調(diào)用,下面我們分別再用java實(shí)現(xiàn)一下服務(wù)器端和客戶端:
生成Java版開發(fā)庫(kù):
thrift-0.9.1.exe -gen java RpcService.thrift
1台颠、Java服務(wù)器版
package demo.rpc;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.thrift.TException;
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;
/**
* Thrift測(cè)試服務(wù)器
*/
public class Server implements RpcService.Iface {
public static void main(String[] as) {
TNonblockingServerTransport serverTransport = null;
try {
serverTransport = new TNonblockingServerSocket(19090);
} catch (TTransportException e) {
e.printStackTrace();
}
RpcService.Processor<RpcService.Iface> processor = new RpcService.Processor<RpcService.Iface>(
new Server());
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();
}
@Override
public List<String> funCall(long callTime, String funCode,
Map<String, String> paramMap) throws TException {
System.out.println("-->FunCall:" + callTime + " " + funCode + " "
+ paramMap);
List<String> retList = new ArrayList<>();
for (Entry<String, String> entry : paramMap.entrySet()) {
retList.add(entry.getKey() + entry.getValue());
}
return retList;
}
}
2褐望、Java客戶端版
package demo.rpc;
import java.util.HashMap;
import java.util.Map;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
/**
* Thrift測(cè)試客戶端
*/
public class Client {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
TTransport transport = new TFramedTransport(new TSocket("localhost", 19090));
TBinaryProtocol protocol = new TBinaryProtocol(transport);
//TCompactProtocol protocol = new TCompactProtocol(transport);
RpcService.Client client = new RpcService.Client(protocol);
transport.open();
Map<String, String> param = new HashMap<String, String>();
param.put("name", "qinerg");
param.put("passwd", "123456");
for (int i = 0; i < 1000; i++) {
System.out.println(client.funCall(System.currentTimeMillis() , "login", param));
}
transport.close();
} catch (TException x) {
x.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println(" 本次調(diào)用完成時(shí)間:" + endTime + " " + startTime + " " + (endTime - startTime));
}
}
好了,現(xiàn)在啟動(dòng)java版服務(wù)器端串前,用golang版客戶端調(diào)用瘫里,完全沒有問題。啟動(dòng)golang版服務(wù)器端程序荡碾,用java版客戶端調(diào)用谨读,同樣OK。
完美實(shí)現(xiàn)了跨語(yǔ)言調(diào)用玩荠。