• php thrift TServerSocket实现端口复用


    <?php
    namespace MessageController;
    use ThinkController;
    use ThriftExceptionTException;
    use ThriftProtocolTBinaryProtocol;
    use ThriftTransportTBufferedTransport;
    use ThriftTransportTHttpClient;
    use ThriftTransportTPhpStream;
    use ThriftTMultiplexedProcessor;
    use ThriftProtocolTMultiplexedProtocol;
    use MessageServicesMessageServie;
    use RpcMsgMessageClient;
    use RpcMsgMessageProcessor;
    use ThriftFactoryTBinaryProtocolFactory;
    use ThriftFactoryTTransportFactory;
    use ThriftServerTServerSocket;
    use ThriftServerTSimpleServer;
    use ThriftServerTForkingServer;
    use ThriftTransportTSocket;

    server

    public function message_rpc()
        {
            try {
                // 初始化多个服务提供者handle
                $messageprocessor = new RpcMsgMessageProcessor(new MessageServie());
                // 创建多个服务Processor
                $sendProcessor = new RpcMsgSendMsgProcessor(new MessageServicesSendMsgServie());
    
                // 将服务注册到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', '9988');
    
    
    //            $processor->process($pFactory, $pFactory);
    
    
                $server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory);
                $server->serve();
            } catch (TException $tx) {
                ThinkLog::write($tx->getMessage());
            } catch(Exception $e){
                ThinkLog::write($e->getMessage());
            }
        }

    client

    public function local()
        {
            try {
                ini_set('memory_limit', '1024M');
                $socket = new TSocket('192.168.1.188', '9988');
                $socket->setRecvTimeout(50000);
                $socket->setDebug(true);
                $transport = new TBufferedTransport($socket, 1024, 1024);
                $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', '9988');
                $socket->setRecvTimeout(50000);
                $socket->setDebug(true);
                $transport = new TBufferedTransport($socket, 1024, 1024);
                $protocol = new TBinaryProtocol($transport);
    
    
                $client = new  RpcMsgSendMsgClient(new TMultiplexedProtocol($protocol, "sendMsg"));
    
    //            $client = new RpcMsgSendMsgClient($protocol);
                $transport->open();
                $result = $client->sendMsg('send msg');
                print_r($result);
                $transport->close();
            } catch (TException $tx) {
                print_r($tx->getMessage());
            }
    
        }
  • 相关阅读:
    dwz tabs table实现翻页及各tabs查询
    DruidDataSource配置
    利用blob对象实现大文件分片上传
    HTML5 File API 全介绍
    JS获取当前网页内容,创建文件并下载,URL.createObjectURL和URL.revokeObjectURL
    使用 CSS 接收用户的点击事情并对相关节点进行操作
    Flex布局
    background: inherit制作倒影、单行居中两行居左超过两行省略
    层叠顺序与堆栈上下文、font-family字体定义顺序的
    简单使用GA监控网站浏览行为
  • 原文地址:https://www.cnblogs.com/sunlong88/p/11016277.html
Copyright © 2020-2023  润新知