安裝centos須要的擴展php
yum install -y automake libtool flex bison pkgconfig gcc-c++ libevent-devel zlib-devel python-devel ruby-devel openssl-devel
安裝boost,本地源碼安裝最安全html
官網下載地址:https://www.boost.org/users/download/python
wget http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz tar -xvf boost_1_63_0.tar.gz cd boost_1_63_0 ./bootstrap.sh ./b2 install
安裝boost的時候,使用的yum install boost-devel,在最終安裝thrift包的時候,會報找不到libboost_unit_test_framework.a文件,並非google中表述的把/usr/lib64/libboost_unit_test_framework.a 軟鏈接下就好,而是安裝後的boost連/usr/lib64/目錄下都不存在這個文件libboost_unit_test_framework.a文件;c++
安裝thrift ,不要用git包,直接到官網上下載tar包git
wget http://apache.fayea.com/thrift/0.11.0/thrift-0.11.0.tar.gz tar -xvf thrift-0.11.0.tar.gz cd thrift-0.11.0 ./configure --with-lua=no make && make install
php使用thriftapache
hrift 再也不介紹。體驗一把 PHP 的Server , PHP 的Client。bootstrap
0.安裝,裝備環境,不表,運行 thrift -version 看到版本就行。centos
1.寫 HelloThrift.thrift .api
這個是個標準文本,裏面只有一個sayHello 方法。安全
HelloThrift.thrift
namespace php HelloThrift service HelloService { string sayHello(1:string username) }
經過 thrift -r --gen php:server HelloThrift.thrift 命令會在當前目錄下生成一個 gen-php 。爲了方便更名爲 gen_php 。注意劃線。
2.把 thrift 的 lib文件夾 拷到項目的根下
3.寫服務端。Server.php
1 <?php 2 namespace HelloThrift\php; 3 4 error_reporting(E_ALL); 5 6 require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; 7 use Thrift\ClassLoader\ThriftClassLoader; 8 9 $GEN_DIR = realpath(dirname(__FILE__)).'/gen_php'; 10 $loader = new ThriftClassLoader(); 11 $loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib'); 12 $loader->registerDefinition('HelloThrift',$GEN_DIR); 13 $loader->register(); 14 15 if (php_sapi_name() == 'cli') { 16 ini_set('display_errors',"stderr"); 17 } 18 19 use Thrift\Protocol\TBinaryProtocol; 20 use Thrift\Transport\TPhpStream; 21 use Thrift\Transport\TBufferedTransport; 22 23 class HelloHandler implements \HelloThrift\HelloServiceIf { 24 25 public function sayHello($username) 26 { 27 return "Hello ".$username; 28 } 29 } 30 31 header('Content-Type','application/x-thrift'); 32 if (php_sapi_name() == 'cli') { 33 echo PHP_EOL; 34 } 35 36 $handler = new HelloHandler(); 37 $processor = new \HelloThrift\HelloServiceProcessor($handler); 38 39 $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); 40 $protocol = new TBinaryProtocol($transport,true,true); 41 42 $transport->open(); 43 $processor->process($protocol,$protocol); 44 $transport->close();
服務器的只有一個sayHello 方法。在require 時注意一下文件路徑。
4.寫客戶端。Client.php
1 <?php 2 namespace HelloThrift\php; 3 4 error_reporting(E_ALL); 5 require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; 6 use Thrift\ClassLoader\ThriftClassLoader; 7 8 $GEN_DIR = realpath(dirname(__FILE__)).'/gen_php'; 9 $loader = new ThriftClassLoader(); 10 $loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib'); 11 $loader->registerDefinition('HelloThrift',$GEN_DIR); 12 $loader->register(); 13 14 use Thrift\Protocol\TBinaryProtocol; 15 use Thrift\Transport\TSocket; 16 use Thrift\Transport\THttpClient; 17 use Thrift\Transport\TBufferedTransport; 18 use Thrift\Exception\TException; 19 20 try { 21 if (array_search('--http',$argv)) { 22 $socket = new THttpClient('localhost',8080,'/Server.php'); 23 } else { 24 $socket = new TSocket('localhost',9090); 25 } 26 27 $transport = new TBufferedTransport($socket,1024,1024); 28 $protocol = new TBinaryProtocol($transport); 29 $client = new \HelloThrift\HelloServiceClient($protocol); 30 31 $transport->open(); 32 33 echo $client->sayHello(" World! "); 34 35 $transport->close(); 36 } catch (\Exception $e) { 37 print 'TException:'.$e->getMessage().PHP_EOL; 38 }
裏面也只調用一次 sayHello 方法。
5.運行服務。在項目的根下運行命令
php7 -S localhost:8080 -t ./
6.運行客戶端進行測試。直接使用 php Client --http 命令。能夠看到 Hello World! 成功。
目錄結構:
安裝參考文獻:
https://blog.csdn.net/cjf_wei/article/details/78700321
http://www.1207.me/archives/301.html
https://blog.csdn.net/mingtian625/article/details/52302893
https://blog.csdn.net/wzbwzh/article/details/85009391