【问题标题】:thrift nodejs client and php server works together?thrift nodejs客户端和php服务器一起工作?
【发布时间】:2016-08-06 03:07:57
【问题描述】:

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

我设置了一个nginx服务器来托管localhost:9099,目录为/path/to/my/tutorial/,生成一个服务器脚本为index.php并将其放在根目录下,当访问localhost:9099时,客户端访问服务器通过index.phpindex.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,也没有任何标准输出。

【问题讨论】:

  • 感谢@JensG 删除无用的词~
  • 这是关于 SO(和一般 SE)的一般政策。个人求助的呼喊声和问候语与预期的问答格式不太匹配。所以这与你个人无关。

标签: php node.js nginx rpc thrift


【解决方案1】:

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

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

检查这个: NodeJS Hbase thrift weirdness

【讨论】:

    【解决方案2】:

    最后我的伙伴帮我找到了答案。

    nodejs cient的官方demo有一些问题:

    官方代码:

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

    我们的代码:

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

    删除(),使代码正常工作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2017-07-23
    • 2012-05-23
    • 1970-01-01
    • 2011-01-31
    • 2014-03-16
    相关资源
    最近更新 更多