public function demoThriftClient() { static $request_count = 0; try { $host = '127.0.0.1'; $port = 8091; $email = '*****@*****.**'; $info = ['email' => $email, 'name' => '张三', 'userId' => 222]; print "connect {$host}:{$port}..." . $request_count++ . "\n\n"; $socket = new TSocket($host, $port); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new AccountClient($protocol); $transport->open(); $newAccount = new \Demo\AccountInfo(); $ret = $client->setUserInfo($newAccount); // var_dump($ret); $accountInfo = $client->getUserInfoByEmail($email); // var_dump($accountInfo); $transport->close(); return response()->json($accountInfo); } catch (TException $e) { print 'TException: ' . $e->getMessage() . "\n"; } //return view('welcome'); }
/** * Execute the console command. * * @return mixed */ public function handle() { //1万次请求测试 $choice = $this->choice('select call thrift rpc times: ', ['1 calls' => 1, '100 calls' => 100, '1000 calls' => 1000, '10000 calls' => 10000], 1); $i = abs($choice); while ($i-- > 0) { try { $host = '127.0.0.1'; $port = 8091; $email = '*****@*****.**'; $info = ['email' => $email, 'name' => '张三', 'userId' => 222]; print "connect {$host}:{$port}...\n\n"; $socket = new TSocket($host, $port); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new AccountClient($protocol); $transport->open(); $newAccount = new \Demo\AccountInfo($info); $ret = $client->setUserInfo($newAccount); var_dump($ret); $accountInfo = $client->getUserInfoByEmail($email); print_r($accountInfo); $transport->close(); } catch (TException $e) { print 'TException: ' . $e->getMessage() . "\n"; } } }
} use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Exception\TException; use Demo\AccountClient; try { if (array_search('--http', $argv)) { $host = 'localhost'; $port = 9090; $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); } else { $host = '192.168.1.109'; $port = 9090; print "connect {$host}:{$port}...\n\n"; $socket = new TSocket($host, $port); } $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new AccountClient($protocol); $transport->open(); $newAccount = new \Demo\AccountInfo($info = ['email' => $email, 'name' => 'xiao san', 'userId' => 222]); $ret = $client->setUserInfo($newAccount); var_dump($ret); $accountInfo = $client->getUserInfoByEmail('*****@*****.**'); print_r($accountInfo); $transport->close(); } catch (TException $e) { print 'TException: ' . $e->getMessage() . "\n"; }