thrift nodejs客户端和php服务器一起工作?

时间:2016-04-14 13:50:22

标签: php node.js nginx rpc thrift

我打算使用nodejs作为客户端,将php作为服务器。我使用官方tutorial.thrift

我设置了一个nginx服务器来托管localhost:9099和dir /path/to/my/tutorial/,生成一个服务器脚本作为index.php并将其放在root中,当访问localhost:9099时,以便客户端通过index.php访问服务器。 index.php内容:

<?php

error_reporting(E_ALL);

require_once __DIR__ . "/Thrift/ClassLoader/ThriftClassLoader.php";
use Thrift\ClassLoader\ThriftClassLoader;

$ROOT = realpath(dirname(__FILE__));
$GEN_DIR = $ROOT.'/gen-php';

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $ROOT);
$loader->registerNamespace('Swoole', $ROOT);
$loader->registerNamespace('tutorial', $GEN_DIR);
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$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\Server\TServer;

header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') {
    echo "\r\n";
}

$handler = new \tutorial\Handler();
$processor = new \tutorial\CalculatorProcessor($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();

client.node.js内容:

/**
 * Created by Kron on 16/4/12.
 */
var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var Calculator = require('../gen-nodejs/Calculator');
var ttypes = require('../gen-nodejs/tutorial_types');

transport = ThriftTransports.TBufferedTransport();
protocol = ThriftProtocols.TBinaryProtocol();


var connection = thrift.createConnection("localhost", 9099, {
    transport : transport,
    protocol : protocol
});

var client = thrift.createClient(Calculator, connection);


connection.on('error', function(err) {
    console.log(err);
    //assert(false, err);
});

// Create a Calculator client with the connection

client.ping(function(err, response) {
    console.log('ping()');
});

client.add(1,1, function(err, response) {
    console.log("1+1=" + response);
});

work = new ttypes.Work();
work.op = ttypes.Operation.DIVIDE;
work.num1 = 1;
work.num2 = 0;

client.calculate(1, work, function(err, message) {
    if (err) {
        console.log("InvalidOperation " + err);
    } else {
        console.log('Whoa? You know how to divide by zero?');
    }
});

work.op = ttypes.Operation.SUBTRACT;
work.num1 = 15;
work.num2 = 10;

client.calculate(1, work, function(err, message) {
    console.log('15-10=' + message);

    client.getStruct(1, function(err, message){
        console.log('Check log: ' + message.value);

        //close the connection once we're done
        connection.end();
    });
});

client.php内容:

<?php

error_reporting(E_ALL);

require_once __DIR__ . "/../Thrift/ClassLoader/ThriftClassLoader.php";
use Thrift\ClassLoader\ThriftClassLoader;

$ROOT = realpath(dirname(__FILE__).'/../');
$GEN_DIR = $ROOT.'/gen-php';

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $ROOT);
$loader->registerNamespace('Swoole', $ROOT);
$loader->registerNamespace('tutorial', $GEN_DIR);
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$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', 9099, '/clients_servers/tutorial.server.php');
    } else {
        $socket = new THttpClient('localhost', 9099); // 8080 for node server, 9099 for php server
    }

    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);
    $client = new \tutorial\CalculatorClient($protocol);

    $transport->open();

    $client->ping();
    print "ping()\n";

    $sum = $client->add(1,1);
    print "1+1=$sum\n";

    $work = new \tutorial\Work();

    $work->op = \tutorial\Operation::DIVIDE;
    $work->num1 = 1;
    $work->num2 = 0;

    try {
        $client->calculate(1, $work);
        print "Whoa! We can divide by zero?\n";
    } catch (\tutorial\InvalidOperation $io) {
        print "InvalidOperation: $io->why\n";
    }

    $work->op = \tutorial\Operation::SUBTRACT;
    $work->num1 = 15;
    $work->num2 = 10;
    $diff = $client->calculate(1, $work);
    print "15-10=$diff\n";

    $log = $client->getStruct(1);
    print "Log: $log->value\n";

    $transport->close();

} catch (TException $tx) {
    print 'TException: '.$tx->getMessage()."\n";
}

它们几乎和原版一样。

  1. 当我使用client.php访问主持人时,它运作良好。
  2. 当我使用client.node.js访问主持人时,它无法正常工作。
  3. 当我尝试使用节点服务器而不是主机时,请使用client.php访问它,我必须将$socket = new THttpClient('localhost', 9099)更改为$socket = new TSocket('localhost', 9099)才能使用它工作。
  4. client.node.js从未碰过localhost:9099,没有任何标准记。

2 个答案:

答案 0 :(得分:1)

最后,我的搭档帮我找到了答案。

nodejs cient的官方演示有一些问题:

官方代码:

transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();

我们的代码:

transport = ThriftTransports.TFramedTransport;
protocol = ThriftProtocols.TBinaryProtocol;

删除(),这会使代码生效。

答案 1 :(得分:0)

[hbase-dir] /bin/hbase-daemon.sh start thrift -f

我面临同样的错误。参考http://dailyjs.com/post/hbase TFramedTransport默认是禁用的。您必须使用上面的命令启用它。 注意:启用后,您无法连接php。我在php中使用不同的协议

检查一下: NodeJS Hbase thrift weirdness