• java进阶-Netty


    Netty

    在此非常感谢尚硅谷学院以及韩顺平老师在B站公开课

    Netty视频教程

    Netty demo代码文件

    I/O

    NIO之前先说一下BIO(Blocking IO),如何理解这个Blocking呢?客户端监听(Listen)时,Accept是阻塞的,只有新连接来了,Accept才会返回,主线程才能继读写socket时,Read是阻塞的,只有请求消息来了,Read才能返回,子线程才能继续处理读写socket时,Write是阻塞的,只有客户端把消息收了,Write才能返回,子线程才能继续读取下一个请求

    BIO

    在这里插入图片描述
    在这里插入图片描述

    • 适用连接数目少且固定的服务
    package com.example.demo01.netty;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class BIODemo01 {
    
        public static void main(String[] args) throws IOException {
    
            ExecutorService executorService = Executors.newCachedThreadPool();
    
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("服务器启动成功。。。");
    
            while(true){
    
                try {
                    System.out.println(Thread.currentThread().getName() + " 等待连接正在阻塞。。。。。。");
                    final Socket socket = serverSocket.accept();
    
                    executorService.submit(() -> {
    
                        socketHandler(socket);
                    });
    
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    
        private static void socketHandler(Socket socket) {
    
            try {
                InputStream inputStream = socket.getInputStream();
    
                byte[] bytes = new byte[1024];
    
                while (true){
                    System.out.println(Thread.currentThread().getName() + " 读取数据正在阻塞。。。");
                    int read = inputStream.read(bytes);
                    if(read != -1){
                        System.out.println(new String(bytes, 0, read));
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述

    NIO

    在这里插入图片描述

    • 适用连接数目多且连接比较短的服务
    • jdk1.4开始支持

    AIO

    • 适用连接数目多且连接比较长的服务
    • jdk7开始支持

    NIO原理

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    FIleBuffer

    package com.example.demo01.netty.nio;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class NioFileChannel {
    
        public static void main(String[] args) throws Exception {
    
    
            String str = new String("测试文档");
            ByteBuffer allocate = ByteBuffer.allocate(1024);
            allocate.put(str.getBytes());
            allocate.flip();
    
            FileOutputStream fileOutputStream = new FileOutputStream("d:\\test.txt");
            FileChannel channel = fileOutputStream.getChannel();
            channel.write(allocate);
    
            fileOutputStream.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    package com.example.demo01.netty.nio;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class NioFileChannelInputStream {
    
        public static void main(String[] args) throws IOException {
            File file = new File("d:\\test.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
    
            FileChannel channel = fileInputStream.getChannel();
    
            ByteBuffer allocate = ByteBuffer.allocate((int) file.length());
    
            channel.read(allocate);
    
            System.out.println(new String(allocate.array()));
            fileInputStream.close();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    package com.example.demo01.netty.nio;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.nio.channels.FileChannel;
    
    public class NioTrancfer {
    
        public static void main(String[] args) throws Exception {
    
            FileInputStream fileInputStream = new FileInputStream("d:\\1.jpeg");
            FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.jpeg");
    
            FileChannel sourceChannel = fileInputStream.getChannel();
            FileChannel destChannel = fileOutputStream.getChannel();
    
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            sourceChannel.close();
            destChannel.close();
            fileInputStream.close();
            fileOutputStream.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    selector

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    selectionKey

    在这里插入图片描述

    零拷贝

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    Reactor

    在这里插入图片描述

    单Reactor单线程

    在这里插入图片描述

    单Reactor多线程

    在这里插入图片描述

    主从Reactor多线程

    在这里插入图片描述

    Netty线程模型
    • 简单版
      在这里插入图片描述
    • 详细版

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    OpenV2X:OpenInfra延伸到Open Vertical Infra的尝试和探索
    【Json】——jsoncpp的序列化以及反序列化
    量化投资学习——商品期货研究(一)
    C++ 模板 (一)
    十四天学会C++之第一天(入门和基本语法)
    一个坚持多年且很好用的windows系列原版操作系统下载站
    django 商品及购物车逻辑实现
    26-SpringBoot 缓存
    leetcode动态规划系列(求路径篇)
    强化学习
  • 原文地址:https://blog.csdn.net/Semanteme/article/details/133277787