first, the development environment
- 1, there is a deployment of the thrift framework of the computer, I use the CENTOS5 under the thrift framework
(The deployment step can be see http://blog.csdn.net/san1156/article/details/41146483)
- 2. The deployed thrift environment needs to support Java and C + +
second, sample function
- Using C + + as server, Java client, client remote Call service
third, the development process
- 1. Create the interface IDL file with the suffix named. Thrift
File name: strreversed.thriftstruct strinfo { 1:string str,}service strscheduler{ bool Addstr (1:strinfo str), Strinfo getstr ()}
- 2. Compile and generate the corresponding language header file
Compile statement: Thrift-gen CPP Strreversed.thriftthrift-gen java strreversed.thrift
- 3, after compiling, the folder under this directory, similar to Gen-cpp, Gen-java, gen-py,cd go in, you can see the corresponding language header file
Gen-cpp file list:-rw-r--r--1 root root 281 Apr 9 15:05 strreversed_constants.cpp-rw-r--r--1 root root 375 apr< C4/>9 15:05 strreversed_constants.h-rw-r--r--1 root root 1619 APR 9 15:05 strreversed_types.cpp-rw-r--r--1 Root root 1230 Apr 9 15:05 strreversed_types.h-rw-r--r--1 root root 16679 Apr 9 15:05 strscheduler.cpp-rw-r--r--1 root root 9410 Apr 9 15:05 strscheduler.h-rw-r--r--1 root root 4715 May 11 16 : StrScheduler_server.skeleton.cpp
Gen-java file list:-rw-r--r--1 root root 10754 may 15:28 strinfo.java-rw-r--r--1 root root 50847 may one 15:28 Strscheduler . java
- 4. Use the compiled header file and write the server Code and client code
Modify file list C + + service side: StrScheduler_server.skeleton.cppjava client: Add File Javaclient.java
- 5. Use the language's compilation tool to generate the executable file
For example:
c++:g++-dhave_netinet_in_h-i/usr/local/include/thrift-i/usr/local/include/boost-i. /gen-cpp-l/usr/local/lib/-lthrift strreversed_constants.cpp strreversed_types.cpp StrScheduler.cpp StrScheduler_ Server.skeleton.cpp-o Cppserver
Java:javac *.java
Iv. Testing
- Start the service, CD to the Gen-cpp directory, execute the command:./cppserver
- Launch client, CD to Gen-java directory, execute command: Java javaclient Zhangshuaipeng
- The server receives the data Zhangshuaipeng
v. Data formats supported by thrift
Type description bool true, False byte 8-bit signed integer I16 16-bit signed integer i32 32-bit signed integer i64 64-bit signed integer double A 64-bit floating-point string UTF-8 encoded string binary- character array struct-struct list of ordered elements, similar to STL's vector set of unordered sets of distinct elements, A map similar to STL's set map Key-value, similar to the STL map exception , is a exception base class service that inherits from the local language and contains multiple function interfaces (pure virtual functions)
Vi. Other
- 1. Development of C + + client
- 1.1. Add File Client.cpp
- 1.2. Compile the statement:
g++-dhave_netinet_in_h-i/usr/local/include/thrift-i/usr/local/include/boost-i. /gen-cpp-l/usr/local/lib/-lthrift strreversed_constants.cpp strreversed_types.cpp StrScheduler.cpp client.cpp-o Cppclient
- 2. Development of Java Server
- 2.1. Add Files Javaserver.java and Testimpl.java
- 2.2, the compilation statement ibid.
Thrift Cross-language sample development