Skip to main content
 首页 » 编程设计

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

2025年05月04日42Terrylee

我刚刚开始学习 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) 

类似地,我也通过 http://tchannel.readthedocs.org/projects/tchannel-python/en/latest/guide.html 链接在 python 中尝试了同样的事情。 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 命令运行它时,我得到` Listening on my-local-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) 

谁能告诉我错在哪里?

请您参考如下方法:

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

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 
    ) 
}