我想在一种传输上使用多种服务 (Thrift)
我想创建多个服务,并且希望将它们与不同的标识符一起使用。 所以我的意思是:
我有一个用户和项目服务。 我想同时使用这些。
我的意思是我可以向 xmlrpc 上的“handlermap”添加更多“服务”。
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 ));
这是我的大问题,我无法添加服务器的多个实例。
提前感谢您的帮助。
I'd like to create several services, and I want to use them with different identifiers.
So I mean :
I've got a Users and Projects service .
I want to use these at the same time.
I mean I can add more 'services' to the "handlermap" on xmlrpc.
http://ws.apache.org/xmlrpc/server.html
phm.addHandler("Users",
Users.class);
phm.addHandler("Projects",
Projects.class);
I would like to do the same in the thrift.
Here is a simple example :
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)
}
Then I create an implementatino, then add it to the instance of 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 ));
And here's my big problem, I can't add multiple instances of the server.
Thank you for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
多路复用服务(本质上这就是您想要在这里做的)现在正在集成。已经有多种语言可用的补丁,要么已经被接受,要么正在审查过程中。
https://issues.apache.org/jira/browse/THRIFT-563是一个很好的起点。
PS:欢迎审稿人和贡献;-)
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 调用通过没有“targetService”字段的 TMessage 结构进行传输。因此,如果不将此字段添加到 TMessage 并重新编译 thrift,就没有直接的方法可以将多个服务绑定到单个端口。
可以通过实现类似于 TSimpleSever(或任何其他 TServer)的自定义 TServer 来进行黑客攻击。
服务器应在循环中读取目标服务并获取相应的处理器:
客户端应在每条消息前添加目标服务字符串。这可以通过将 TBinaryProtocol 包装在自定义协议中来完成:
这种方法的主要缺点是失去与其他客户端的互操作性。因此,最好在不同的端口上启动两个不同的 TServer,或者在单个 Thrift 服务中定义所有方法,然后将调用委托给适当的处理程序。
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:
Client should prefix each message with target service string. This can be done by wrapping TBinaryProtocol in a custom protocol:
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.