Thrift 多路复用服务端示例
1. 服务端代码:Java 版本
定义多个服务接口
首先,定义多个服务接口,并保存为 .thrift 文件。
example1.thrift
namespace java example
service ExampleService1 {
string sayHello(1:string name)
}
example2.thrift
namespace java example
service ExampleService2 {
i32 add(1:i32 a, 2:i32 b)
}
# 生成 Java 代码:
thrift --gen java example1.thrift
thrift --gen java example2.thrift
实现服务端
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.protocol.TMultiplexedProcessor;
import example.ExampleService1;
import example.ExampleService2;
public class ThriftServer {
public static void main(String[] args) {
try {
// 实现服务接口
ExampleService1Impl handler1 = new ExampleService1Impl();
ExampleService2Impl handler2 = new ExampleService2Impl();
// 多路复用处理器
TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();
// 注册服务
multiplexedProcessor.registerProcessor("ExampleService1", new ExampleService1.Processor<>(handler1));
multiplexedProcessor.registerProcessor("ExampleService2", new ExampleService2.Processor<>(handler2));
// 创建服务器传输
TServerTransport serverTransport = new TServerSocket(9090);
// 启动多线程服务器
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(multiplexedProcessor));
System.out.println("Server started...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
服务实现类:
public class ExampleService1Impl implements ExampleService1.Iface {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
public class ExampleService2Impl implements ExampleService2.Iface {
@Override
public int add(int a, int b) {
return a + b;
}
}
2. 客户端代码:Java 版本
客户端实现(连接多个服务并调用方法):
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import example.ExampleService1;
import example.ExampleService2;
public class ThriftClient {
public static void main(String[] args) {
TTransport transport = null;
try {
transport = new TSocket("localhost", 9090);
transport.open();
// 创建基础协议
TBinaryProtocol baseProtocol = new TBinaryProtocol(transport);
// 多路复用协议
TMultiplexedProtocol protocol1 = new TMultiplexedProtocol(baseProtocol, "ExampleService1");
TMultiplexedProtocol protocol2 = new TMultiplexedProtocol(baseProtocol, "ExampleService2");
// 创建客户端
ExampleService1.Client client1 = new ExampleService1.Client(protocol1);
ExampleService2.Client client2 = new ExampleService2.Client(protocol2);
// 调用方法
String greeting = client1.sayHello("Alice");
int result = client2.add(5, 7);
System.out.println("Response from ExampleService1: " + greeting);
System.out.println("Response from ExampleService2: " + result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (transport != null) {
transport.close();
}
}
}
}
3. C++ 版本:服务端实现
定义 Thrift 接口(C++)
使用和 Java 一样的 example1.thrift 和 example2.thrift 文件。
生成 C++ 代码
thrift --gen cpp example1.thrift thrift --gen cpp example2.thrift
服务端实现
#include
#include
#include
#include
#include
#include "ExampleService1.h"
#include "ExampleService2.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using namespace example;
class ExampleService1Handler : public ExampleService1If {
public:
void sayHello(std::string& _return, const std::string& name) override {
_return = "Hello, " + name;
}
};
class ExampleService2Handler : public ExampleService2If {
public:
void add(int32_t& _return, const int32_t a, const int32_t b) override {
_return = a + b;
}
};
int main() {
using namespace apache::thrift::server;
using namespace apache::thrift::transport;
using namespace apache::thrift::protocol;
try {
// 服务实现
std::shared_ptr handler1 = std::make_shared();
std::shared_ptr handler2 = std::make_shared();
// 创建多路复用处理器
TMultiplexedProcessor multiplexedProcessor;
multiplexedProcessor.registerProcessor("ExampleService1", std::make_shared(handler1));
multiplexedProcessor.registerProcessor("ExampleService2", std::make_shared(handler2));
// 传输层和协议
std::shared_ptr serverSocket(new TServerSocket(9090));
std::shared_ptr transportFactory(new TBufferedTransportFactory());
std::shared_ptr protocolFactory(new TBinaryProtocolFactory());
// 启动服务
TThreadPoolServer server(multiplexedProcessor, serverSocket, transportFactory, protocolFactory);
std::cout << "Server started..." << std::endl;
server.serve();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
总结
- 服务端实现:
- 在服务端使用 TMultiplexedProcessor 注册多个服务,每个服务都需要一个处理器。
- 每个服务的协议都被 TMultiplexedProtocol 包装,以便能够进行多路复用。
- 客户端实现:
- 客户端使用 TMultiplexedProtocol 来区分调用不同的服务,并与服务端的 TMultiplexedProcessor 进行匹配。