轉載請指明出處:http://www.cnblogs.com/lpit/p/4902292.html
thrift 是什么東西,就不多說了 ,網上都有。
現在展示我做的API。
1. 安裝thrift 后開始寫代碼。
環境:
Linux version 2.6.18;
Thrift version 0.9.3;
PHP 5.5.9 ;
Python 2.4.3;
部署:
server 端使用 php,
client 端使用 php,
端口監控使用 python,
目錄結構:
2. 展示代碼:
創建 thrift 文件。
說明:准備使用thrift實現一個獲取圖片信息的API,這里展示調通的結果。
下面是 thrift 文件 ,文件名: imginfo.thrift
1 namespace php Img 2 3 service ImgInfo{ 4 string getimgInfo(1: string url), 5 }
2. 生成文件thrift文件中間接口文件
1 thrift -r --gen php:server imginfo.thrift
官網上面沒有添加server參數。我添加sever並測試通過,官網的方法本人沒有測試。
會在 imginfo.thrift 的目錄生成一個 gen-php 目錄,采用不同的語言會生成不同的目錄
3. 編寫客戶端(client)程序
在imginfo.thrift 目錄層 創建php目錄 用於存放 客戶端腳本。
文件名:c.php
1 <?php 2 3 namespace Img\php; 4 # 紅色是需要根據自己的實際情況修改目錄的點 5 error_reporting(E_ALL); 6 7 require_once '/root/tools/thrift-0.9.3/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; 8 9 use Thrift\ClassLoader\ThriftClassLoader; 10 11 $GEN_DIR = realpath(dirname(__FILE__) . '/../') . '/gen-php'; 12 $loader = new ThriftClassLoader(); 13 $loader->registerNamespace('Thrift', '/root/tools/thrift-0.9.3/lib/php/lib'); 14 $loader->registerDefinition('Img', $GEN_DIR); 15 $loader->register(); 16 17 18 use Thrift\Protocol\TBinaryProtocol; 19 use Thrift\Transport\TSocket; 20 use Thrift\Transport\THttpClient; 21 use Thrift\Transport\TBufferedTransport; 22 use Thrift\Exception\TException; 23 24 try { 25 if (array_search('--http', $argv)) { 26 $socket = new THttpClient('localhost', 8080, '/php/s.php'); // 實際的server文件地址 27 } else { 28 $socket = new TSocket('localhost', 8080); 29 } 30 $transport = new TBufferedTransport($socket, 1024, 1024); 31 $protocol = new TBinaryProtocol($transport); 32 $client = new \Img\ImgInfoClient($protocol); 33 $transport->open(); 34 35 #[ start 根據自己的文件調用就行 ]################################################# 36 $ret = $client->getimgInfo('BBBBBBBBBBBBBBBBBBBB'); 37 echo $ret; 38 echo "<br /> \r\n"; 39 40 #[ end ]################################################# 41 42 $transport->close(); 43 } catch (TException $tx) { 44 print 'TException: ' . $tx->getMessage() . "\n"; 45 } 46 ?>
4. 編寫服務端(server)端
文件名:s.php ;也放在了php目錄下。
需要注意的點 我用紅色標識
1 #! /bin/env php 2 <?php 3 namespace Img\php; 4 5 error_reporting(E_ALL); 6 7 require_once '/root/tools/thrift-0.9.3/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; 8 9 use Thrift\ClassLoader\ThriftClassLoader; 10 11 $GEN_DIR = realpath(dirname(__FILE__).'/../').'/gen-php'; 12 13 $loader = new ThriftClassLoader(); 14 $loader->registerNamespace('Thrift', '/root/tools/thrift-0.9.3/lib/php/lib'); 15 $loader->registerDefinition('Img', $GEN_DIR); 16 $loader->register(); 17 if (php_sapi_name() == 'cli') { 18 ini_set("display_errors", "stderr"); 19 } 20 21 use Thrift\Protocol\TBinaryProtocol; 22 use Thrift\Transport\TPhpStream; 23 use Thrift\Transport\TBufferedTransport; 24 25 class ImgInfoHandler implements \Img\ImgInfoIf { 26 protected $log = array(); 27 # 編寫自己個功能吧 28 public function getimgInfo($name) { 29 return $name . ' ==> client Request server is ok!'; 30 } 31 32 }; 33 34 header('Content-Type', 'application/x-thrift'); 35 if (php_sapi_name() == 'cli') { 36 echo "\r\n"; 37 } 38 39 $handler = new \Img\php\ImgInfoHandler(); 40 $processor = new \Img\ImgInfoProcessor($handler); 41 42 $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); 43 $protocol = new TBinaryProtocol($transport, true, true); 44 45 $transport->open(); 46 $processor->process($protocol, $protocol); 47 $transport->close();
4. 編寫端口監控代碼。
使用python,用於監控指定端口的http請求並交給s.php 處理
需要注意的使用紅色標識
1 #!/usr/bin/env python 2 3 4 5 import os 6 import BaseHTTPServer 7 import CGIHTTPServer 8 9 # chdir(2) into the tutorial directory. 10 os.chdir(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) 11 # 指定目錄 ,如果目錄錯誤 請求會失敗 12 class Handler(CGIHTTPServer.CGIHTTPRequestHandler): 13 cgi_directories = ['/php'] 14 15 16 BaseHTTPServer.HTTPServer(('', 8080), Handler).serve_forever()
注意: client端請求server的腳本地址 需要和這里的保持一致。
5. 測試:
1) 開啟監控
python r.py
2)c 端請求 s 端
[root@localhost php]# php c.php --http
BBBBBBBBBBBBBBBBBBBB ==> client Request server is ok!<br />
3)python 監控結果展示
[root@localhost php]# python r.py localhost.localdomain - - [22/Oct/2015 02:36:34] "POST /php/s.php HTTP/1.0" 200 -
完成,希望對大家有幫助。
轉載請指明出處:http://www.cnblogs.com/lpit/p/4902292.html