Thrift multi-threaded semi-synchronous semi-asynchronous service model-TThreadedSelectorServer

TThreadedSelectorServer is the most mature and respected RPC service model in the industry. TThreadedSelectorServer is an extension of the above NonblockingServer, which separates the Selector threads of Accept and Read/Write, and introduces the Worker worker thread pool. It is also a Half-sync/Half-async service model.


MainReactor is the Accept thread, which is used to monitor client connections. SubReactor uses IO event threads (multiple), which are mainly responsible for processing IO read and write events of all clients. The Worker worker thread is mainly used to process the handler callback of each rpc request Processing (this part is synchronous).

Let's look at the implementation of the server:

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. package cn.slimsmart.thrift.demo.helloworld;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.TProcessor;  
  5. import org.apache.thrift.protocol.TBinaryProtocol;  
  6. import org.apache.thrift.server.TServer;  
  7. import org.apache.thrift.server.TThreadedSelectorServer;  
  8. import org.apache.thrift.transport.TFramedTransport;  
  9. import org.apache.thrift.transport.TNonblockingServerSocket;  
  10.   
  11. /** 
  12.  * Service model of multi-threaded Half-sync/Half-async. 
  13.  * Needs to be specified as: TFramedTransport The method of data transfer. 
  14.  */  
  15. publicclass HelloTThreadedSelectorServer {   
  16.       
  17.     // register port  
  18.     publicstaticfinalint SERVER_PORT = 8080;     
  19.   
  20.     publicstaticvoid main(String[] args) throws TException {    
  21.           
  22.         TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());  
  23.         // transport channel - non-blocking    
  24.         TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);  
  25.           
  26.         //多线程半同步半异步  
  27.         TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);  
  28.         tArgs.processor(tprocessor);  
  29.         tArgs.transportFactory(new TFramedTransport.Factory());  
  30.         //二进制协议  
  31.         tArgs.protocolFactory(new TBinaryProtocol.Factory());  
  32.         // 多线程半同步半异步的服务模型  
  33.         TServer server = new TThreadedSelectorServer(tArgs);  
  34.         System.out.println("HelloTThreadedSelectorServer start....");  
  35.         server.serve(); // 启动服务  
  36.           
  37.     }  
  38.   
  39. }  

 

http://blog.csdn.net/zhu_tianwei/article/details/44115297

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604742&siteId=291194637