Thrift Server 的TNonblockingServer

采用Thrift 的 TNonblockingServer(分别用python cpp java)

在实际业务当中,thrift更加推荐使用TNonblockingServer(nio线程模型)采用方便不同语言写成的产品之间通信和对接

我这里略过thrift 的安装和编译thrift文件

注意安装thrift的时候需要

--with-libevent

后边c++编译的时候需要libevent和libthrift libthriftnb这三个动态库(当然你link静态库也是可以的)

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处理业务代码,自己定义我就不写出来了

 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(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 socket(new TSocket("localhost", 9000));
    stdcxx::shared_ptr transport(new TFramedTransport(socket));
    stdcxx::shared_ptr 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 handler(new AIHandler());
    ::apache::thrift::stdcxx::shared_ptr processor(new AIFilterServiceProcessor(handler));
    ::apache::thrift::stdcxx::shared_ptr serverSocket(new TNonblockingServerSocket(port));
    ::apache::thrift::stdcxx::shared_ptr 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})

你可能感兴趣的:(Thrift Server 的TNonblockingServer)