每種語言都有本身最擅長的領域,Golang 最適合的領域就是服務器端程序。 java
作爲服務器端程序,須要考慮性能同時也要考慮與各類語言之間方便的通信。採用http協議簡單,但性能不高。採用TCP通信,則須要考慮封包、解包、粘包等等不少因素,並且想寫個高效的TCP服務,也很難。 git
其實,對於此類需求,採用RPC(Remote Procedure Call Protocol)編程最靠譜。使用 RPC 編程被認爲是在分佈式環境中運行的客戶機和服務器應用程序之間進行可靠通訊的最強大、最高效的方法之一。 golang
Golang內置了對RPC支持,但只能適用於go語言程序之間調用,且貌似序列化、反序列化性能不高。若是go語言能使用Thrift開發,那麼就如虎添翼了。惋惜,thrift雖然很早就包含了golang的代碼,但一直都存在各類問題沒法正確執行,以致於GitHub上有許多大牛小牛自行實現的Thrift代碼,但依然各類問題……直到0.9.1版本的發佈! apache
是的,最近,Apache Thrift 0.9.1正式發佈了。新版的Thrift終於對Golang提供了完美的支持。通過實驗,服務器端、客戶端已經完美支持跨語言調用,且性能、尤爲是內存佔用上,編譯型語言的特色展示出來,比java版的實現強了不少。 編程
下面,咱們採用golang實現一個Thrift的Server端和Client端程序。 服務器
go get git.apache.org/thrift.git/lib/go/thrift
RpcService.thrift namespace go demo.rpc namespace java demo.rpc // 測試服務 service RpcService { // 發起遠程調用 list<string> funCall(1:i64 callTime, 2:string funCode, 3:map<string, string> paramMap), }
下載開發庫編譯器 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 app
自動生成出的源碼結構以下: socket
其中 constants.go、rpc_service.go、ttypes.go 是協議庫,編寫程序須要用到。rpc_service-remote.go 是自動生成的例程,能夠不用。 分佈式
下面,咱們來寫服務器端程序:
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() }
加空行也不過才43行,怎麼樣簡單吧。
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)) } // 轉換成毫秒 func currentTimeMillis() int64 { return time.Now().UnixNano() / 1000000 }
分別編譯,先啓動服務器端,而後在執行客戶端程序。能夠看到控制檯正確打印出了信息,說明調用經過。
-->FunCall: 1380446325199 login map[name:qinerg passwd:123456]
爲了驗證跨語言調用,下面咱們分別再用java實現一下服務器端和客戶端:
生成Java版開發庫:
thrift-0.9.1.exe -gen java RpcService.thrift
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測試服務器 */ 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; } }
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測試客戶端 */ 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(" 本次調用完成時間:" + endTime + " " + startTime + " " + (endTime - startTime)); } }
好了,如今啓動java版服務器端,用golang版客戶端調用,徹底沒有問題。啓動golang版服務器端程序,用java版客戶端調用,一樣OK。
完美實現了跨語言調用。