上一篇是监听 8080端口 ,转发请求到Server.php处理,这里将修改为Server监听端口,客户端进行连接,发送请求。
主要修改文件有
ComputeServer.php 重命名为 CServer.php
ComputeClient.php 重命名为CClient.php
1 CServer.php
<?php namespace Compute thrift \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\TServer socket ; 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.phpphp CClient.php #即可以看到结果输出
注:sever包含很多模式,阻塞、非阻塞、多线程等。