2015-09-27 62 views
2

我剛開始學習Uber的Tchannel。我試圖從python和nodejs中的tchannel文檔運行代碼。在這兩種情況下,我都無法將客戶端連接到服務器。Tchannel服務器代碼不起作用。 (Python和nodejs)

這是我的代碼看起來像在的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鏈接。 代碼蟒蛇看起來是這樣的:

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) 

有誰能告訴我什麼是錯誤?

回答

1

對於節點示例,指南中的節儉文件需要更新。嘗試使用以下(我剛剛添加每個字段所需的關鍵字):

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

謝謝莊。現在正在工作。 – Techie