Using thrift as Go and C + + intermediate RPC and issues (i)

Source: Internet
Author: User

These two days because of work need to check some cross-language RPC interface. I am referring to the inclusion of serialization and TCP interactions.

Check out the following three main categories:

1.ICE this thing does not support go, give up directly.

2.GRPC, this thing just released, but also full use of the c++11 features, our production environment has not yet upgraded, he relies on Protobuf 3.0.0, this is a beta version, take over the compiled, rely on Google's gmock. My virtual machine can't even go up, only temporarily.

3.Thrift, this from 0.9.1 on the perfect (official) Support Go language, then take this fencing.

Note: The C + + test environment Ubuntu12.04lts Kernel 3.2.0-23 gcc version 4.6.3 Go is running on the Win7 notebook.

First thing to install thrift, download the latest thrift0.9.2 version from the official website.

Compile and install reference http://my.oschina.net/zc741520/blog/399049

Libevent Library I used it a while ago. No installation is necessary, the version used is 2.0.22-stable

Go version thrift from this address

Git-wip-us.apache.org/repos/asf/thrift.git/lib/go/thrift, get it.
Git.apache.org/thrift.git/lib/go/thrift always expires.

The thrift file is as follows:

Namespace CPP hellowordnamespace go hellowordservice test{string hello (1:string data);}

I wrote the world wrong ... =_=!

Thrift--gen CPP Helloword.thrift

Thrift--gen Go Helloword.thrift

Generated C + + file is a problem, go inside to modify the next Test.go and contants.go the location of the thrift Library

Import ("Helloword" "FMT" "Git-wip-us.apache.org/repos/asf/thrift.git/lib/go/thrift" "OS" "NET")

The following starts the C + + code.

C + + server-side code

1#include <thrift/concurrency/ThreadManager.h>2#include <thrift/concurrency/PosixThreadFactory.h>3#include <thrift/protocol/TBinaryProtocol.h>4#include <thrift/server/TSimpleServer.h>5#include <thrift/server/TThreadPoolServer.h>6#include <thrift/server/TThreadedServer.h>7#include <thrift/transport/THttpServer.h>8#include <thrift/transport/TServerSocket.h>9#include <thrift/concurrency/ThreadManager.h>Ten#include <thrift/concurrency/PosixThreadFactory.h> One#include <thrift/protocol/TBinaryProtocol.h> A#include <thrift/server/TSimpleServer.h> -#include <thrift/server/TThreadPoolServer.h> -#include <thrift/server/TThreadedServer.h> the#include <thrift/transport/THttpServer.h> -#include <thrift/transport/TServerSocket.h> -#include <thrift/transport/TTransportUtils.h> -#include <thrift/TToString.h> +#include <iostream> -#include <string> +#include"Test.h" A#include <boost/bind.hpp> at#include <boost/function.hpp> -#include <thrift/protocol/TBinaryProtocol.h> -#include <thrift/transport/TSocket.h> -#include <thrift/transport/TTransportUtils.h> - using namespacestd; - using namespaceApache::thrift; in using namespaceapache::thrift::concurrency; - using namespaceapache::thrift::p rotocol; to using namespaceApache::thrift::transport; + using namespaceApache::thrift::server; - using namespace:: Apache::thrift; the using namespace: : Apache::thrift::p rotocol; * using namespace:: Apache::thrift::transport; $ using namespace:: Boost;Panax Notoginseng using namespace:: Helloword; - using:: Boost::bind; the  + classTesthandler:Virtual  PublicTestif { A   Public: the Testhandler () {} +  -   voidHello (std::string& _return,ConstSTD::string&data) { $ //if (data== "ping") $printf"[%d] recv ping\n", (int) Time (NULL)); -_return ="Pong"; -printf"[%d] send pong\n", (int) Time (NULL)); the }}; - Wuyi intMainintargcChar**argv) { the   intPort =9000; -Shared_ptr<testhandler> Handler (NewTesthandler ()); WuShared_ptr<tprocessor> Processor (NewTestprocessor (handler)); -Shared_ptr<tservertransport> Servertransport (NewTserversocket (port)); AboutShared_ptr<ttransportfactory> Transportfactory (Newttransportfactory ()); $Shared_ptr<tprotocolfactory> Protocolfactory (Newtbinaryprotocolfactory ()); - Tsimpleserver Server (processor, Servertransport, Transportfactory, protocolfactory); - Server.serve (); -   return 0; A}

C + + client-side code

1#include <iostream>2#include <string>3#include <thrift/protocol/TBinaryProtocol.h>4#include <thrift/transport/TSocket.h>5#include <thrift/transport/TTransportUtils.h>6#include"Test.h"7 using namespacestd;8 using namespaceApache::thrift;9 using namespaceapache::thrift::p rotocol;Ten using namespaceApache::thrift::transport; One using namespaceHelloword; A intMainintargcChar**argv) { -         stringServer ="127.0.0.1"; -         if(argc>1) server= argv[1]; theBoost::shared_ptr<ttransport> socket (NewTsocket (Server,9000)); -Boost::shared_ptr<ttransport> Transport (NewTbufferedtransport (socket)); -Boost::shared_ptr<tprotocol> Protocol (NewTbinaryprotocol (transport)); - testclient Client (protocol); + Try { -Transport->open (); +         stringcao="haha"; A Client.hello (Cao,cao); atcout <<"Hello"<< Cao <<Endl; - } - Catch(Apache::thrift::tapplicationexception &e) - { -Cout<<e.what () <<Endl; - } in         return 0; -}

Compiling the server side

g++-o server server.cpp Test.cpp helloword_constants.cpp helloword_types.cpp-lthrift-lboost_system

Compiling client Side

g++-o client client.cpp Test.cpp helloword_constants.cpp Helloword_types.cpp-lthrift-lboost_system

There is no problem testing C + +.


Write the code for GO:

GO Server Side

Package Mainimport ("Helloword" "FMT" "Git-wip-us.apache.org/repos/asf/thrift.git/lib/go/thrift" "OS") const ( NETWORKADDR = "192.168.20.76:9000") type Test Struct{}func (this *test) Hello (data string) (R string, err error) {  //Note this , my thrift code is written in the interface when the lowercase hello in test.go is the FMT into uppercase. Fmt. Println ("rec", data) R = data+ "haha" fmt. PRINTLN ("Send", R) Return}func main () {transportfactory: = thrift. Newttransportfactory ()    protocolfactory: = Thrift. Newtbinaryprotocolfactorydefault ()    //protocolfactory: = Thrift. Newtcompactprotocolfactory ()     servertransport, err: = Thrift. Newtserversocket (NETWORKADDR)    if err! = Nil {        FMT. Println ("error!", err)        OS. Exit (1)    }     handler: = &test{}    Processor: = Helloword. Newtestprocessor (handler)     server: = Thrift. NewTSimpleServer4 (processor, Servertransport, Transportfactory, protocolfactory)    FMT. PRINTLN ("Thrift Server In", NETWORKADDR)    server. Serve ()}

GO Client Code

Package Mainimport ("Helloword" "FMT" "Git-wip-us.apache.org/repos/asf/thrift.git/lib/go/thrift" "OS" "NET") Func main () {transportfactory: = thrift. Newttransportfactory () Protocolfactory: = Thrift. Newtbinaryprotocolfactorydefault () transport, err: = Thrift. Newtsocket (NET. Joinhostport ("127.0.0.1", "9000")) if err! = Nil {        FMT. Fprintln (OS. Stderr, "Error resolving Address:", err)        OS. Exit (1)    } usetransport: = Transportfactory.gettransport (transport) Client: = Helloword. Newtestclientfactory (Usetransport, protocolfactory) If err: = transport. Open (); Err! = Nil {        FMT. Fprintln (OS. Stderr, "Error opening socket to server", "", err)        OS. Exit (1)    }    defer transport. Close () for I: = 0; I < 2; i++ {        r1, e1: = client. Hello ("Hello")        fmt. Println (i, "call->", R1, E1)    }}

Of course the test is no problem.

Here is the cross-access.

Of course, based on the above code is also no problem, haha haha, because the problem was I k ah. Attached.

C + + client side and GO server side

GO Client VS C + + server also no problem I can't.

The following article discusses the problems and analysis.

Using thrift as Go and C + + intermediate RPC and issues (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.