Thrift Services server

This link: https: //blog.csdn.net/hjx_1000/article/details/42779915
related sample code, see: http: //download.csdn.net/detail/hjx_1000/8374829

Five, Thrift server-side summary and analysis of several operation modes

Thrift server provides a plurality of operating modes, 5 to herein relates to the following operating modes: TSimpleServer, TNonblockingServer, THsHaServer, TThreadPoolServer, TThreadedSelectorServer, detailed work principle of this operation mode is as follows. 5:

1. TSimpleServer mode

TSimpleServer mode of operation is only one worker, cycle monitor the arrival of new requests and complete the processing of the request, it is only available in a simple demonstration of the time, it works shown in Figure 5.1:

Figure 5.1 TSimpleServer of operating mode

TSimpleServer operating mode using the simplest blocking IO, implementation concise, easy to understand, but can only receive and process a socket connection efficiency is relatively low, mainly used in the working process demonstration Thrift, very few in the actual development process use it.

2. TNonblockingServer mode

TNonblockingServer work mode, which is single-threaded work, but this mode uses NIO way, all the socket are registered to the selector, in one thread monitor all socket by seletor cycle, the end of each selector, handle all the socket is in the ready state, data socket for incoming data reading operation, the data socket for data transmission is sent, the listening socket for generating a new socket and registered with the service selector in the following figure 5.2 shows:

Figure 5.2, TNonblockingServer operating mode

After the service processing on the read data in FIG. 5.2 is a request according to the read call, calling specific function to complete the processing, the processing can be completed only function for subsequent operations;

TNonblockingServer mode advantages:

Compared to the efficiency improvement TSimpleServer mainly in IO multiplexing, TNonblockingServer non-blocking IO, while monitoring the status of the plurality of socket change;

TNonblockingServer mode Cons:

TNonblockingServer operational mode or single-threaded processing order to complete the business process complicated, time consuming, such as certain database interface functions required to read a longer execution time, at which point the pattern efficiency is not high, because the multi call to request the task is still the order executed one after another.

3. THsHaServer mode (asynchronous half Semisynchronous)

THsHaServer class is TNonblockingServer subclass, in Section 5.2 of TNonblockingServer mode, using a thread to complete listen and business processes for all socket, resulting in low efficiency, the introduction of THsHaServer mode is a partial solution to these problems. THsHaServer mode, specifically the introduction of a thread pool to service processing, as shown in Figure 5.3;

Figure 5.3 THsHaServer mode

THsHaServer advantages:

Compared with TNonblockingServer mode, THsHaServer after completion of data reading, the business process referred to complete a thread pool, the main thread returns directly for the next cycle of operation, greatly enhance the efficiency;

THsHaServer disadvantages:

As can be seen from Figure 5.3, the main thread and the complete data read and write operation to listen to all the socket, when a large number of concurrent requests, and a large amount of data transmitted, the listening socket on a new connection request can not be accepted in time.

4. TThreadPoolServer mode

TThreadPoolServer mode uses blocking socket work the way ,, if there blocking the main thread is responsible for listening, "listening socket" in the arrival of a new socket, service processing referred to a thread pool to handle, as shown in Figure 5.4:

Figure 5.4 thread pool mode operation process

TThreadPoolServer mode advantages:

Thread pool mode, read data and business processes are handed over to the thread pool is complete, only the main thread is responsible for monitoring new connections, so the amount of concurrent connections the new large can be promptly accepted. Thread pool mode is more suitable for the server can have up to predict the number of cases of concurrent clients, then each request can be processed promptly business thread pool, the performance is very high.

TThreadPoolServer mode Cons:

Thread pool mode handling capacity is limited by the ability to work the thread pool, when the number of threads in the thread pool is greater than the number of concurrent requests, a new request can only be waiting.

5.      TThreadedSelectorServer

TThreadedSelectorServer mode is the most advanced model currently offered Thrift, if it has several internal parts:

(1) a AcceptThread thread object, listen for new connections designed for use on the Socket;

Network I (2) a plurality of objects designed to handle traffic SelectorThread socket / O operations, network data read and write all of these threads are done;

(3) a load balancer SelectorThreadLoadBalancer objects, mainly for AcceptThread socket thread receives a new connection request, decided to assign the new connection request to which SelectorThread thread.

(4) a ExecutorService type of worker threads pool, in SelectorThread thread, there is business to monitor socket call request has come, after reading the request is, to make the thread pool thread ExecutorService accomplish specific implementation of the call;

Figure 5.5 TThreadedSelectorServer working process model

As shown above, TThreadedSelectorServer mode 5.5 AcceptThread a dedicated thread for processing a new connection request, and therefore able to respond to a large number of concurrent connection requests; it further network I / O operations across multiple SelectorThread threads to complete, and therefore fast network I / O read and write, can cope well with the network I / O more; TThreadedSelectorServer performance for most application scenarios are not bad, so if you really do not know what kind of work mode selection, use TThreadedSelectorServer can.
----------------
Disclaimer: This article is the original article CSDN bloggers' Happy child _ ", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/houjixin/article/details/42779915

Published 10 original articles · won praise 0 · Views 150

Guess you like

Origin blog.csdn.net/weixin_37599299/article/details/103025889