Thrift学习(三)协议通信实现

做一个demo感受一下整个过程,demo是简单的计算器功能。

1 目录结构

|----genphp thrift -r --gen php:server ComputeThrift.thrift 
|----lib  #thrift 的 lib文件夹
|----ComputeThrift.thrift #接口thrift文件
|----ComputeHandler.php #服务器handler定义文件
|----ComputeServer.php #服务器server代码,用于处理client请求
|----ComputeClient.php #客户端client代码

2 创建thrift文件

namespace php ComputeThrift

service ComputeService {
    i64 plus(1:i64 operator1, 2:i64 operator2)
    i64 minus(1:i64 operator1, 2:i64 operator2)
    i64 multiply(1:i64 operator1, 2:i64 operator2)
    i64 divide(1:i64 operator1, 2:i64 operator2)
}

3 thrift生成gen_php文件

thrift -r --gen php:server ComputeThrift.thrift 

重命名为genphp

4 ComputeHandler.php代码

<?php
namespace thrift;

error_reporting(E_ALL);

require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;

$gendir = realpath(dirname(__FILE__)).'/genphp';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__.'/lib/php/lib');
$loader->registerDefinition('ComputeThrift', $gendir);
$loader->register();

if (php_sapi_name() == 'cli') {
    ini_set('display_errors',"stderr");
}

class ComputeHandler implements \ComputeThrift\ComputeServiceIf
{
    public function plus($op1, $op2){
        return $op1 + $op2;
    }

    public function minus($op1, $op2)  {
        return $op1 - $op2;
    }

    public function multiply($op1, $op2){
        return $op1 * $op2;
    }

    public function divide($op1, $op2){
        return $op1 / $op2;
    }
}

5 ComputeServer.php代码

<?php
namespace ComputeThrift\php;

error_reporting(E_ALL);

require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
require_once __DIR__.'/ComputeHandler.php';

use Thrift\ClassLoader\ThriftClassLoader;
use \thrift\ComputeHandler;

$gendir = realpath(dirname(__FILE__)).'/genphp';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__.'/lib/php/lib');
$loader->registerDefinition('ComputeThrift', $gendir);
$loader->register();

if (php_sapi_name() == 'cli') {
    ini_set('display_errors',"stderr");
}

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;

header('Content-Type','application/x-thrift');
if (php_sapi_name() == 'cli') {
    echo PHP_EOL;
}
try {
    $handler = new ComputeHandler();
    $processor = new \ComputeThrift\ComputeServiceProcessor($handler);

    $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
    $protocol = new TBinaryProtocol($transport, true, true);

    $transport->open();
    $processor->process($protocol,$protocol);
    $transport->close();

} catch(\TException $e) {
    print 'TException:'.$e->getMessage().PHP_EOL;
}

6 ComputeClient.php代码

<?php
namespace  ComputeThrift\php;

error_reporting(E_ALL);
require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;

$gendir = realpath(dirname(__FILE__)).'/genphp';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__.'/lib/php/lib');
$loader->registerDefinition('ComputeThrift', $gendir);
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;

try {
    if (array_search('--http', $argv)) {
        $socket = new THttpClient('localhost',8080,'/ComputeServer.php');
    } else {
        $socket = new TSocket('localhost',9090);
    }

    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol  = new TBinaryProtocol($transport);
    $client = new \ComputeThrift\ComputeServiceClient($protocol);

    $transport->open();

    $op1 = 1;
    $op2 = 2;
    $plus = $client->plus($op1, $op2);
    echo '1 + 2 = '.$plus. "\n";

    $minus = $client->minus($op1, $op2);
    echo '1 - 2 = '.$minus. "\n";

    $multiply = $client->multiply($op1, $op2);
    echo '1 * 2 = '.$multiply. "\n";

    $divide = $client->divide($op1, $op2);
    echo '1 / 2 = '.$divide. "\n";

    $transport->close();

} catch (\Exception $e) {
    print 'TException:'.$e->getMessage().PHP_EOL;
}

7 运行

php -S localhost:8080 #启动server
php Client.php --http #运行client,就会打印出四次算术运算的结果
  • Server Output:

server output

Client Output:

Client output

注:这里只是在client.php里定义socket的host和port,然后转发Server.php文件执行,最终是需要server.php中定义监听端口,这样才是完整的,这里只做测试。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值