search
HomeJavajavaTutorialOverview of Java IO model and Netty framework

Overview of Java IO model and Netty framework

Apr 22, 2023 pm 02:34 PM
javaionetty

    什么是Netty

    • 异步,基于事件驱动的网络应用框架,用以快速开发高性能,高可靠的网络IO程序

    • 主要针对在TCP协议下,面向Clients端的高并发应用

    • 本质是一个NIO框架,适用于服务器通讯等场景

    异步:发送请求无需等待响应,程式接着往下走。

    事件驱动:一个连接事件或者断开事件,或者读事件或者写事件,发生后的后续处理。

    Overview of Java IO model and Netty framework

    Netty典型应用:

    • 高性能rpc框架用来远程服务(过程)调用,比如Dubbo。

    • 游戏行业,页面数据交互。

    • 大数据领域如Hadoop高性能通讯和序列化组件(AVRO)。

    IO模型

    简单理解就是用什么通道去进行数据发送和接收。

    BIO:一个连接一个线程,连接不做任何事会造成不必要的线程开销。适用于连接数目较小且固定的架构。

    Overview of Java IO model and Netty framework

    NIO:服务端一个线程(也可以多个),维护一个多路复用器。由多路复用器去处理IO线程。适用于连接数目多且较短的架构

    Overview of Java IO model and Netty framework

    AIO:异步非阻塞,还未得到广泛应用。适用于连接数目多且连接较长的架构。

    BIO

    BIO编程简单流程

    • 服务端创建启动ServerSocket

    • 客户端启动Socket对服务器进行通信,默认服务器会对每一个客户创建一个线程。

    • 客户端发出请求后,先咨询线程是否有响应,如果没有则等待或者拒绝。

    • 如果有响应,则等待请求结束后,再继续执行。(阻塞)

    BIO简单实例

    public class BIOserver {
        public static void main(String[] args) throws IOException {
            // 为了方便直接用了Executors创建线程池
            ExecutorService service = Executors.newCachedThreadPool();
            //指定服务端端口
            ServerSocket serverSocket = new ServerSocket(6666);
            System.out.println("服务器启动");
            while(true){
                //阻塞等待连接
                Socket socket = serverSocket.accept();
                System.out.println("连接到一个客户端");
                //每个连接对应一个线程
                service.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            handler(socket);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
        public static void handler(Socket socket) throws IOException {
            System.out.println("Thread:"+Thread.currentThread().getId());
            byte[] bytes = new byte[1024];
            InputStream inputStream = socket.getInputStream();
            while (true){
                //阻塞等待读取
                int n = inputStream.read(bytes);
                if(n!=-1){
                    System.out.println(new String(bytes,0,n));
                }else {
                    break;
                }
            }
            socket.close();
        }
    }

    测试:使用windows的telnet

    Overview of Java IO model and Netty framework

    使用 ctrl+]

    Overview of Java IO model and Netty framework

    Overview of Java IO model and Netty framework

     可以在服务端控制台看到,已经读取到发送的数据

    Overview of Java IO model and Netty framework

    NIO

    三大核心部分:Channel(可类比Socket),Buffer,Selector

    大概是这个样子。客户端和Buffer交互,Buffer和Channel是一对一的关系。Selector选择操作Channel(事件驱动,如果Channel有事件发生,Selector才去选择操作。)

    Overview of Java IO model and Netty framework

    Buffer

    Buffer基本使用

    ByteBuffer使用场景较为广泛。

    buffer就是一个内存块,所以说nio是面向块/缓冲,底层是数组。数据读写是通过buffer。可以使用方法flip切换读写。

    public class BufferNio {
        public static void main(String[] args) {
            //创建buffer容量为5个int
            IntBuffer buffer = IntBuffer.allocate(5);
            //放数据
            buffer.put(1);
            buffer.put(2);
            buffer.put(3);
            buffer.put(4);
            buffer.put(5);
            //读写切换
            buffer.flip();
            //取数据
            //内部维护一个索引,每次get索引都会往后边移动
            while(buffer.hasRemaining()){
                System.out.println(buffer.get());
            }
        }
    }

    Buffer四个主要属性

    // Invariants: mark <= position <= limit <= capacity
        private int mark = -1;
        private int position = 0;
        private int limit;
        private int capacity;

    mark:标记,很少改变

    position:下一个要被读元素的位置,为下次读写做准备

    limit:缓冲器当前的终点,不能对缓冲区极限意外的区域读写,可变。

    capacity:不可变,创建时指定的最大容量。

    上边出现了读写切换的方法flip,我们看下源码,可以看出来通过改变属性实现可读可写的。

    public final Buffer flip() {
            limit = position;
            position = 0;
            mark = -1;
            return this;
        }

    可以通过啊更改limit或者position来实现你想要的操作。参数自己决定

    buffer.limit(2);
            buffer.position(1);

    Channel

    可读可写,上接Selector,下连Buffer。

    当客户端连接ServerSocketChannel时,创建客户端自己的SocketChannel。

    本地文件写案例

    public class ChannelNio {
        public static void main(String[] args) throws IOException {
            String str = "少壮不努力,老大徒伤悲";
            //创建输出流
            FileOutputStream os = new FileOutputStream("D:\\xxxxxxxxxxxxxxxxxxx\\a.txt");
            //获取FileChannel
            FileChannel channel = os.getChannel();
            //创建缓冲
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //把字符串放入缓冲区
            buffer.put(str.getBytes());
            //反转ByteBuffer
            buffer.flip();
            //将ByteBuffer写入到FileChannel
            channel.write(buffer);
            //关闭流
            os.close();
        }
    }

    图示理解

    Overview of Java IO model and Netty framework

    本地文件读案例

    public class ChannelNio {
        public static void main(String[] args) throws IOException {
            FileInputStream is = new FileInputStream("D:\\xxxxxxxxxxxxxxxxxxx\\a.txt");
            FileChannel channel = is.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            channel.read(buffer);
            System.out.println(new String(buffer.array()));
            is.close();
        }
    }

    本地文件拷贝案例

    方法一

    public class ChannelNio {
        public static void main(String[] args) throws IOException {
            FileInputStream is = new FileInputStream("D:\\xxxxxxxxxxxxxxxxxxx\\a.txt");
            FileChannel channel = is.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            FileOutputStream os = new FileOutputStream("D:\\xxxxxxxxxxxxxxxxxxx\\b.txt");
            FileChannel osChannel = os.getChannel();
            while (true){
                buffer.clear();
                int i = channel.read(buffer);
                if(i==-1){
                    break;
                }
                buffer.flip();
                osChannel.write(buffer);
            }
            is.close();
            os.close();
        }
    }

    方法二

    public class ChannelNio {
        public static void main(String[] args) throws IOException {
            FileInputStream is = new FileInputStream("D:\\xxxxxxxxxxxxxxxxxxx\\HELP.md");
            FileChannel channel = is.getChannel();
            FileOutputStream os = new FileOutputStream("D:\\xxxxxxxxxxxxxxxxxxx\\HELP222.md");
            FileChannel osChannel = os.getChannel();
            osChannel.transferFrom(channel,0,channel.size());
            is.close();
            os.close();
        }
    }

    Selector

    用一个线程处理多个客户端连接。可以检测多个注册通道的事件,并作出相应处理。不用维护所有线程。

    Overview of Java IO model and Netty framework

    Selector can obtain a SelectionKey set of the registered SocketChannel, then listen to the select, obtain the SelectionKey when an event occurs, and finally obtain the channel through the SelectionKey to perform corresponding operations and complete the business.

    The above is the detailed content of Overview of Java IO model and Netty framework. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

    The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

    How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

    The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

    How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

    The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

    How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

    The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

    How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

    Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use