Thrift RPC 通信实现(优化)

上一篇是监听8080端口,转发请求到Server.php处理,这里将修改为Server监听端口,客户端进行连接,发送请求。

主要修改文件有

ComputeServer.php  重命名为 CServer.php
ComputeClient.php 重命名为CClient.php

1 CServer.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;
use Thrift\Factory\TBinaryProtocolFactory;
use Thrift\Factory\TTransportFactory;
use Thrift\Server\TServerSocket;
use Thrift\Server\TSimpleServer;

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);

    $transportFactory = new TTransportFactory();
    $protocolFactory = new TBinaryProtocolFactory(true, true);
     
    $transport = new TServerSocket('localhost', 8080);
    $server = new TSimpleServer($processor, $transport, $transportFactory, $transportFactory, $protocolFactory, $protocolFactory);
    $server->serve();

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

2 CClient.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 {
    $socket = new TSocket('localhost',8080);

    $transport = new TBufferedTransport($socket);
    $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;
}

3 执行操作

php CServer.php
client
php CClient.php
server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值