背景:公司要用Node与其他语言(Java)写的服务通信。
1,服务端 helloServer.jsvar thrift = require('thrift');var helloService = require('./HelloService');var server = thrift.createServer(helloService, { hello: function(para, success){ console.log("para: " + para); success(null, "Hi, Client! I am Server!"); }}, {});server.listen(8080);2,客户端 helloClient.js
var thrift = require('thrift');var helloService = require('./HelloService');//创建连接和客户端var connection = thrift.createConnection('localhost', 8080);connection.on('error', function(err) { console.error(err);});var client = thrift.createClient(helloService, connection);//调用hello方法var para = 'Hi Server! I am Client.';client.hello(para, function(err, res){ if(err){ console.error("Error: " + err); }else{ console.log("Result: " + res); } connection.end();});
3,thrift compiler version: 0.9.2.
4,此版本的node第三方库thrift的server.js文件有个bug,导致thrift.createServer(processor, handler, options)的第三个参数必须得传。 截图红框中的代码应该为:if (options && options.tls) {[
connection.domain: object
connection._events: object
connection._maxListeners: undefined
connection.seqId2Service: object
connection.connection: object
connection.options: object
connection.transport: function
connection.protocol: function
connection.offline_queue: object
connection.connected: boolean
connection._debug: boolean
connection.retry_max_delay: object
connection.connect_timeout: boolean
connection.host: string
connection.port: number
connection.end: function
connection.initialize_retry_vars: function
connection.write: function
connection.connection_gone: function
connection.setMaxListeners: function
connection.emit: function
connection.addListener: function
connection.on: function
connection.once: function
connection.removeListener: function
connection.removeAllListeners: function
connection.listeners: function
]
[
thrift.Thrift: object
thrift.Connection: function
thrift.createClient: function
thrift.createConnection: function
thrift.createSSLConnection: function
thrift.createStdIOClient: function
thrift.createStdIOConnection: function
thrift.HttpConnection: function
thrift.createHttpConnection: function
thrift.createHttpClient: function
thrift.createServer: function
thrift.createMultiplexServer: function
thrift.createWebServer: function
thrift.Int64: function
thrift.Q: function
thrift.Multiplexer: function
thrift.MultiplexedProcessor: function
thrift.TFramedTransport: function
thrift.TBufferedTransport: function
thrift.TBinaryProtocol: function
thrift.TJSONProtocol: function
thrift.TCompactProtocol: function
]