2

此教程基于 Linux CentOS 6.0, php 5.3X 环境

下载

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

PS:官网下那个安装包有诸多问题

安装

环境变量:

export PATH=$PATH:{php_src}/bin

这一步很重要,用于系统寻找 phpize 和 php-config

安装依赖库:

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

安装 autoconf (需要2.65版本以上):

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 依赖关系真是让人捉急啊...

安装 thrift:

./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

检查工作:

  • 查看PHP扩展安装目录中有 thrift_protocol.so 则 PHP 扩展安装成功
  • 查看 /usr/local/include/thrift/c_glib 存在则C Library安装成功
  • 查看 /usr/local/include/thrift/(server|protocol|...) 存在则C++ Library安装成功

修改 php.ini:

添加

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

(全文完)


丁靖
619 声望121 粉丝

好好学习, 天天编码.


下一篇 »
autoconf-101