我想在一个传输上使用多个服务(Thrift)

kmm*_*tkq 7 java thrift handler

我想创建几个服务,我想使用不同的标识符.所以我的意思是:

我有一个用户和项目服务.我想同时使用这些.

我的意思是我可以在xmlrpc上的"handlermap"中添加更多"服务".

http://ws.apache.org/xmlrpc/server.html

phm.addHandler("Users",
             Users.class); 
phm.addHandler("Projects",
               Projects.class);
Run Code Online (Sandbox Code Playgroud)

我想在节俭中做同样的事情.

这是一个简单的例子:test.thrift

typedef i64 UserId

struct Bonk
{
  1: string message,
  2: i32 type
}

struct Insanity
{
  1: map<Bonk, UserId> userMap,
  2: list<Bonk> xtructs
}



service ThriftTest
{
  void         testVoid(),
  string       testString(1: string test),
  byte         testByte(1: byte test),
  i32          testI32(1: i32 test),
  i64          testI64(1: i64 test),
  double       testDouble(1: double test),
  list<map<i32,i32>> testMap(1: map<i32,i32> test),
  map<string,string> testStringMap(1: map<string,string> test),
  set<i32>     testSet(1: set<i32> test),
  map<i32,map<i32,i32>> testMapMap(1: i32 test),
  map<UserId, map<i32,Insanity>> testInsanity(1: Insanity argument)
}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个implementatino,然后将其添加到TServer的实例.

Users.Processor users_proccesor = new Users.Processor(New UsersImpl());
Projects.Processor project_processor = new Projects.Processors(new ProjectsImp());
// I would like to add Users and Projects  
ThriftTest.Processor prc = new ThriftTest.Processor(new ThiftTestImp());
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TSimpleServer(new Args(serverTransport).processor( prc ));
Run Code Online (Sandbox Code Playgroud)

这是我的大问题,我无法添加服务器的多个实例.

提前谢谢你的帮助.

Jen*_*nsG 7

现在正在整合多路复用服务(本质上就是你想要做的).已经有许多语言的补丁可用,已经被接受或正在被审查过程中.

https://issues.apache.org/jira/browse/THRIFT-563是一个很好的起点.

PS:欢迎评论者和贡献;-)


Wil*_*ire 5

RPC 调用在没有“targetService”字段的 TMessage 结构中通过线路传输。因此,如果不将此字段添加到 TMessage 并重新编译 thrift,就没有直接的方法将多个服务绑定到单个端口。

可以通过实现类似于 TSimpleSever(或任何其他 TServer)的自定义 TServer 来进行黑客攻击。

服务器应该在循环中读取目标服务并获取相应的处理器:

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

客户端应使用目标服务字符串作为每条消息的前缀。这可以通过将 TBinaryProtocol 包装在自定义协议中来完成:

public void writeMessageBegin(TMessage message) throws TException {
    wrapped.writeString(target);
    wrapped.writeMessageBegin(message);
}
Run Code Online (Sandbox Code Playgroud)

这种方法的主要缺点是失去了与其他客户端的互操作性。因此,最好在不同的端口上启动两个不同的 TServer,或者在单个 thrift 服务中定义所有方法,然后将调用委托给适当的处理程序。