netty学习04

来源:互联网 发布:淘宝扣24分怎么办 编辑:程序博客网 时间:2024/06/05 16:21

1.网络服务一般的结构:

  读取请求--->解码请求--->处理服务--->编码响应--->发送响应

经典的服务设计是“每一个请求一个线程”,如下图



 2.Reactor模式

Reactor响应I/O事件,分发到合适的Handler处理。

Handler执行非阻塞的动作。

基本的Reactor设计,单线程版本


示例代码:

 

Java代码  收藏代码
  1. package com.zhang.nio;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetSocketAddress;  
  5. import java.nio.channels.SelectionKey;  
  6. import java.nio.channels.Selector;  
  7. import java.nio.channels.ServerSocketChannel;  
  8. import java.nio.channels.SocketChannel;  
  9. import java.util.Set;  
  10.   
  11. public class Reactor implements Runnable {  
  12.   
  13.     final Selector selector;  
  14.   
  15.     final ServerSocketChannel serverSocketChannel;  
  16.   
  17.     public Reactor(int port) throws IOException {  
  18.         selector = Selector.open();  
  19.         serverSocketChannel = ServerSocketChannel.open();  
  20.         serverSocketChannel.socket().bind(new InetSocketAddress(port));  
  21.         serverSocketChannel.configureBlocking(false);  
  22.         SelectionKey key = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);  
  23.         key.attach(new Acceptor());  
  24.     }  
  25.   
  26.     @Override  
  27.     public void run() {  
  28.         while (!Thread.interrupted()) {  
  29.             try {  
  30.                 selector.select();  
  31.                 Set<SelectionKey> selectionKeys = selector.selectedKeys();  
  32.                 for(SelectionKey selectionKey : selectionKeys){  
  33.                     dispatch(selectionKey);  
  34.                 }  
  35.                 selectionKeys.clear();  
  36.             } catch (IOException e) {  
  37.                 // TODO Auto-generated catch block  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     private void dispatch(SelectionKey selectionKey) {  
  45.         Runnable run = (Runnable) selectionKey.attachment();  
  46.         if(run != null){  
  47.             run.run();  
  48.         }  
  49.     }  
  50.       
  51.     class Acceptor implements Runnable{  
  52.   
  53.         @Override  
  54.         public void run() {  
  55.             try {  
  56.                 SocketChannel channel = serverSocketChannel.accept();  
  57.                 if(channel != null){  
  58.                     new Handler(selector,channel);  
  59.                 }  
  60.             } catch (IOException e) {  
  61.                 // TODO Auto-generated catch block  
  62.                 e.printStackTrace();  
  63.             }  
  64.               
  65.         }  
  66.   
  67.     }  
  68.   
  69. }  
 

 

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.nio.ByteBuffer;  
  3. import java.nio.channels.SelectionKey;  
  4. import java.nio.channels.Selector;  
  5. import java.nio.channels.SocketChannel;  
  6.   
  7. public class Handler implements Runnable {  
  8.   
  9.     private final static int DEFAULT_SIZE = 8092;  
  10.   
  11.     private final SocketChannel socketChannel;  
  12.   
  13.     private final SelectionKey seletionKey;  
  14.   
  15.     private static final int READING = 0;  
  16.   
  17.     private static final int SENDING = 1;  
  18.       
  19.     private int state = READING;  
  20.   
  21.     ByteBuffer inputBuffer = ByteBuffer.allocate(DEFAULT_SIZE);  
  22.   
  23.     ByteBuffer outputBuffer = ByteBuffer.allocate(DEFAULT_SIZE);  
  24.   
  25.     public Handler(Selector selector, SocketChannel channel) throws IOException {  
  26.         this.socketChannel = channel;  
  27.         socketChannel.configureBlocking(false);  
  28.         this.seletionKey = socketChannel.register(selector, 0);  
  29.         seletionKey.attach(this);  
  30.         seletionKey.interestOps(SelectionKey.OP_READ);  
  31.         selector.wakeup();  
  32.     }  
  33.   
  34.     @Override  
  35.     public void run() {  
  36.         if(state == READING){  
  37.             read();  
  38.         }else if(state == SENDING){  
  39.             write();  
  40.         }  
  41.   
  42.     }  
  43.       
  44.     class Sender implements Runnable {  
  45.   
  46.         @Override  
  47.         public void run() {  
  48.             try {  
  49.                 socketChannel.write(outputBuffer);  
  50.             } catch (IOException e) {  
  51.                 // TODO Auto-generated catch block  
  52.                 e.printStackTrace();  
  53.             }  
  54.             if(outIsComplete()){  
  55.                 seletionKey.cancel();  
  56.             }  
  57.               
  58.               
  59.         }  
  60.           
  61.     }  
  62.   
  63.     private void write() {  
  64.         try {  
  65.             socketChannel.write(outputBuffer);  
  66.         } catch (IOException e) {  
  67.             // TODO Auto-generated catch block  
  68.             e.printStackTrace();  
  69.         }  
  70.         while(outIsComplete()){  
  71.             seletionKey.cancel();  
  72.         }  
  73.           
  74.     }  
  75.   
  76.     private void read() {  
  77.         try {  
  78.             socketChannel.read(inputBuffer);  
  79.             if(inputIsComplete()){  
  80.                 process();  
  81.                 seletionKey.attach(new Sender());  
  82.                 seletionKey.interestOps(SelectionKey.OP_WRITE);  
  83.                 seletionKey.selector().wakeup();  
  84.             }  
  85.         } catch (IOException e) {  
  86.             // TODO Auto-generated catch block  
  87.             e.printStackTrace();  
  88.         }  
  89.           
  90.           
  91.     }  
  92.       
  93.     public boolean inputIsComplete(){  
  94.         return false;  
  95.     }  
  96.       
  97.     public boolean outIsComplete(){  
  98.         return false;  
  99.           
  100.     }  
  101.       
  102.     public void process(){  
  103.           
  104.     }  
  105.   
  106. }  
 

NIO支持的特性:

 

  • Channels,连接到文件、socket等等,支持非阻塞读。
  • Buffers,与数组相似的对象,能直接从Channels中读写。
  • Selectors,辨识哪一个通道的集合有IO事件。(Tell which of a set of Channels have IO events)
  • SelectionKeys,维护IO事件的状态和绑定。
多线程设计
  • 添加线程增加可扩展性,主要应用在多核处理器中
  • Worker 线程,Reactors要快速出发handlers。handlers的处理降低了Reactor的速度。将非I/O操作分离到其他线程中处理。
  • Multiple Reactor Threads,多Reactor线程。Reactor线程可以使IO操作饱和,分布负载到其他的reactors,负载均衡来匹配CPU和IO之间的速度差异。


 使用多个Reactors


 

 

文章转自:http://ryanflyer.iteye.com/blog/1672876
0 0