1.netty介绍

这篇具有很好参考价值的文章主要介绍了1.netty介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.介绍

  1. 是JBOSS通过的java开源框架
  2. 是异步的,基于事件驱动(点击一个按钮调用某个函数)的网络应用框架,高性能高可靠的网络IO程序
  3. 基于TCP,面向客户端高并发应用/点对点大量数据持续传输的应用
  4. 是NIO框架 (IO的一层层封装) TCP/IP->javaIO和网络编程–>NIO—>Netty

2.应用场景

  1. 互联网 RPC框架比如阿里的Dubbo
  2. 网络游戏 可以定制TCP/UDP 和http协议栈
  3. 大数据 hadoop序列化组件和实时数据文件共享 AVRO
    还有Flink Spark Akka…其他开源项目

3.IO模型

  1. BIO(blocking原生javaIO,阻塞性,一个连接需要一个线程处理,连接不使用阻塞也占用线程)
    //面试: 适用连接数目少架构稳定的,对服务器资源要求高,但程序简单容易理解 jdk1.4之前唯一的选择
  2. NIO(No blocking/new 同步非阻塞)(一个线程使用selector维护多个客户端,轮询检测活动 多路复用,可以开多个线程) 图1nettyNIO原理、
    1.netty介绍,netty,netty

//适用 连接数目多且连接比较短的(轻操作)的结果 比如聊天,弹幕,服务器之间通信,编程复杂 jdk1.4开始支持
3. AIO(等待时间长才需要异步,异步非阻塞) 是有效请求才启动线程
//面试: 适合;连接数量多且连接长的, 比如相册服务器.编程复杂,jdk7开始支持

4.BIO实例(可以使用线程池改善)

win的 telnet 127.0.0.1 6666端口的ctrl+]的send xxx可以发送数据到服务器

5.NIO

  1. 结构 如果通道没有事件,选择器不处理,线程也不阻塞 selector(选择器)<–>channel(通道)<—> buffer(缓冲区,数据先在这里实现了非阻塞)<—>socket //IntBuffer的使用
    java除了boolean类型,是类型统一管理,所以比BIO更快 //!!!一定要flip()读写切换 才能读取值
  2. 关系
    1.buffer是内存块(面向缓冲/块的编程),是一个数组,达到一定量才给channel
    2.selector由哪个channel有事件才切换过去的,不是自动切换的 ?
    3.buffer channel是双向的,区别BIO 有in output流
    4.一个thread对应一个selector,对应多个channel

6.Buffer的属性

position
filp()会使他变为0,读写的时候也会改变,一直往后移
limit(块的最大容量) 和capacity(自己设置的块容量)一样用来判断是否超出范围 capacity 自己设置的数组容量 mark 标记

//常用的api

IntBuffer in=IntBuffer.allocate(5);//5个容量 
                                   .flip //读写切换,如果是读的可以切换为写的,所以说NIO是双向的
                                  .position(1);//设置读写的位置
                                  .limit(3);//设置读写限制个数<3
                                  .get(); //得到元素通过iterator
                                  .put(1);
                                  .put(1,1);//在指定位置放数据
                                  .clear();//清除缓冲器 
                                  .hasRemain();//相当于hasNext()
                                  .isReadOnly();//是否为只读缓冲区
                                  //抽象方法jdk1.6引入 hasArray()缓冲区是否可访问底层的buffer数组 array()返回buffer数组
                                 //常用ByteBuffer

7.Channel 可以向buffer读写和拷贝

1.ServerSockerChannel 类似ServerSocker,SockerChannel就是Socket,还有一模一样的UDP的类
2.FileChannel类
  read() 通道读数据到buffer write() buffer写入通道 transferFrom()目标通道复制到本通道 transferTo() channel复制到目标通道

8.使用channel生成文件,与BIO的关系,底层就是BIO

          String str="aaa";
         out=new FileOutputStream("d;\\aaa.txt"); //输出流包括channel
          FileChannel channel=out.getChannel();

            byteBuffer=ByteBuffer.allocate(1024);
            byteBuffer.put(str.getBytes);
            byteBuffer.flip(); //初始状态是读,切换成写
            channel.write(byteBuffer);//向其他通道写
            out.close();//关闭流
public class bufferDemo {
    public static void main(String[] args) throws IOException {
        String str="helloworld";

        FileOutputStream out = new FileOutputStream("D:\\abs.txt");
        FileChannel channel = out.getChannel();
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        byteBuffer.position(2);
        byteBuffer.put(str.getBytes()); //放在limit后面会报错,这个是限制容量
        //相当于截断写入的数据
        byteBuffer.limit(7);

        //必须要写
        byteBuffer.flip();
        channel.write(byteBuffer);
        channel.close();
        out.close();


    }
}

9.读数据

      File file=new File("d:\\aa.txt");
        in=new FileInputStream(file);
         fileChannel=in.getChannel();
         byteBuffer= ByteBuffer.allocate((int)file.length());
         fileChannel.read(byteBuffer);
              sout(new String(  byteBuffer.array()));
               in.close();
public class bfDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\abs.txt");
        FileInputStream inputStream = new FileInputStream(file);
        FileChannel channel = inputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        channel.read(byteBuffer);

            System.out.println(new java.lang.String(byteBuffer.array()));


    }
}

10.一个buffer 多个channel(创建两个流)拷贝文件 边读边写
//在java写路径相对位置是 本项目最父类的项目路径!!!(以前写相对路径总是出错)
//必须需要不然position=limit

      byteBytebuffer.clear();
                             .flip();
     public class bfDemo3Copy {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("D:\\abs.txt");
        FileChannel c1 = inputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\abs1.txt");
        FileChannel c2 = fileOutputStream.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(inputStream.available());
        c1.read(allocate);
        allocate.flip();
        c2.write(allocate);

    }

}

//一块一块读,反转写入.steps are combinate bytes to buffer and send to another channel(outputStream)

public class NIOFileChannel03 {
    public static void main(String[] args) throws Exception {

        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel fileChannel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true) { //循环读取
            byteBuffer.clear(); //清空buffer!!!
            int read = fileChannel01.read(byteBuffer);
            System.out.println("read =" + read);
            if(read == -1) { //表示读完
                break;
            }
            //将buffer 中的数据写入到 fileChannel02 -- 2.txt
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
        }

        //关闭相关的流
        fileInputStream.close();
        fileOutputStream.close();
    }
}

11.拷贝图片

     destch.transferFrom(sourceCh,0,sourceCh.size());
    //关闭所有通道和流
public class bfDemo4transferCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("D:\\abs.txt");
        FileChannel c1 = inputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\abs3.txt");
        FileChannel c2 = fileOutputStream.getChannel();
        c2.transferFrom(c1,0,c1.size());
    }
}

12.buffer放的顺序和取的顺序一样不然抛出异常BufferUnderflowException

  ByteBuffer bf= ByteBuffer.allocate(64);
     bf.putInt(100); 
     bf.putLong(100L);
    bf.flip();
   bf.getInt(); 
     bf.getLong();

13.写完转为只读buffer
//12的代码

    buffer.asReadOnlyBuffer();public class OnlyWrite {
    public static void main(String[] args) {
        ByteBuffer bf= ByteBuffer.allocate(64);
        bf.putInt(100);
        bf.putLong(100L);
        bf.flip();
        ByteBuffer byteBuffer = bf.asReadOnlyBuffer();
        byteBuffer.getInt();
        byteBuffer.getLong();
        byteBuffer.putLong(10);//错误不能写,只能读
    }
}

14.MappedByteBuffer可以在堆外内存修改文件(效率高)操作系统不用拷贝一次到内存

access=new RandAccessFile("1.txt","rw");
 channel=access.getChannel();
map=channel.map(FileChannel.MapMode.READ_WRITE,0,5);模式,开始位置,结束位置 <5byte
   map.put(0,(byte)'H'); //在内存直接修改文件内容,然后放回磁盘
  access.close();
 public class MapBuffer {
    public static void main(String[] args) throws IOException {
        RandomAccessFile access = new RandomAccessFile("1.txt", "rw");
        FileChannel channel = access.getChannel();
        //到内存修改的大小范围是 0-5, if out of the bound will throw an expection
        MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        map.put(0,(byte)'A');
        map.put(4,(byte)'H');

        access.close();
        System.out.println("修改成功");
    }
}

//idea需要在文件夹外打开,不然看不到效果

15.分散和聚集 scatter(写的时候放到多个buffer[数组array])和gather(读的时候多个buffer) 加快读写效率(the key is using many buffer to read and read)(read and write only once that can get more buffer)

  
public class scatterAndGather {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocket =  ServerSocketChannel.open().bind(new InetSocketAddress(6666));
        ByteBuffer[] byteBuffers= new ByteBuffer[2];
        byteBuffers[0]=ByteBuffer.allocate(3);
        byteBuffers[1]=ByteBuffer.allocate(5);

        SocketChannel accept = serverSocket.accept();
        int msgLen=8;
       while (true){
           int byteRead=0;
          while (byteRead<msgLen){

              long read = accept.read(byteBuffers);
              byteRead++;
              System.out.println("bufferRead:"+byteRead);
              Arrays.asList(byteBuffers).stream()
                      .map(
                              buffer->"position"+buffer.position()+
                                      ",limit="+buffer.limit()
                      )
                              .forEach(System.out::println);



          }
           Arrays.asList(byteBuffers).stream().forEach(buffer ->buffer.flip() );
          long byteWrite=0;
          while (byteWrite<msgLen){
              //一次读出多少个字节
              long l=accept.write(byteBuffers);
              byteWrite+=l;
              System.out.println(byteWrite);
          }
          Arrays.asList(byteBuffers).forEach(byteBuffer -> {
              System.out.println(new String(byteBuffer.array() ));
              byteBuffer.clear();});
           System.out.println("byteRead:="+byteRead+"byteWrite:"+byteWrite+"msgLen:"+msgLen);

       }


    }
}

16.selector管理多个连接(channel) 不停如轮询检测事件,有事件处理,阻塞不等待,处理其他通道的请求,解决了多线程单通道的问题,提高了性能文章来源地址https://www.toymoban.com/news/detail-619041.html

   selector类selects()//得到channel的连接的selectkey集合阻塞,
  selector.select(1000)//阻塞1000毫秒,必须返回数据
  selector.wakeup()//唤醒selector,阻塞中可以唤醒
  selector.selectNow()//非阻塞,立即返回结果,核心方法
           selector.selectedKeys() //select获取绑定channel产生事件的selectkey集合
//可以注册channel到selector上返回selectionKey  (集合)

到了这里,关于1.netty介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • [Netty] Netty自带的心跳机制 (十五)

    [Netty] Netty自带的心跳机制 (十五)

    1.IdleStateHandler介绍 Netty服务端心跳机制: IdleStateHandler, 这个类可以对三种类型的心跳检测。 IdleHandler继承了ChannelInboundHandlerAdapter, 载了userEventTriggered方法, 执行了关闭连接的逻辑 2.IdleStateHandler源码解析 readerIdleTime:为读超时时间(即测试端一定时间内未接受到被测试端消息

    2023年04月24日
    浏览(8)
  • Netty 系列教程——Netty 与 HTTP 协议

    作者:禅与计算机程序设计艺术 Netty 是由 JBOSS 提供的一个 Java 高性能网络应用程序框架。通过 Netty 的高效的 buffer 池、通道 pipeline、事件驱动机制等技术实现了天生的异步非阻塞特性。为了简化开发难度并提升开发效率,Netty 推出了一系列的开源组件如 Socket 通信模块(ne

    2024年02月06日
    浏览(12)
  • io.netty学习(二)Netty 架构设计

    io.netty学习(二)Netty 架构设计

    目录 前言 Selector 模型 SelectableChannel Channel 注册到 Selector SelectionKey 遍历 SelectionKey 事件驱动 Channel 回调 Future 事件及处理器 责任链模式 责任链模式的优缺点 ChannelPipeline 将事件传递给下一个处理器 总结 上一篇文章,我们对  Netty 做了一个基本的概述,知道什么是 Netty 以及

    2024年02月10日
    浏览(13)
  • Netty3 和Netty4区别

    Netty3 和Netty4区别

    Survive by day and develop by night. talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive. happy for hardess to solve denpendies. Netty3 和Netty4区别 需求: 1.Netty3和Netty4区别 Netty3和Netty4区别: 2.demo Netty是一个基于NIO的高性能网络框架,提供了TCP,UDP和

    2024年02月14日
    浏览(11)
  • Netty-Netty源码分析流程图

    Netty-Netty源码分析流程图

     补充

    2024年01月23日
    浏览(41)
  • 13.Netty源码之Netty中的类与API

    13.Netty源码之Netty中的类与API

    Bootstrap 意思是引导,一个 Netty 应用通常由一个 Bootstrap 开始,主要作用是配置整个 Netty 程序,串联各个组件,Netty 中ServerBootstrap 是服务端启动引导类。 java //泛型 AbstractBootstrapB extends AbstractBootstrapB, C, C extends Channel ​ ServerBootstrap extends AbstractBootstrapServerBootstrap, ServerChannel ​

    2024年02月15日
    浏览(32)
  • 【Netty】Netty中的超时处理与心跳机制(十九)

    【Netty】Netty中的超时处理与心跳机制(十九)

    回顾Netty系列文章: Netty 概述(一) Netty 架构设计(二) Netty Channel 概述(三) Netty ChannelHandler(四) ChannelPipeline源码分析(五) 字节缓冲区 ByteBuf (六)(上) 字节缓冲区 ByteBuf(七)(下) Netty 如何实现零拷贝(八) Netty 程序引导类(九) Reactor 模型(十) 工作原理

    2024年02月06日
    浏览(11)
  • 【Netty】使用 SSL/TLS 加密 Netty 程序(二十)

    【Netty】使用 SSL/TLS 加密 Netty 程序(二十)

    回顾Netty系列文章: Netty 概述(一) Netty 架构设计(二) Netty Channel 概述(三) Netty ChannelHandler(四) ChannelPipeline源码分析(五) 字节缓冲区 ByteBuf (六)(上) 字节缓冲区 ByteBuf(七)(下) Netty 如何实现零拷贝(八) Netty 程序引导类(九) Reactor 模型(十) 工作原理

    2024年02月07日
    浏览(7)
  • 34.Netty源码之Netty如何处理网络请求

    34.Netty源码之Netty如何处理网络请求

    通过前面两节源码课程的学习,我们知道 Netty 在服务端启动时会为创建 NioServerSocketChannel,当客户端新连接接入时又会创建 NioSocketChannel,不管是服务端还是客户端 Channel,在创建时都会初始化自己的 ChannelPipeline。 如果把 Netty 比作成一个生产车间,那么 Reactor 线程无疑是车间

    2024年02月11日
    浏览(10)
  • Netty系列(一):Springboot整合Netty,自定义协议实现

    Netty是由JBOSS提供的一个java开源框架,现为 Github上的独立项目。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。 也就是说,Netty 是一个基于NIO的客户、服务器端的编程框架,使用Netty 可以确保你快速和简单

    2023年04月25日
    浏览(13)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包