Uber の Tchannel の学習を始めたばかりです。Python と nodejs で tchannel ドキュメントからコードを実行しようとしています。どちらの場合も、クライアントをサーバーに接続できません。
これは、 http : //tchannel-node.readthedocs.org/en/latest/GUIDE/ から従った nodejs での私のコードの外観です。
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)
誰が間違いを教えてもらえますか?