There are many tutorials for installing the Thrift Framework under Windows. The difference in this article is that, without the help of Cygwin or MinGW, only VS2010, and thrift download the source files, install thrift and use.
Download these two files from the official website first:
· Thrift-0.9.1.tar.gz
· Thrift Compiler for Windows (Thrift-0.9.1.exe)
The first file is the source code package, and the second executable file is used to generate the post code for the target language under Windows.
In addition, boost libraries and libevent libraries are also required.
Installing Thrift0) Preparatory work
Thrift-0.9.1.tar.gz Source Package
Installing VS2010
Install the Boost library, I use the boost1.51 version
Install the Libevent library, the libevent-2.0.21-stable used here
1) Decompression thrift-0.9.1.tar.gz
Enter \thrift-0.9.1\lib\cpp,vs2010 Open Thrift.sln, there are LIBTHRIFT,LIBTHRIFTNB two works.
The difference between the two works is that the LIBTHRIFTNB project is a non-blocking (non-blocking) mode server, and the nonblocking mode relies on the Libevent library.
2) Libthrift project configuration:
Libthrift> Properties->c/c++-> General, additional include Directories->\boost\boost_1_51
Libthrift> Property, library Manager, general, additional library directory->\boost\boost_1_51\lib
3) LIBTHRIFTNB project configuration:
Libthriftnb> Properties->c/c++-> General, additional include directories
\boost\boost_1_51
\libevent-2.0.21-stable
\libevent-2.0.21-stable\include
\libevent-2.0.21-stable\win32-code
Libthriftnb> Property, library Manager, general--Add library directory,
\boost\boost_1_51\lib
4) Compiling libthrift and LIBTHRIFTNB engineering
After the compilation is complete, generate the Libthrift.lib file, and the Libthriftnb.lib file under \thrift-0.9.1\lib\cpp\debug.
Select release mode to generate the Libthrift.lib file and the Libthriftnb.lib file under \thrift-0.9.1\lib\cpp\release.
At this point, the installation is complete.
Development Steps
After installing the thrift, you can start developing. The development process is divided into several steps:
1th Step: Write the. thrift file, which is the interface profile (Interface Description file);
2nd step: Using Thrift Compiler for Windows (Thrift-0.9.1.exe), generate the target language code;
3rd Step: The server-side program introduces thrift generated code to implement RPC business code.
4th Step: The client introduces code to invoke the remote service.
The Blue Thrift.exe is the second file downloaded from the official website-"IDL translator", which helps you to "translate" the. thrift file into the RPC code of the target language.
Example
For this example, remote server provides a function. The client calls this function. The function of the remote function is simple, which is to output "Hello Thrift".
1) write the. Thrift file
Create a new text file Hello.txt, and then modify the extension Hello.thrift after saving the following content
1 service Hello {2 void func1 () 3 }
2) Generate target language code
The second file downloaded to the official website Thrift-0.9.1.exe and hello.thrift into a directory (hello).
Open the CMD command Line window, go to this directory, execute the command:
C:\users\admin\desktop\hello>thrift-0.9.1.exe--gen CPP Hello.thrift
Execute successfully, under the Hello directory, generate a Gen-cpp folder.
3) Create a project
Visual Studio 2010 Creates a new Win32 console application.
Project Name server
Solution Name Hello
Note: Select the empty item in the additional option.
Similarly, under the Hello solution, create a new empty project client.
4) Add files to the project
Add files to the server project.
Copy the files in the Gen-cpp folder to the server project and add them to the server project.
Add a file to the client project.
Copy the files from the Gen-cpp folder to the client project, delete the Hello_server.skeleton.cpp, and add the client.cpp files.
The file structure of the Final solution is this:
5) configuration Item Properties
Sever Engineering Server> Properties->c/c++-> General, additional include Directories->\boost\boost_1_51
Sever Engineering Server> Properties->c/c++-> General, additional include Directories->\THRIFT-0.9.1\LIB\CPP\SRC
Sever Engineering Server> Properties->c/c++-> General, additional include Directories->\thrift-0.9.1\lib\cpp\src\thrift
Sever engineering Server> Properties, connectors, additional library directories->\boost\boost_1_51\lib
Sever engineering Server> Properties, connectors, additional library directories->\thrift-0.9.1\lib\cpp\debug
The additional library directory points to the debug directory that was just compiled
Similarly, the client project also makes such a configuration.
Client Engineering client> Properties->c/c++-> General, additional include Directories->\boost\boost_1_51
Client Engineering client> Properties->c/c++-> General, additional include Directories->\THRIFT-0.9.1\LIB\CPP\SRC
Client Engineering client> Properties->c/c++-> General, additional include Directories->\thrift-0.9.1\lib\cpp\src\thrift
Client Engineering Client> Properties, connectors, additional library directories->\boost\boost_1_51\lib
Client Engineering Client> Properties, connectors, additional library directories->\thrift-0.9.1\lib\cpp\debug
6) Client code
The client.cpp file is empty and the code is added:
1#include <transport/TSocket.h>2#include"hello.h"3#include <protocol/TBinaryProtocol.h>4#include <server/TSimpleServer.h>5#include <transport/TServerSocket.h>6#include <transport/TBufferTransports.h>7#include <string>8 #pragmaComment (lib, "Libthrift.lib")9 using namespace:: Apache::thrift;Ten using namespace: : Apache::thrift::p rotocol; One using namespace:: Apache::thrift::transport; A using namespace:: apache::thrift::server; - - usingboost::shared_ptr; the - #pragmaComment (lib, "Libthrift.lib")//link library Files - - intMainintargcChar**argv) { + intPort =9090; -Shared_ptr<ttransport> socket (NewTsocket ("127.0.0.1",9090)); +Shared_ptr<ttransport> Transport (NewTbufferedtransport (socket)); AShared_ptr<tprotocol> Protocol (NewTbinaryprotocol (transport)); at helloclient Client (protocol); - Try{ -Transport->open (); - - client.func1 (); - inTransport->close (); -}Catch(texception&tx) { toprintf"error:%s\n", Tx.what ()); + } - GetChar (); the return 0; *}
7) Server code
The Hello_server.skeleton.cpp file already has thrift generated code, slightly modified, and eventually follows:
1 //This autogenerated skeleton file illustrates how to build a server.2 3 //You should copy it to another filename to avoid overwriting it.4 5 6 7#include"hello.h"8 9#include <thrift/protocol/TBinaryProtocol.h>Ten One#include <thrift/server/TSimpleServer.h> A -#include <thrift/transport/TServerSocket.h> - the#include <thrift/transport/TBufferTransports.h> - - #pragmaComment (lib, "Libthrift.lib") - + using namespace:: Apache::thrift; - + using namespace: : Apache::thrift::p rotocol; A at using namespace:: Apache::thrift::transport; - - using namespace:: apache::thrift::server; - - - in usingboost::shared_ptr; - to + - classHellohandler:Virtual PublicHelloif { the * Public: $ Panax Notoginseng Hellohandler () { - the //Your initialization goes here + A } the + - $ voidfunc1 () { $ - //Your implementation goes here - theprintf"Hello thrift\n"); - Wuyi } the - }; Wu - About $ intMainintargcChar**argv) { - - //-----------------------------// - WORD wversionrequested; A + wsadata wsadata; the - interr; $ theWversionrequested =makeword (2,2 ); the theErr = WSAStartup (wversionrequested, &wsadata); the //-------------------------------// - //make a note of the above code, which is a piece of code that relies on Windows in //By 2014.9.2 The stable version of 0.9.1, still need this code to be able to compile under windows. the //But if you use Git clone the latest version, this error has been fixed the //The latest version commented out this code, as can be compiled under Windows. About //remark time: 2014.9.2 the the the intPort =9090; + -Shared_ptrNewHellohandler ()); the BayiShared_ptr<tprocessor> Processor (NewHelloprocessor (handler)); the theShared_ptr<tservertransport> Servertransport (NewTserversocket (port)); - -Shared_ptr<ttransportfactory> Transportfactory (Newtbufferedtransportfactory ()); the theShared_ptr<tprotocolfactory> Protocolfactory (Newtbinaryprotocolfactory ()); the the - the Tsimpleserver Server (processor, Servertransport, Transportfactory, protocolfactory); the the Server.serve ();94 the return 0; the the}
8) Commissioning and Operation
Start the server project before you start the client project. Operation Result:
Summary
Here thrift installation and development of basic operating procedures are finished. Thrift's official documentation is not perfect, the installation method described in this article is not available on the online list of many tutorials, the main difference is not using Cygwin or mingw. It's helpful for students who want to use Visual Studio as a development environment.
The required files in the tutorial, can be obtained from the Internet, the core code in the text has been shown, I believe retrace will be able to successfully run the first thrift example.
Finally thank Chen Xiaosu (Beijing) Students, this tutorial is organized from his/her name of a folder. I got it from the thrift Exchange QQ group 193713524 share. Here are some additional picture descriptions and my understanding. The last example, simplified, is to directly and clearly describe the installation process and the configuration of the development.
Installation and development of Apache Thrift under Windows