netty学习04

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值