博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thrift学习(三)协议通信实现
阅读量:4293 次
发布时间:2019-05-27

本文共 3423 字,大约阅读时间需要 11 分钟。

做一个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 ComputeThriftservice 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代码

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代码

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代码

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 #启动serverphp Client.php --http #运行client,就会打印出四次算术运算的结果
  • Server Output:

server output

Client Output:

Client output

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

转载地址:http://xmuws.baihongyu.com/

你可能感兴趣的文章
Tomcat类加载机制
查看>>
for(;;) and while(true)
查看>>
CountDownLatch:闭锁
查看>>
关于系统与系统间调用时的逻辑健壮性
查看>>
notepad++的使用
查看>>
Spring使用@Value注解各种类型的值(Map,List,Set,数组,基本数据类型)
查看>>
搞懂Java ArrayList原码
查看>>
时间复杂度到底怎么算
查看>>
java8 日期时间类
查看>>
可重入锁
查看>>
IDEA 编辑之后保留缩进空格的设置
查看>>
MySQL limit offset的替代方案
查看>>
一点必要的感悟
查看>>
springboot Binder类
查看>>
java问题排查工具
查看>>
springboot的配置文件优先级,@ConfigurationProperties注解
查看>>
EnvironmentPostProcessor与spring.factories
查看>>
springboot没有加载application.yml文件的原因
查看>>
springboot conditional相关
查看>>
springboot @Configuration 和 @Import, @EnableAutoConfiguration 注解
查看>>