什么是Thrift
The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages
上面这段话选自Apache对Thrift的一句话介绍,Thrift是一个高性能的RPC服务框架。在技术选型时,有如下三方面的需求时,考虑使用Thrift
1.高并发
2.请求和响应的数据传输量大
3.业务系统跨多种语言
个人认为在没有特别strong的理由情况下,慎用Thrift。HTTP请求通常都能够满足需求,使用Thrift带来很多开发上的额外的工作量,一个简单的服务,就要写客户端代码,.thrift接口定义以及服务器端的服务响应代码,简单的事情复杂化。使用更多的RPC框架是codehaus的Hessian
Thrift安装
以在Ubuntu上安装为例
参考:
http://thrift.apache.org/docs/install/debian
http://thrift.apache.org/docs/BuildingFromSource
1.下载Thrift安装包
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.1/thrift-0.9.1.tar.gz
2.安装Thrift编译和安装所以来的工具和库
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
3.解压thrift-0.9.1.tar.gz
tar -xzvf thrift-0.9.1.tar.gz
4.进入Thrift的解压目录
5.在终端命令行执行命令
./configure JAVAC=/software/devsoftware/jdk1.7.0_55/javac
--JAVAC选项表示显式的指定javac命令的位置,而不需thrift在编译时根据系统配置进行猜测
配置过程提示信息如下,等Thrift熟悉了,再来看看这个过程应该是比较有意思的
tom$ ./configure JAVAC=/software/devsoftware/jdk1.7.0_55/javac checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking how to create a ustar tar archive... gnutar checking for pkg-config... /usr/bin/pkg-config checking pkg-config is at least version 0.9.0... yes checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for style of include used by make... GNU checking dependency style of gcc... gcc3 checking how to run the C preprocessor... gcc -E checking for g++... g++ checking whether we are using the GNU C++ compiler... yes checking whether g++ accepts -g... yes checking dependency style of g++... gcc3 checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking how to print strings... printf checking for a sed that does not truncate output... /bin/sed checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for fgrep... /bin/grep -F checking for ld used by gcc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking whether the shell understands some XSI constructs... yes checking whether the shell understands "+="... yes checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop checking for /usr/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for dlltool... no checking how to associate runtime and link libraries... printf %s\n checking for ar... ar checking for archiver @FILE support... @ checking for strip... strip checking for ranlib... ranlib checking command to parse /usr/bin/nm -B output from gcc object... ok checking for sysroot... no checking for mt... mt checking if mt is a manifest tool... no checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for dlfcn.h... yes checking for objdir... .libs checking if gcc supports -fno-rtti -fno-exceptions... no checking for gcc option to produce PIC... -fPIC -DPIC checking if gcc PIC flag -fPIC -DPIC works... yes checking if gcc static flag -static works... yes checking if gcc supports -c -o file.o... yes checking if gcc supports -c -o file.o... (cached) yes checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... yes checking how to run the C++ preprocessor... g++ -E checking for ld used by g++... /usr/bin/ld -m elf_x86_64 checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking for g++ option to produce PIC... -fPIC -DPIC checking if g++ PIC flag -fPIC -DPIC works... yes checking if g++ static flag -static works... yes checking if g++ supports -c -o file.o... yes checking if g++ supports -c -o file.o... (cached) yes checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking dynamic linker characteristics... (cached) GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether make sets $(MAKE)... (cached) yes checking for bison... bison -y checking for flex... flex checking lex output file root... lex.yy checking lex library... -lfl checking whether yytext is a pointer... yes checking whether ln -s works... yes checking for gawk... (cached) mawk checking for ranlib... (cached) ranlib checking for boostlib >= 1.40.0... yes checking for libevent >= 1.0... yes checking for zlib >= 1.2.3... yes checking for QT... no checking for GLIB... no checking for GOBJECT... no checking for MONO... no checking for MONO... no checking for javac and java... no checking for ant... no checking for ant version > 1.7... expr: syntax error no checking for erl... no checking for erlc... no checking for a Python interpreter with version >= 2.4... python checking for python... /usr/bin/python checking for python version... 2.7 checking for python platform... linux2 checking for python script directory... ${prefix}/lib/python2.7/dist-packages checking for python extension module directory... ${exec_prefix}/lib/python2.7/dist-packages checking for trial... /usr/bin/trial checking for perl... /usr/bin/perl checking for perl module Bit::Vector... no checking for php... no checking for php-config... no checking for phpunit... no checking for ruby... no checking for rake... no checking for bundle... no checking for cabal... no checking for runhaskell... no checking for go... no checking for DMD... no checking for library containing strerror... none required checking for an ANSI C-conforming const... yes checking for inline... inline checking for working volatile... yes checking for stdbool.h that conforms to C99... yes checking for _Bool... no checking for ANSI C header files... (cached) yes checking whether time.h and sys/time.h may both be included... yes checking for sys/wait.h that is POSIX.1 compatible... yes checking return type of signal handlers... void checking arpa/inet.h usability... yes checking arpa/inet.h presence... yes checking for arpa/inet.h... yes checking sys/param.h usability... yes checking sys/param.h presence... yes checking for sys/param.h... yes checking fcntl.h usability... yes checking fcntl.h presence... yes checking for fcntl.h... yes checking for inttypes.h... (cached) yes checking limits.h usability... yes checking limits.h presence... yes checking for limits.h... yes checking netdb.h usability... yes checking netdb.h presence... yes checking for netdb.h... yes checking netinet/in.h usability... yes checking netinet/in.h presence... yes checking for netinet/in.h... yes checking pthread.h usability... yes checking pthread.h presence... yes checking for pthread.h... yes checking stddef.h usability... yes checking stddef.h presence... yes checking for stddef.h... yes checking for stdlib.h... (cached) yes checking sys/socket.h usability... yes checking sys/socket.h presence... yes checking for sys/socket.h... yes checking sys/time.h usability... yes checking sys/time.h presence... yes checking for sys/time.h... yes checking sys/un.h usability... yes checking sys/un.h presence... yes checking for sys/un.h... yes checking sys/poll.h usability... yes checking sys/poll.h presence... yes checking for sys/poll.h... yes checking sys/resource.h usability... yes checking sys/resource.h presence... yes checking for sys/resource.h... yes checking for unistd.h... (cached) yes checking libintl.h usability... yes checking libintl.h presence... yes checking for libintl.h... yes checking malloc.h usability... yes checking malloc.h presence... yes checking for malloc.h... yes checking openssl/ssl.h usability... yes checking openssl/ssl.h presence... yes checking for openssl/ssl.h... yes checking openssl/rand.h usability... yes checking openssl/rand.h presence... yes checking for openssl/rand.h... yes checking openssl/x509v3.h usability... yes checking openssl/x509v3.h presence... yes checking for openssl/x509v3.h... yes checking sched.h usability... yes checking sched.h presence... yes checking for sched.h... yes checking wchar.h usability... yes checking wchar.h presence... yes checking for wchar.h... yes checking for pthread_create in -lpthread... yes checking for clock_gettime in -lrt... yes checking for setsockopt in -lsocket... no checking for BN_init in -lcrypto... yes checking for SSL_ctrl in -lssl... yes checking for int16_t... yes checking for int32_t... yes checking for int64_t... yes checking for int8_t... yes checking for mode_t... yes checking for off_t... yes checking for size_t... yes checking for ssize_t... yes checking for uint16_t... yes checking for uint32_t... yes checking for uint64_t... yes checking for uint8_t... yes checking for ptrdiff_t... yes checking whether struct tm is in sys/time.h or time.h... time.h checking whether AI_ADDRCONFIG is declared... yes checking for working alloca.h... yes checking for alloca... yes checking for pid_t... yes checking vfork.h usability... no checking vfork.h presence... no checking for vfork.h... no checking for fork... yes checking for vfork... yes checking for working fork... yes checking for working vfork... (cached) yes checking for stdlib.h... (cached) yes checking for GNU libc compatible malloc... yes checking for working memcmp... yes checking for stdlib.h... (cached) yes checking for GNU libc compatible realloc... yes checking sys/select.h usability... yes checking sys/select.h presence... yes checking for sys/select.h... yes checking for sys/socket.h... (cached) yes checking types of arguments for select... int,fd_set *,struct timeval * checking whether lstat correctly handles trailing slash... yes checking whether stat accepts an empty string... no checking whether strerror_r is declared... yes checking for strerror_r... yes checking whether strerror_r returns char *... yes checking for strftime... yes checking for vprintf... yes checking for _doprnt... no checking for strtoul... yes checking for bzero... yes checking for ftruncate... yes checking for gethostbyname... yes checking for gettimeofday... yes checking for memmove... yes checking for memset... yes checking for mkdir... yes checking for realpath... yes checking for select... yes checking for socket... yes checking for strchr... yes checking for strdup... yes checking for strerror... yes checking for strstr... yes checking for strtol... yes checking for sqrt... yes checking for alarm... yes checking for clock_gettime... yes checking for sched_get_priority_min... yes checking for sched_get_priority_max... yes checking the behavior of a signed right shift... arithmetic configure: creating ./config.status config.status: creating Makefile config.status: creating compiler/cpp/Makefile config.status: creating compiler/cpp/version.h config.status: creating compiler/cpp/src/windows/version.h config.status: creating lib/Makefile config.status: creating lib/cpp/Makefile config.status: creating lib/cpp/test/Makefile config.status: creating lib/cpp/thrift-nb.pc config.status: creating lib/cpp/thrift-z.pc config.status: creating lib/cpp/thrift-qt.pc config.status: creating lib/cpp/thrift.pc config.status: creating lib/c_glib/Makefile config.status: creating lib/c_glib/thrift_c_glib.pc config.status: creating lib/c_glib/test/Makefile config.status: creating lib/csharp/Makefile config.status: creating lib/d/Makefile config.status: creating lib/d/test/Makefile config.status: creating lib/erl/Makefile config.status: creating lib/go/Makefile config.status: creating lib/go/test/Makefile config.status: creating lib/hs/Makefile config.status: creating lib/java/Makefile config.status: creating lib/js/test/Makefile config.status: creating lib/perl/Makefile config.status: creating lib/perl/test/Makefile config.status: creating lib/php/Makefile config.status: creating lib/php/test/Makefile config.status: creating lib/py/Makefile config.status: creating lib/rb/Makefile config.status: creating test/Makefile config.status: creating test/cpp/Makefile config.status: creating test/hs/Makefile config.status: creating test/nodejs/Makefile config.status: creating test/php/Makefile config.status: creating test/perl/Makefile config.status: creating test/py/Makefile config.status: creating test/py.twisted/Makefile config.status: creating test/py.tornado/Makefile config.status: creating test/rb/Makefile config.status: creating tutorial/Makefile config.status: creating tutorial/cpp/Makefile config.status: creating tutorial/go/Makefile config.status: creating tutorial/java/Makefile config.status: creating tutorial/js/Makefile config.status: creating tutorial/py/Makefile config.status: creating tutorial/py.twisted/Makefile config.status: creating tutorial/py.tornado/Makefile config.status: creating tutorial/rb/Makefile config.status: creating config.h config.status: config.h is unchanged config.status: creating lib/cpp/src/thrift/config.h config.status: lib/cpp/src/thrift/config.h is unchanged config.status: executing depfiles commands config.status: executing libtool commands thrift 0.9.1 Building C++ Library ......... : yes Building C (GLib) Library .... : no Building Java Library ........ : no Building C# Library .......... : no Building Python Library ...... : yes Building Ruby Library ........ : no Building Haskell Library ..... : no Building Perl Library ........ : no Building PHP Library ......... : no Building Erlang Library ...... : no Building Go Library .......... : no Building D Library ........... : no C++ Library: Build TZlibTransport ...... : yes Build TNonblockingServer .. : yes Build TQTcpServer (Qt) .... : no Python Library: Using Python .............. : /usr/bin/python If something is missing that you think should be present, please skim the output of configure to find the missing component. Details are present in config.log.
6. 在终端执行make命令进行编译
make
7. 执行命令make check执行完整性检查测试
make check
8. 执行命令sh test/test.sh执行单元测试
sh test/test.sh
9. 执行命令make install完成最后安装
sudo make install
10.执行which thrift查看thrift的安装路径
whcih thrift
默认,thrift安装在/usr/local/bin/thrift
11.执行thrift --help查看thrift命令的选项
Usage: thrift [options] file Options: -version Print the compiler version -o dir Set the output directory for gen-* packages (default: current directory) -out dir Set the ouput location for generated files. (no gen-* folder will be created) -I dir Add a directory to the list of directories searched for include directives -nowarn Suppress all compiler warnings (BAD!) -strict Strict compiler warnings on -v[erbose] Verbose mode -r[ecurse] Also generate included files -debug Parse debug trace to stdout --allow-neg-keys Allow negative field keys (Used to preserve protocol compatibility with older .thrift files) --allow-64bit-consts Do not print warnings about using 64-bit constants --gen STR Generate code with a dynamically-registered generator. STR has the form language[:key1=val1[,key2,[key3=val3]]]. Keys and values are options passed to the generator. Many options will not require values. Available generators (and options): as3 (AS3): bindable: Add [bindable] metadata to all the struct classes. c_glib (C, using GLib): cocoa (Cocoa): log_unexpected: Log every time an unexpected field ID or type is encountered. validate_required: Throws exception if any required field is not set. cpp (C++): cob_style: Generate "Continuation OBject"-style classes. no_client_completion: Omit calls to completion__() in CobClient class. templates: Generate templatized reader/writer methods. pure_enums: Generate pure enums instead of wrapper classes. dense: Generate type specifications for the dense protocol. include_prefix: Use full include paths in generated files. csharp (C#): async: Adds Async support using Task.Run. asyncctp: Adds Async CTP support using TaskEx.Run. wcf: Adds bindings for WCF to generated classes. serial: Add serialization support to generated classes. nullable: Use nullable types for properties. hashcode: Generate a hashcode and equals implementation for classes. union: Use new union typing, which includes a static read function for union types. d (D): delphi (delphi): ansistr_binary: Use AnsiString for binary datatype (default is TBytes). register_types: Enable TypeRegistry, allows for creation of struct, union and container instances by interface or TypeInfo() erl (Erlang): go (Go): package_prefix= Package prefix for generated files. thrift_import= Override thrift package import path (default:git.apache.org/thrift.git/lib/go/thrift) gv (Graphviz): exceptions: Whether to draw arrows from functions to exception. hs (Haskell): html (HTML): standalone: Self-contained mode, includes all CSS in the HTML files. Generates no style.css file, but HTML files will be larger. java (Java): beans: Members will be private, and setter methods will return void. private-members: Members will be private, but setter methods will return 'this' like usual. nocamel: Do not use CamelCase field accessors with beans. hashcode: Generate quality hashCode methods. android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above). java5: Generate Java 1.5 compliant code (includes android_legacy flag). sorted_containers: Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map. javame (Java ME): js (Javascript): jquery: Generate jQuery compatible code. node: Generate node.js compatible code. ocaml (OCaml): perl (Perl): php (PHP): inlined: Generate PHP inlined files server: Generate PHP server stubs oop: Generate PHP with object oriented subclasses rest: Generate PHP REST processors py (Python): new_style: Generate new-style classes. twisted: Generate Twisted-friendly RPC services. tornado: Generate code for use with Tornado. utf8strings: Encode/decode strings using utf8 in the generated code. slots: Generate code using slots for instance members. dynamic: Generate dynamic code, less code generated but slower. dynbase=CLS Derive generated classes from class CLS instead of TBase. dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase. dynimport='from foo.bar import CLS' Add an import line to generated code to find the dynbase class. rb (Ruby): rubygems: Add a "require 'rubygems'" line to the top of each generated file. st (Smalltalk): xsd (XSD):