以下是一個 使用多路復用(TMultiplexedProtocol) 的 Thrift 客戶端完整流程和關鍵函數(以 Java 為例),適用于當服務端使用 TMultiplexedProcessor 注冊了多個服務時,客戶端可以區分并調用不同的服務。
? 客戶端整體流程(使用 TMultiplexedProtocol)
1. 定義多個服務(以兩個為例)
// example1.thrift
namespace java exampleservice ExampleService1 {string sayHello(1:string name)
}
// example2.thrift
namespace java exampleservice ExampleService2 {i32 add(1:i32 a, 2:i32 b)
}
使用 Thrift 編譯器生成 Java 代碼:
thrift --gen java example1.thrift
thrift --gen java example2.thrift
2. 服務端使用多路復用(服務端部分簡略)
TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();multiplexedProcessor.registerProcessor("ExampleService1", new ExampleService1.Processor<>(new ExampleService1Impl()));
multiplexedProcessor.registerProcessor("ExampleService2", new ExampleService2.Processor<>(new ExampleService2Impl()));TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(multiplexedProcessor));
server.serve();
3. 客戶端實現(核心)
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
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 {// 1. 打開傳輸層連接transport = new TSocket("localhost", 9090);transport.open();// 2. 創建基礎協議TProtocol baseProtocol = new TBinaryProtocol(transport);// 3. 使用多路復用協議(針對不同服務)TMultiplexedProtocol protocol1 = new TMultiplexedProtocol(baseProtocol, "ExampleService1");TMultiplexedProtocol protocol2 = new TMultiplexedProtocol(baseProtocol, "ExampleService2");// 4. 創建客戶端 Stub(代理)ExampleService1.Client client1 = new ExampleService1.Client(protocol1);ExampleService2.Client client2 = new ExampleService2.Client(protocol2);// 5. 調用服務方法String greeting = client1.sayHello("Alice");int result = client2.add(5, 7);System.out.println("Service1 response: " + greeting);System.out.println("Service2 response: " + result);} catch (Exception e) {e.printStackTrace();} finally {// 6. 關閉連接if (transport != null) {transport.close();}}}
}
4. C++ 版本:客戶端實現
客戶端代碼(C++)
#include
#include
#include
#include #include "ExampleService1.h"
#include "ExampleService2.h"using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;int main() {try {// 客戶端連接std::shared_ptr socket(new TSocket("localhost", 9090));std::shared_ptr transport(new TBufferedTransport(socket));std::shared_ptr protocol(new TBinaryProtocol(transport));transport->open();// 使用多路復用協議TMultiplexedProtocol protocol1(protocol, "ExampleService1");TMultiplexedProtocol protocol2(protocol, "ExampleService2");// 創建客戶端ExampleService1Client client1(protocol1);ExampleService2Client client2(protocol2);// 調用服務std::string greeting;client1.sayHello(greeting, "Alice");std::cout << "Response from ExampleService1: " << greeting << std::endl;int result;client2.add(result, 5, 7);std::cout << "Response from ExampleService2: " << result << std::endl;transport->close();} catch (const std::exception& e) {std::cerr << "Error: " << e.what() << std::endl;}return 0;
}
🔍 客戶端關鍵類和函數說明
類 / 函數 | 作用說明 |
TSocket(host, port) | 創建客戶端傳輸連接(基于 TCP) |
transport.open() | 打開連接 |
TBinaryProtocol | 使用二進制協議進行編碼 |
TMultiplexedProtocol | 多路復用協議,用于標識服務名稱 |
ExampleService1.Client | 客戶端代理,調用服務方法 |
client1.sayHello("Alice") | 實際調用遠程服務端方法 |
transport.close() | 關閉連接 |
? 小提示
- 多路復用的關鍵是:
- 服務端使用 TMultiplexedProcessor 注冊服務(帶名字);
- 客戶端用 TMultiplexedProtocol 創建不同服務的協議實例(帶同樣的名字)。