Tchannel服务器代码不工作。 (Python和nodejs)

我刚开始学习Uber的Tchannel。 我试图从python和nodejs的tchannel文档运行代码。 在这两种情况下,我都无法将客户端连接到服务器。

这是我的代码看起来像nodejs,我从http://tchannel-node.readthedocs.org/en/latest/GUIDE/ :

var TChannel = require('tchannel'); var myLocalIp = require('my-local-ip'); var rootChannel = TChannel(); rootChannel.listen(0,myLocalIp()); rootChannel.on('listening', function onListen() { console.log('got a server', rootChannel.address()); }); var TChannelThrift = rootChannel.TChannelAsThrift; var keyChan = rootChannel.makeSubChannel({ serviceName: process.env.USER || 'keyvalue' }); var fs = require('fs'); var keyThrift = TChannelThrift({ source: fs.readFileSync('./keyvalue.thrift', 'utf8') }); var ctx = { store: {} }; keyThrift.register(keyChan, 'KeyValue::get_v1', ctx, get); keyThrift.register(keyChan, 'KeyValue::put_v1', ctx, put); function get(context, req, head, body, cb) { cb(null, { ok: true, body: { value: context.store[body.key] } }); } function put(context, req, head, body, cb) { context.store[body.key] = body.value; cb(null, { ok: true, body: null }); } 

当我运行这个代码时,我得到这个错误:

 node sever.js assert.js:93 throw new assert.AssertionError({ ^ AssertionError: every field must be marked optional, required, or have a default value on GetResult including "value" in strict mode at ThriftStruct.link (/home/bhaskar/node_modules/thriftrw/struct.js:154:13) at Thrift.link (/home/bhaskar/node_modules/thriftrw/thrift.js:199:32) at new Thrift (/home/bhaskar/node_modules/thriftrw/thrift.js:69:10) at new TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:46:17) at TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:38:16) at Object.<anonymous> (/home/bhaskar/uber/tchannel/thrift/sever.js:16:17) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 

类似的,我也尝试了在python遵循http://tchannel.readthedocs.org/projects/tchannel-python/en/latest/guide.html链接相同的东西。 python中的代码如下所示:

 from __future__ import absolute_import from tornado import ioloop from tornado import gen from service import KeyValue from tchannel import TChannel tchannel = TChannel('keyvalue-server') values={} @tchannel.thrift.register(KeyValue) def getValue(request): key = request.body.key value = values.get(key) if value is None: raise KeyValue.NotFoundError(key) return value @tchannel.thrift.register(KeyValue) def setValue(request): key = request.body.key value = request.body.value values[key] = value def run(): tchannel.listen() print('Listening on %s' % tchannel.hostport) if __name__ == '__main__': run() ioloop.IOLoop.current().start() 

当我运行这个与python server.py命令我听到我的本地IP:58092

`但是当我尝试使用tcurl将其与客户端连接为:

 tcurl -p localhost:58092 -t ~/keyvalue/thrift/service.thrift service KeyValue::setValue -3 '{"key": "hello", "value": "world"}' 

我得到这个:

 assert.js:93 throw new assert.AssertionError({ ^ AssertionError: every field must be marked optional, required, or have a default value on NotFoundError including "key" in strict mode at ThriftException.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/struct.js:154:13) at Thrift.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:199:32) at new Thrift (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:69:10) at new TChannelAsThrift (/usr/lib/node_modules/tcurl/node_modules/tchannel/as/thrift.js:46:17) at asThrift (/usr/lib/node_modules/tcurl/index.js:324:18) at onIdentified (/usr/lib/node_modules/tcurl/index.js:278:13) at finish (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:266:9) at Array.onIdentified [as 1] (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:257:9) at DefinedEvent.emit (/usr/lib/node_modules/tcurl/node_modules/tchannel/lib/event_emitter.js:90:25) at TChannelConnection.onOutIdentified (/usr/lib/node_modules/tcurl/node_modules/tchannel/connection.js:383:26) 

谁能告诉我什么是错误?

对于节点示例,需要更新指南中的节俭文件。 尝试使用以下(我只是为每个字段添加一个必需的关键字):

 struct GetResult { 1: required string value } service KeyValue { GetResult get_v1( 1: required string key ) void put_v1( 1: required string key, 2: required string value ) }