• 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());
            }
    
        }
  • 相关阅读:
    JSON特殊字符的处理
    java中高并发和高响应解决方法
    对redis深入理解
    对java中arraylist深入理解
    Redis的字典扩容与ConcurrentHashMap的扩容策略比较
    PHP压缩上传图片
    windows 平台 php_Imagick 拓展遇到的那些坑!
    windows7下php5.4成功安装imageMagick,及解决php imagick常见错误问题。(phpinfo中显示不出来是因为:1.imagick软件本身、php本身、php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个文件放到/php/目录下面)
    php使用imagick模块实现图片缩放、裁剪、压缩示例
    文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
  • 原文地址:https://www.cnblogs.com/sunlong88/p/11016277.html
Copyright © 2020-2023  润新知