thrift-101

此教程基於 Linux CentOS 6.0, php 5.3X 環境php

下載

git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift

PS:官網下那個安裝包有諸多問題java

安裝

環境變量:python

export PATH=$PATH:{php_src}/bin

這一步很重要,用於系統尋找 phpize 和 php-configc++

安裝依賴庫:git

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel openssl-devel

安裝 autoconf (須要2.65版本以上):apache

wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar
tar xzvf autoconf-latest.tar.gz
cd autoconf-xxx
./configure --prefix=/usr

libtool, autoconf, automake 依賴關係真是讓人捉急啊...bootstrap

安裝 thrift:ruby

./bootstrap.sh
./configure --with-cpp --with-boost --without-python --without-csharp --without-java --without-erlang --without-perl --with-php --with-php_extension --without-ruby --without-haskell --without-go --without-d --without-nodjs --without-lua --without-openssl=/usr
make && make install

檢查工做:socket

  • 查看PHP擴展安裝目錄中有 thrift_protocol.so 則 PHP 擴展安裝成功
  • 查看 /usr/local/include/thrift/c_glib 存在則C Library安裝成功
  • 查看 /usr/local/include/thrift/(server|protocol|...) 存在則C++ Library安裝成功

修改 php.ini:flex

添加

extension="thrift_protocol.so"

Demo

建立 demo.thrift:

namespace cpp demo
namespace php demo

/*
 C like comments are supported
*/
// This is also a valid comment

typedef string my_string // We can use typedef to get pretty names for the types we are using
service Demo
{
    my_string hello(1:my_string thing),
}

生成PHP客戶端:

thrift --gen php demo.thrift

生成C++服務端:

thrift --gen cpp demo.thrift

會產生兩個文件夾 gen-phpgen-cpp

編譯服務端:

找到 libthrift-1.0.0-dev.so 的位置 (我機器上的位置在/usr/local/lib )

1) 在gen-cpp

  • 建立服務端文件, 這裏我複製了thrfit生成的skeleton文件
cp Demo_server.skeleton.cpp Demo_server.cpp
  • 建立Makefile
GEN_SRC := Demo.cpp demo_php_constants.cpp demo_php_types.cpp
GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))

THRIFT_DIR := /usr/local/include/thrift
BOOST_DIR := /usr/local/include

INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)

.PHONY: all clean

all: demo_server

%.o: %.cpp
    $(CXX) -Wall $(INC) -c $< -o $@

demo_server: Demo_server.o $(GEN_OBJ)
    $(CXX) -L/usr/local/lib -lthrift $^ -o $@

clean:
    $(RM) *.o demo_server
  • 添加 ld 路徑: /etc/ld.so.conf.d 下建立 libthrift-x86_64.conf (名字能夠自定義, 以.conf結尾就行) 文件, 添加路徑/usr/local/lib.
  • make編譯後在文件夾下會生成demo_server.
  • ./demo_server啓動服務端.

2) 在gen-php

  • {thrift_src}/lib/php/lib/Thrift文件夾複製到gen-php
  • demo文件下建立文client.php
<?php
require_once '../Thrift/ClassLoader/ThriftClassLoader.php';
require_once 'Demo.php';
require_once 'Types.php';

use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Protocol\TBinaryProtocolAccelerated;

use demo\DemoClient;

$loader = new ThriftClassLoader();
$loader->register();
$loader->registerNamespace('Thrift\Base', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\Type', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\Exception', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\Transport', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\Protocol', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\Factory', dirname(dirname(__FILE__)));
$loader->registerNamespace('Thrift\StringFunc', dirname(dirname(__FILE__)));
$loader->registerNamespace('demo', dirname(dirname(__FILE__)));

//TBase
$loader->loadClass('TBase');
//Type
$loader->loadClass('TType');
$loader->loadClass('TMessageType');
//Transport
$loader->loadClass('TSocket');
$loader->loadClass('TBufferedTransport');
//Protocol
$loader->loadClass('TProtocol');
$loader->loadClass('TBinaryProtocolAccelerated');
//Factorys
$loader->loadClass('TStringFuncFactory');
//StringFunc
$loader->loadClass('Core');
//Exception
$loader->loadClass('TException');
$loader->loadClass('TProtocolException');
$loader->loadClass('TApplicationException');

try {
    $host = '127.0.0.1';
    $port = 9090;
    $socket = new TSocket($host ,$port);  
    $transport = new TBufferedTransport($socket, 1024, 1024);  
    $protocol = new TBinaryProtocolAccelerated($transport); 

    $client = new DemoClient($protocol);
    $transport->open();

    $ret = $client->hello("Hello world!!");
    echo $ret;

    $transport->close();
} catch (TException $e) {
    print 'Something went wrong: ' . $e->getMessage() . "\n";  
}

執行:

php client.php

服務端顯示:

hello

(全文完)

本站公眾號
   歡迎關注本站公眾號,獲取更多信息