我计划使用nodejs作为客户端,使用php作为服务器。我使用官方的tutorial.thrift。
我设置了一个nginx服务器来使用dir /path/to/my/tutorial/
来承载/path/to/my/tutorial/
,生成一个服务器脚本作为index.php,并在访问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";
}
它们和原版差不多。
client.php
访问主机时,它工作得很好。client.node.js
访问主机时,它不起作用。client.php
访问它,我必须将$socket = new THttpClient('localhost', 9099)
更改为$socket = new TSocket('localhost', 9099)
以使其工作。client.node.js
从来没有碰过localhost:9099
,而且没有任何标准。发布于 2016-04-15 00:30:47
最后我的搭档帮我找到了答案。
nodejs的官方演示有一些问题:
官方代码:
transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();
我们的代码:
transport = ThriftTransports.TFramedTransport;
protocol = ThriftProtocols.TBinaryProtocol;
删除()
,这将使代码工作。
发布于 2017-03-09 10:20:51
hbase/bin/HBASE-daemon.sh启动节俭-f
我也面临着同样的错误。默认情况下,http://dailyjs.com/post/hbase TFramedTransport是禁用的。您必须使用上面的命令来启用它。注意:启用之后,您无法连接php。我在php中使用不同的协议。
检查这个:NodeJS Hbase节俭怪异
https://stackoverflow.com/questions/36625190
复制