看看怎么唱好thrift shop是怎么玩的

1、Thrift简介Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架,该框架已经开源并且加入的Apache。Thrift主要功能是:通过自定义的&Definition&Language(IDL),可以创建基于RPC的和的服务代码。服务代码的生成是通过Thrift内置的代码生成器来实现的。Thrift&的跨语言性体现在,它可以生成C++,&,&,&PHP,&Ruby,&Erlang,&Perl,&Haskell,&C#,&Cocoa,&,&Node.js,&Smalltalk,&OCaml&,&Delphi等语言的代码,且它们之间可以进行透明的通信。2、为什么要使用Thrift?众所周知,PHP因为开发周期快,易部署,上手简单,社区活跃而很受欢迎,但是有优点就有缺点,php没有得到一些大型开源的支持,例如Hbase,cassandra等,所以这时候,就会采用Thrift做为中介,来统一Java等服务端与php客户端的通信,Thrift只是根据用来生成固有格式的代码框架,不做使用,真正提供服务的是通过框架写出的java等代码。3、安装Thrift,我准备使用java写服务端,所以需要安装java等系列环境1)安装java环境安装下载jdk-7u79--x64_0.rpm安装rpm -ivh jdk-7u79-linux-x64_0.rpm环境变量配置export JAVA_HOME=/usr/java/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar2)安装antmkdir /usr/ant
cd /usr/ant
wget -c http://mirror./apache//ant/binaries/apache-ant-1.9.6-bin.tar.gz
tar -zvxf apache-ant-1.9.6-bin.tar.gz
export ANT_HOME=/usr/ant/apache-ant-1.9.6
export PATH=$ANT_HOME/bin:$PATH3)更新bison到2.5以上版本wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
tar xvf bison-2.5.1.tar.gz
cd bison-2.5.1
./configure --prefix=/usr
sudo make install4)安装autoconf,需要2.65以上版本wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar xvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=/usr
sudo make install5)[非必须]升级g++,需要4.2版本以上,如果是了,就不要升级g++了,判断方法g++ -vwget -c http://ftp.tsukuba.wide.ad.jp//gcc/releases/gcc-4.8.1/gcc-4.8.1.tar.gz
tar -zvxf gcc-4.8.1.tar.gz
cd gcc-build-4.8.1
./contrib/download_prerequisites
./configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make install
cp gcc-build-4.8.1/prev-x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.18 /usr/lib
ln -sf /usr/lib/libstdc++.so.6.0.18 /usr/lib64/libstdc++.so.66)安装Thriftwget -c http://archive.apache.org/dist/thrift/0.9.0/thrift-0.9.0.tar.gz
cd thrift-0.9.0
./configure --with-ruby=no --with-java
make install在命令行执行thrift -查看,显示如下就成功了。&下一篇将介绍java服务端与php客户端编码教程,如有问题欢迎批评指证:)参考文献:http://thrift.apache.org/docs/BuildingFromSourcehttp://dongxicheng.org/search-engine/thrift-framework-intro/http://blog.csdn.net/yang/article/details/
版权声明:本文为博主原创,未经博主允许不得转载。Posts - 43,
Articles - 2,
Comments - 46
17:53 by xlw, ... 阅读,
1. 基本名词
& & &Types: 为了满足多语言平台的要求,需要提供基本数据类型来进行转换。比如在C++的Map和Python的Dict之间能够相互转换。
& & &Transport: 对于每一种语言,都应该有一个抽象的公共层来完成对应的双向数据传输。
& & &Protocal: 数据需要有一种方式来使用对应的传输层的code,而不用关心传输层的具体实现细节。
& & &Versioning:数据需要有自己的版本号来实现对应的健壮性。
& & &Processing : 产生code来完成RPC调用。
& & 1. Goals:&
& & & & &1. 使用最基本的数据类型,不管上层使用怎么样的编程语言。
& & & & &2. 不使用动态数据类型,也不需要开发者写对象序列化或者传输的代码(已经封装)。
& & & & &3. IDL 用来告诉代码生成器如何在多语言之上安全地传输数据。
& & 2. Base Types:
& & & & &1. 选取了大多数语言中都存在的基本数据类型,忽略了一些语言中特别的类型。(无符号类型), 所有的基本
& & & & & & 类型都是有符号的。
& & & & &2. 基本类型包括bool, byte,i16,i32,i64,double,string。
& & 3. Structs:
& & & & & 1. & 提供了一种common的对象来代表面向对象语言中的对象类型,每一个filed都有一个标志符。
& & & & & 2. & 每个filed必须有一个标志符和一个给定的默认值。标志符如果没有给定的话,会自动分配一个,但是强烈建议给定标志符,为了方便版本控制。
& & &4. Container:
& & & & & 1. &List, set, Map。其中List被翻译成STL中的vector, 或者Java中的ArrayList,set和map类似。
& & & & & 2. &要求对应的基本语言类型支持对应的Thrift的基本数据类型。
& & & & & 3. &在每一种目标语言中,对于每一种定义的类型,生成两个接口,read和write,用于序列化和传输对应的数据结构。
& & &5. Exceptions:
& & & & & 1. 使用Exception关键字来代替对应的structs关键字,得到对应的基本类。
& & & & & 2. 生成的对象继承自每一种语言中的对应的Exception的基类。
& & &6. Services:
& & & & & &1. 提供了服务端的接口调用,通过代码生成器会生成Client端和Server端的桩函数接口,共上层调用使用。
& & & & & &2. 在每一个函数前面可以加上async关键字,这个关键字能够实现对应的一部方法。
& & & & & &3. 一个纯void函数也会返回一个response,用来保证该操作在服务端已经被执行。
& & & & & &4. async call只保证消息在传输层是没有问题的,因此,在使用async的时候需要保证当前的使用环境对于丢包是可以接受的。
3. Transport
& & & 1. 接口
& & & & & & 1. 传输层用来促进数据传输。一个关键的设计是Thrift将数据传输层和代码生成器分开。
& & & & & & 2. 由于抽象的IO炒作产生的tradoff相比起实际的IO操作显得微不足道。
& & & & & & 3. 需要满足的基本功能:如何读写数据,不管是在网络中,还是共享内存,还是从磁盘文件中读写。
& & & & & & 4. 提供的接口如下:open, close,isOpen, read, write, flush。还有一些用于batch操作的接口
& & & & & & 5. 对于TServerTransport,还有接口用来创建对应的Transport对象。如open, listen, accept, close。
& & & &2. 实现
& & & & & & 1. TSocket 类对于TCP/IP的流式套接字实现了一个通用,简单的接口。
& & & & & & 2. TFileTransport类实现了一个抽象接口,用于将磁盘文件导入到数据流中。
& & & &3. Utilities
& & & & & & 1. 网络传输层设计的支持简单的扩展,比如组合,TBufferdTransport实现在传输层实现了buffer的功能,TFrameTransport实现了数据头定长大小,用于Noblocking传输。TMemoryBuffer允许读和写直接在进程的堆顶完成。
4. Protocal
& & & & &1. 接口
& & & & & & & &1. 另外一块主要的部分是thrift中将数据结构和传输层分离,thrift定义了一些传输时的基本类型,但是没有强制定义需要实现的编码格式,只要能被代码生成器识别就好。
& & & & & & & & 2. &thrift协议的接口简单明了,支持两种功能:双向有序的消息,基本类型,容器,结构的编码。
& & & & &2. 结构
& & & & & & & & 1. Thrift中默认使用流式结构来传输对应的协议,这样避免了因为需要分片或者在发送前整体计算带来的性能损失。当有的场景需要使用分片,或者分片的性能优势更加明显的时候,可以使用TFramedTransport这个抽象接口类。
& & & & &3. 实现
& & & & & & & & &1. Thrift的实现了一个空间高效的二进制协议,所有的数据都以一个扁平的二进制格式存在,整形被转换为网络字节序,字符串在头上加上它的长度,所有的消息和field 头都用它的整数序列号,忽略这些filed中的字符串名字。
& & & & & & & & &2. 我们没有做过多的性能优化,为了代码的整洁和高效。如果有需要,可以加入这些优化方案。
5.Versioning
& & & &1. 域标志符
& & & & & & 1. Thrift在版本控制上面是健壮的,它可以接受旧客户端过来的请求并正确处理。
& & & & & & 2. Thrift中的版本控制是通过域标志符来确定的,thrift结构中的每一项前面都有一个对应的标志符,标志符和数据类型唯一化开item。
& & & & & & 3. 为了避免冲突,自动分配的标志符从-1开始递减,人工定义的标志符只能为整数。
& & & & & & 4. 在数据是自定义的前提下,当反序列化的时候,thrift的code 生成器可以根据标识符来判断读取到的数据是否对齐,也可以跳过不能识别的域,保证兼容性。
& & & & & & 5. 域标志符也可以在参数列表中声。
& & & &2. Isset
& & & & & & &1. 用来标注必须存在于结构体中的域,如果必须存在,标注为true,这样可以当该域不存在的时候,可以通知上层调用者。
& & & & 3. 场景分析
& & & & & & & 1. 4种场景,加减域/新旧client或者server。
& & & & &4. Protocal/Transport Versioning
& & & & & & & &1. 实现了TProtocal的抽象接口,用来让协议的实现自己来管理版本。
6. RPC 实现
& & & & 1. TProcessor
& & & & & & & & 1. 核心类,抽象出client和server的基类,核心功能是处理input和output。
& & & & 2. Generated code
& & & & & & & & &2. 自动生成client端和server端的调用接口。
& & & & 3. TServer
& & & & & & & & &1. Tserver 可以有多种实现,来满足不同的需求。如单线程的TSimpleServer, 每个连接一个线程的TThreadedServer, 还有基于线程库的TThreadPoolServer.
& & & & & & & & &2. 开发者可以实现自己的TServer。
7. 实现细节
& & & & &1. 目标语言
& & & & & & & & 1. C++, Java, Python, Ruby, PHP
& & & & & & & & 2. 尽可能简单的抽象,保留在每一种语言中可以自主开发的空间。
& & & & &2. 生成的结构体
& & & & &3. RPC 方法定义
& & & & & & & & 1. RPC call中的方法用string去实现,用一些手段将字符处理的代价尽可能的减少,同时方便编程实现。
& & & & &4. 服务器和多线程
& & & & & & & & 1. 因为种种原因,没有采用boost的线程库,也没有采用ACE,自己实现了一套机制。
& & & & & 5. 线程模型
& & & & & & & & 1. thfirt的线程库包括三个部分:primitives, thread pool manager, timer manager
& & & & & & & & 2. 每个线程是一个对象,提供了Runnerable接口供使用,Synchronized关键字实现类似java的同步块功能。
& & & & & &6. Thread,Runnable, and shared_ptr
& & & & & &7. ThreadManager
& & & & & &8. TimeManager
& & & & & &9. NonBlocking Operation
& & & & & &10. 编译器
& & & & & &11. TFileTransport
8. Facebook thrift service
& & & & & &1. Search&&logging
& & & & & & & & 1. TFileTransport可以实现对应的结构化logging, 这个logging日志可以用来做线上和线下的处理,可以作为redo log。后使用快捷导航没有帐号?
查看: 3621|回复: 5
各位帮我看看,为何我的thrift编译不成功。
金牌会员, 积分 1439, 距离下一级还需 1561 积分
论坛徽章:7
1, 执行configure是如下结果:
[@master thrift-0.9.1]$ ./configure
thrift 0.9.1
Building C++ Library ......... : no
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
Python Library:
& &Using Python .............. : /usr/bin/python
2,执行make的时候报错了。
[hadoop@master thrift-0.9.1]$ make
。。。。。。。
make[3]: Entering directory `/opt/modules/hadoop/thrift-0.9.1/lib/py'
/usr/bin/python setup.py build
running build
running build_py
running build_ext
building 'thrift.protocol.fastbinary' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c src/protocol/fastbinary.c -o build/temp.linux-i686-2.4/src/protocol/fastbinary.o
src/protocol/fastbinary.c:20:20: error: Python.h: No such file or directory
src/protocol/fastbinary.c:21:23: error: cStringIO.h: No such file or directory
src/protocol/fastbinary.c:138: error: expected specifier-qualifier-list before ‘PyObject’
src/protocol/fastbinary.c:148: error: expected specifier-qualifier-list before ‘PyObject’
src/protocol/fastbinary.c:157: error: expected specifier-qualifier-list before ‘PyObject’
src/protocol/fastbinary.c:168: error: expected specifier-qualifier-list before ‘PyObject’
src/protocol/fastbinary.c:178: error: expected specifier-qualifier-list before ‘PyObject’
src/protocol/fastbinary.c:183: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/protocol/fastbinary.c:185: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/protocol/fastbinary.c: In function ‘check_ssize_t_32’:
src/protocol/fastbinary.c:190: warning: implicit declaration of function ‘PyErr_Occurred’
src/protocol/fastbinary.c:194: warning: implicit declaration of function ‘PyErr_SetString’
src/protocol/fastbinary.c:194: error: ‘PyExc_OverflowError’ undeclared (first use in this function)
src/protocol/fastbinary.c:194: error: (Each undeclared identifier is reported only once
src/protocol/fastbinary.c:194: error: for each function it appears in.)
src/protocol/fastbinary.c: At top level:
src/protocol/fastbinary.c:201: error: expected ‘)’ before ‘*’ token
src/protocol/fastbinary.c:220: error: expected declaration specifiers or ‘...’ before ‘PyObject’
src/protocol/fastbinary.c: In function ‘parse_set_list_args’:
src/protocol/fastbinary.c:221: warning: implicit declaration of function ‘PyTuple_Size’
src/protocol/fastbinary.c:221: error: ‘typeargs’ undeclared (first use in this function)
src/protocol/fastbinary.c:222: error: ‘PyExc_TypeError’ undeclared (first use in this function)
src/protocol/fastbinary.c:226: warning: implicit declaration of function ‘PyInt_AsLong’
src/protocol/fastbinary.c:226: warning: implicit declaration of function ‘PyTuple_GET_ITEM’
src/protocol/fastbinary.c:231: error: ‘SetListTypeArgs’ has no member named ‘typeargs’
src/protocol/fastbinary.c: At top level:
src/protocol/fastbinary.c:237: error: expected declaration specifiers or ‘...’ before ‘PyObject’
src/protocol/fastbinary.c: In function ‘parse_map_args’:
src/protocol/fastbinary.c:238: error: ‘typeargs’ undeclared (first use in this function)
src/protocol/fastbinary.c:239: error: ‘PyExc_TypeError’ undeclared (first use in this function)
src/protocol/fastbinary.c:253: error: ‘MapTypeArgs’ has no member named ‘ktypeargs’
src/protocol/fastbinary.c:254: error: ‘MapTypeArgs’ has no member named ‘vtypeargs’
src/protocol/fastbinary.c: At top level:
src/protocol/fastbinary.c:260: error: expected declaration specifiers or ‘...’ before ‘PyObject’
。。。。。。。。。
是说我机器没有python?看了/usr/bin/python 有这个文件啊。。。
金牌会员, 积分 2666, 距离下一级还需 334 积分
论坛徽章:8
./configure -with-cpp=no
关闭C++选项试试
金牌会员, 积分 1439, 距离下一级还需 1561 积分
论坛徽章:7
环境是Red Hat Enterprise Linux Server release 5.2 。
金牌会员, 积分 1439, 距离下一级还需 1561 积分
论坛徽章:7
Yured 发表于
./configure -with-cpp=no
关闭C++选项试试
兄弟,不行啊,还是报上面那个错。
金牌会员, 积分 2666, 距离下一级还需 334 积分
论坛徽章:8
njsunlihua 发表于
兄弟,不行啊,还是报上面那个错。
2,安装g++编译器
root用户下
[root@namenode ~]# yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: centos.
* extras: centos.
* updates: centos.
base& && && && && && && && && && && && && && && && && && && && && & | 3.7 kB& &&&00:00& &&&
extras& && && && && && && && && && && && && && && && && && && && &&&| 3.4 kB& &&&00:00& &&&
updates& && && && && && && && && && && && && && && && && && && && & | 3.4 kB& &&&00:00& &&&
updates/primary_db& && && && && && && && && && && && && && && && &&&| 5.1 MB& &&&00:20& &&&
Setting up Install Process
Package 1kgconfig-0.23-9.1.el6.x86_64 already installed and latest version
Resolving Dependencies
3,安装 openssl openssl-devel (CentOS)
root用户下
[root@namenode ~]# yum -y install openssl openssl-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: centos.
* extras: centos.
* updates: centos.
Setting up Install Process
Resolving Dependencies
4,配置thrift
[grid@namenode thrift]$ pwd
/app/thrift
[grid@namenode thrift]$ ./configure
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... gawk
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
[grid@namenode thrift]$ ./configure -with-cpp=no
关闭C++选项,否则编译安装会报错
5,编译安装
[grid@namenode thrift]$ make
make&&all-recursive
make[1]: Entering directory `/app/thrift'
Making all in compiler/cpp
make[2]: Entering directory `/app/thrift/compiler/cpp'
make&&all-am
make[3]: Entering directory `/app/thrift/compiler/cpp'
g++ -DHAVE_CONFIG_H -I. -I../.. -I../../lib/cpp/src/thrift&&-I./src&&-Wall -g -O2 -MT thrift-main.o -MD -MP -MF .deps/thrift-main.Tpo -c -o thrift-main.o `test -f 'src/main.cc' || echo './'`src/main.cc
mv -f .deps/thrift-main.Tpo .deps/thrift-main.Po
g++ -DHAVE_CONFIG_H -I. -I../.. -I../../lib/cpp/src/thrift&&-I./src&&-Wall -g -O2 -MT thrift-t_c_glib_generator.o -MD -MP -MF .deps/thrift-t_c_glib_generator.Tpo -c -o thrift-t_c_glib_generator.o `test -f 'src/generate/t_c_glib_generator.cc' || echo './'`src/generate/t_c_glib_generator.cc
[root@namenode thrift]# make install
Making install in compiler/cpp
make[1]: Entering directory `/app/thrift/compiler/cpp'
make&&install-am
make[2]: Entering directory `/app/thrift/compiler/cpp'
make[3]: Entering directory `/app/thrift/compiler/cpp'
test -z &/usr/local/bin& || /bin/mkdir -p &/usr/local/bin&
&&/bin/sh ../../libtool& &--mode=install /usr/bin/install -c thrift '/usr/local/bin'
libtool: install: /usr/bin/install -c thrift /usr/local/bin/thrift
make[3]: Nothing to be done for `install-data-am'.
查看安装是否成功
[root@namenode thrift]# clear
[root@namenode thrift]# thrift --version
Thrift version 0.9.1
[root@namenode thrift]# thrift --help
Usage: thrift [options] file
&&-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.
这是我的操作步骤,不过我用的是CentOS。你参照着这5步装了试试。
扫一扫加入本版微信群

我要回帖

更多关于 thrift 的文章

 

随机推荐