I'd like to use multiple services on one transport ( Thrift )

て烟熏妆下的殇ゞ 提交于 2019-12-05 05:44:34

Multiplexed Services (in essence that's what you want to do here) are being integrated right now. There are already patches for a number of languages available, either already accepted or in the process of being reviewed.

https://issues.apache.org/jira/browse/THRIFT-563 is a good place to start.

PS: Reviewers and contributions are welcome ;-)

RPC invocation is transmitted over the wire in TMessage structure that doesn't have 'targetService' field. So there's no straightforward way to bind several services to a single port without adding this field to TMessage and recompiling thrift.

It's possible to do a hack by implementing custom TServer similar to TSimpleSever (or any other TServer).

Server should read target service in the loop and get corresponding processor:

      ...
      inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
      outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
      do {
        String target = inputProtocol.readString();
        processor = processorFactoryMap.get(target).getProcessor(client);
      while (processor.process(inputProtocol, outputProtocol));
      ...

Client should prefix each message with target service string. This can be done by wrapping TBinaryProtocol in a custom protocol:

public void writeMessageBegin(TMessage message) throws TException {
    wrapped.writeString(target);
    wrapped.writeMessageBegin(message);
}

The main disadvantage of this approach is losing interoperability with other clients. So, probably it's better either to start two different TServers on different ports, or define all methods in a single thrift service, and then delegate invocations to appropriate handlers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!