<?php
namespace Message\Controller;
use Think\Controller;
use Thrift\Exception\TException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TPhpStream;
use Thrift\TMultiplexedProcessor;
use Thrift\Protocol\TMultiplexedProtocol;
use Message\Services\MessageServie;
use Rpc\Msg\MessageClient;
use Rpc\Msg\MessageProcessor;
use Thrift\Factory\TBinaryProtocolFactory;
use Thrift\Factory\TTransportFactory;
use Thrift\Server\TServerSocket;
use Thrift\Server\TSimpleServer;
use Thrift\Server\TForkingServer;
use Thrift\Transport\TSocket;

server

public function message_rpc()
{
try {
// 初始化多个服务提供者handle
$messageprocessor = new \Rpc\Msg\MessageProcessor(new MessageServie());
// 创建多个服务Processor
$sendProcessor = new \Rpc\Msg\SendMsgProcessor(new \Message\Services\SendMsgServie()); // 将服务注册到TMultiplexedProcessor中
$tFactory = new TTransportFactory();
$pFactory = new TBinaryProtocolFactory(true, true);
$processor = new TMultiplexedProcessor();
// 将服务注册到TMultiplexedProcessor中
//队列消费者rpc请求
$processor->registerProcessor("MessageApiAction", $messageprocessor);
//消息发送rpc请求
$processor->registerProcessor("sendMsg", $sendProcessor); // 初始化数据传输方式transport
// 利用该传输方式初始化数据传输格式protocol // 监听开始
$transport = new TServerSocket('0.0.0.0', ''); // $processor->process($pFactory, $pFactory); $server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory);
$server->serve();
} catch (TException $tx) {
\Think\Log::write($tx->getMessage());
} catch(\Exception $e){
\Think\Log::write($e->getMessage());
}
}

client

public function local()
{
try {
ini_set('memory_limit', '1024M');
$socket = new TSocket('192.168.1.188', '');
$socket->setRecvTimeout();
$socket->setDebug(true);
$transport = new TBufferedTransport($socket, , );
$protocol = new TBinaryProtocol($transport); $client = new MessageClient(new TMultiplexedProtocol($protocol, "MessageApiAction"));
// $client = new MessageClient($protocol);
$transport->open();
$result = $client->MessageApiAction('message api');
print_r($result);
$transport->close();
} catch (TException $tx) {
print_r($tx->getMessage());
} } public function send_msg()
{
try {
ini_set('memory_limit', '1024M');
$socket = new TSocket('192.168.1.188', '');
$socket->setRecvTimeout();
$socket->setDebug(true);
$transport = new TBufferedTransport($socket, , );
$protocol = new TBinaryProtocol($transport); $client = new \Rpc\Msg\SendMsgClient(new TMultiplexedProtocol($protocol, "sendMsg")); // $client = new \Rpc\Msg\SendMsgClient($protocol);
$transport->open();
$result = $client->sendMsg('send msg');
print_r($result);
$transport->close();
} catch (TException $tx) {
print_r($tx->getMessage());
} }

最新文章

  1. jsContext全局函数调用与对象函数调用、evaluateScript
  2. 学霸数据处理项目之数据处理网页以及后台以及C#代码部分开发者手册
  3. [linux系统]--搭建ftp服务器并且 创建用户 设置密码
  4. 安装docker1.10
  5. .NET深入 c#数据类型2
  6. Beta版本冲刺计划
  7. get_magic_quotes_gpc函数
  8. PowerMock使用遇到的问题——2
  9. Effective C# 学习笔记(原则二:为你的常量选择readonly而不是const)
  10. CSS之边框属性
  11. screen printing
  12. Mahout朴素贝叶斯文本分类
  13. 怎樣添加設置GridView,CheckBox全選功能
  14. 浏览器css隐藏滚动条的方法!除了IE一般都支持
  15. 前端ajax请求百度地图api
  16. Nerd的畅销产品
  17. pycharm opencv4.0安装使用
  18. error while loading shared libraries: xxx.so
  19. echarts中国地图坐标弹框
  20. yum 安装指定 kernel 版本源码

热门文章

  1. gstore安装
  2. ubuntu与xshell连接不起来:
  3. NoSQL 列族数据库
  4. Odoo 新 API 概述
  5. JZOJ100048 【NOIP2017提高A组模拟7.14】紧急撤离
  6. mysql基础教程(二)-----分组函数、多表查询、常见函数
  7. JS基础之EL表达式
  8. 玩转vue的slot内容分发
  9. Leetcode162. Find Peak Element寻找峰值
  10. Leetcode405Convert a Number to Hexadecimal数字转换为十六进制数