您的位置:首页 > 其它

Thrift 实现一个端口注册多个服务

2016-01-25 00:00 295 查看
摘要: Thrift多路复用的使用

thrift 文件

struct User{
1:i32 id;
2:string name,
3:string password
}

service Service1{
User getUser(),
void setUser(1:i32 id)
}

service Service2{
void test()
}

更具.thrift文件生成相应接口然后实现

然后再编写服务器端,客户端。

Java版本

Server

public class ThriftServer {
public static void main(String args[]) {
try {

TServerTransport serverTransport = new TServerSocket(9090);

TMultiplexedProcessor processor = new TMultiplexedProcessor();

//注册一个服务接口
Service1.Processor<Service1Impl> pro =
new Service1.Processor<Service1Impl>(new Service1Impl());
processor.registerProcessor("service1", pro);

//注册一个服务接口
Service2.Processor<Service2Impl> test =
new Service2.Processor<Service2Impl>(new Service2Impl());
processor.registerProcessor("service2", test);

TServer server = new TThreadPoolServer(new Args(serverTransport).processor(processor));
server.serve();//启动服务

} catch (Exception x) {
x.printStackTrace();
}
System.out.println("done.");
}
}


Client

public class ThriftClient {
public static void main(String[] args) {
/*
* if (args.length != 1) { System.out.println("Please enter 'simple' or 'secure'");
* System.exit(0); }
*/

try {
TTransport transport;
// if (args[0].contains("simple")) {
transport = new TSocket("127.0.0.1", 9090);

transport.open();

// }

// else {
/*
* Similar to the server, you can use the parameters to setup client parameters or use
* the default settings. On the client side, you will need a TrustStore which contains
* the trusted certificate along with the public key. For this example it's a
* self-signed cert.
*/

// TSSLTransportParameters params = new TSSLTransportParameters();
// params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS");
/*
* Get a client transport instead of a server transport. The connection is opened on
* invocation of the factory method, no need to specifically call open()
*/

// transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params);

// }

TBinaryProtocol protocol = new TBinaryProtocol(transport);

TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol,"service1");
Service1.Client client1= new Service1.Client(mp1);
System.out.println(client1.getUser().getName());

TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol,"service2");
Service2.Client client2= new Service2.Client(mp2);
client2.test();

transport.close();

} catch (TException x) {
x.printStackTrace();
}
}
}


C++ 版本

Server

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

#include <thrift/processor/TMultiplexedProcessor.h>
#include <thrift/protocol/TMultiplexedProtocol.h>
#include <thrift/protocol/TProtocolDecorator.h>
#include <thrift/processor/TMultiplexedProcessor.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace boost;

int main(int argc, char **argv) {

int port = 9090;
//使用MultiplexedProcessor
shared_ptr<TMultiplexedProcessor> processor(new TMultiplexedProcessor());

shared_ptr<TProcessor> processor1(new Service1Processor
(shared_ptr<Service1Handler>(new Service1Handler())));

shared_ptr<TProcessor> processor2(new Service2Processor
(shared_ptr<Service2Handler>(new Service2Handler())));

//注册各自的Service
processor->registerProcessor("Service1", processor1);
processor->registerProcessor("Service2", processor2);

shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();

return 0;
}


Client

#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include <processor/TMultiplexedProcessor.h>
#include <protocol/TMultiplexedProtocol.h>
#include <protocol/TProtocolDecorator.h>
#include <processor/TMultiplexedProcessor.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace boost;

int main(int argc, char** argv){
shared_ptr<TSocket> transport(new TSocket("localhost", 9090));
shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));

shared_ptr<TMultiplexedProtocol> mp1(new TMultiplexedProtocol(protocol, "Service1"));
shared_ptr<Service1Client> service1(new Service1Client(mp1));

shared_ptr<TMultiplexedProtocol> mp2(new TMultiplexedProtocol(protocol, "Service2"));
shared_ptr<Service2Client> service2(new Service2Client(mp2));

try {
transport->open();

//调用服务1的接口
service1->setUser(123);

//调用服务2的接口
service2->test();

transport->close();
} catch (TException &tx) {
printf("ERROR: %s\n", tx.what());
}

}

demo代码在下面:

http://www.oschina.net/code/list_by_user?id=1403215
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: