安装centos需要的扩展
yum install -y automake libtool flex bison pkgconfig gcc-c++ libevent-devel zlib-devel python-devel ruby-devel openssl-devel
安装boost,本地源码安装最安全
官网下载地址:https://www.boost.org/users/download/
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文件;
安装thrift ,不要用git包,直接到官网上下载tar包
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使用thrift
hrift 不再介绍。体验一把 PHP 的Server , PHP 的Client。
0.安装,装备环境,不表,运行 thrift -version 看到版本就行。
1.写 HelloThrift.thrift .
这个是个标准文本,里面只有一个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
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/9965522.html