采用Thrift 的 TNonblockingServer(分別用python cpp java)
在實際業(yè)務(wù)當(dāng)中元咙,thrift更加推薦使用TNonblockingServer(nio線程模型)采用方便不同語言寫成的產(chǎn)品之間通信和對接
我這里略過thrift 的安裝和編譯thrift文件
注意安裝thrift的時候需要
--with-libevent
后邊c++編譯的時候需要libevent和libthrift libthriftnb這三個動態(tài)庫(當(dāng)然你link靜態(tài)庫也是可以的)
java client
private static final Logger LOG = LoggerFactory.getLogger(AIFilterClient.class);
public static void main(String[] args) throws Exception {
TSocket tSocket = new TSocket("localhost", 9090);
TTransport tTransport = new TFramedTransport(tSocket);
TProtocol tProtocol = new TBinaryProtocol(tTransport);
AIFilterService.Client client = new AIFilterService.Client(tProtocol);
tTransport.open();
LOG.info("{}", tTransport);
AIFilterRequest request = new AIFilterRequest( "https://xxxx.jpg",
"xxxx");
AIFilterResponse response = client.invokeAIFilter(request);
LOG.info("resp {}", response);
}
java server
processor里面的handler處理業(yè)務(wù)代碼,自己定義我就不寫出來了
private static final Logger LOG = LoggerFactory.getLogger(AIFilterService.class);
public static void main(String[] args) throws Exception{
LOG.info("Hello World!");
TProcessor processor =
new AIFilterService.Processor<AIFilterService.Iface>(new AlFilterHandler());
TNonblockingServerTransport transport = new TNonblockingServerSocket(9090);
TNonblockingServer.Args tArgs = new TNonblockingServer.Args(transport);
tArgs.processor(processor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TNonblockingServer(tArgs);
server.serve();
}
python client
socket = TSocket.TSocket(host, port)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = AIFilterService.Client(protocol)
transport.open()
python server
handler = AIFilterHandler()
processor = AIFilterService.Processor(handler)
transport = TSocket.TServerSocket(host, port)
server = TNonblockingServer.TNonblockingServer(processor, transport)
c++ client
std::ios::sync_with_stdio(false);
std::cout << "thrift client begin" << std::endl;
stdcxx::shared_ptr<TTransport> socket(new TSocket("localhost", 9000));
stdcxx::shared_ptr<TTransport> transport(new TFramedTransport(socket));
stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
AIFilterServiceClient client(protocol);
//
AIFilterRequest request;
AIFilterResponse response;
request.__set_url(
"https://xxxx.jpg");
request.__set_taskId("xxxxx");
client.invokeAIFilter(response, request);
transport->close();
response.printTo(std::cout);
std::cout << "end" << std::endl;
c++ server
int port = 9000;
::apache::thrift::stdcxx::shared_ptr<AIHandler> handler(new AIHandler());
::apache::thrift::stdcxx::shared_ptr<TProcessor> processor(new AIFilterServiceProcessor(handler));
::apache::thrift::stdcxx::shared_ptr<TNonblockingServerSocket> serverSocket(new TNonblockingServerSocket(port));
::apache::thrift::stdcxx::shared_ptr<server::TNonblockingServer> server(
new TNonblockingServer(processor, serverSocket));
server->serve();
c++ 的 cmakelists
切記一定要 link libthriftnb 和 libevent
cmake_minimum_required(VERSION 3.10)
add_library(AIFILTER SHARED AIFilterService.cpp AIFilterMessage_types.cpp AIFilterMessage_constants.cpp
AIFilterService_server.skeleton.cpp)
#include_directories(${THRIFT_INCLUDE_DIR})
#link_libraries(${THRIFT_LIBRARIES})
message(ON " ${THRIFT_INCLUDE_DIR} || ${THRIFT_LIBRARIES} || ${THRIFTNB_LIBRARIES}")
message(ON " ${LIBEVENT_LIBRARIES} || ${LIBEVENT_INCLUDE_DIRS} ")
target_include_directories(AIFILTER SYSTEM
PUBLIC ${THRIFT_INCLUDE_DIR})
target_link_libraries(AIFILTER ${THRIFT_LIBRARIES} ${THRIFTNB_LIBRARIES}
${LIBEVENT_LIBRARIES})