2011-08-01 31 views
6

我想创建多个服务,并且我想用不同的标识符来使用它们。 所以我的意思是:我想在一个传输中使用多个服务(Thrift)

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

我的意思是我可以添加更多'服务'到xmlrpc上的“处理程序映射”。

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

phm.addHandler("Users", 
      Users.class); 
phm.addHandler("Projects", 
       Projects.class); 

我愿做同样的节俭。

下面是一个简单的例子: 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) 
} 

然后,我创建一个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)); 

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

非常感谢您的帮助。

回答

4

RPC调用通过TMessage结构中没有'targetService'字段的线传输。因此,没有简单的方法将多个服务绑定到单个端口,而无需将此字段添加到TMessage并重新编译节俭。

通过实现与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)); 
     ... 

客户应前缀目标服务字符串中的每个消息。这可以通过自定义协议包装TBinaryProtocol来完成:

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

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

7

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

https://issues.apache.org/jira/browse/THRIFT-563是一个很好的开始。

PS:欢迎审稿人和贡献者;-)

相关问题