Spring Boot 搭建TCP Server


本示例首選介紹Java原生API實現BIO通信,然后進階實現NIO通信,最后利用Netty實現NIO通信及Netty主要模塊組件介紹。

Netty 是一個異步事件驅動的網絡應用程序框架,用於快速開發可維護的高性能協議服務器和客戶端。

BIO(Blocking I/O) 方案

BIO通信(一請求一應答)模型圖如下

采用 BIO 通信模型 的服務端,通常由一個獨立的 Acceptor 線程負責監聽客戶端的連接。我們一般通過在while(true) 循環中服務端會調用 accept() 方法等待接收客戶端的連接的方式監聽請求,一旦接收到一個連接請求,就可以在這個通信套接字上進行讀寫操作,此時不能再接收其他客戶端連接請求,只能等待當前連接的客戶端的操作執行完成, 如果要讓 BIO 通信模型 能夠同時處理多個客戶端請求,就必須使用多線程(主要原因是socket.accept()、socket.read()、socket.write() 涉及的三個主要函數都是同步阻塞的)

代碼實現

BIO服務端

BIOServer.java

package com.easy.javaBio;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

@Slf4j
public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(10002);
        while (true) {
            Socket client = server.accept(); //等待客戶端的連接,如果沒有獲取連接  ,在此步一直等待
            new Thread(new ServerThread(client)).start(); //為每個客戶端連接開啟一個線程
        }
        //server.close();
    }
}

@Slf4j
class ServerThread extends Thread {

    private Socket client;

    public ServerThread(Socket client) {
        this.client = client;
    }

    @SneakyThrows
    @Override
    public void run() {
        log.info("客戶端:" + client.getInetAddress().getLocalHost() + "已連接到服務器");
        BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //讀取客戶端發送來的消息
        String mess = br.readLine();
        log.info("客戶端:" + mess);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        bw.write(mess + "\n");
        bw.flush();
    }
}

BIO客戶端

BIOClient.java

package com.easy.javaBio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;

@Slf4j
public class BIOClient {

    public static void main(String[] args) throws IOException {
        Socket s = new Socket("0.0.0.0", 10002);

        InputStream input = s.getInputStream();
        OutputStream output = s.getOutputStream();

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
        bw.write("客戶端給服務端發消息測試\n");  //向服務器端發送一條消息
        bw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(input));  //讀取服務器返回的消息
        String mess = br.readLine();
        log.info("服務器:" + mess);
    }
}

運行示例

運行BIO服務端,然后再運行BIO客戶端,觀察控制台

BIOServer控制台輸出:

Connected to the target VM, address: '127.0.0.1:64346', transport: 'socket'
17:29:52.519 [Thread-1] INFO com.easy.javaBio.ServerThread - 客戶端:YHE6OR5UXQJ6D35/192.168.9.110已連接到服務器
17:29:52.523 [Thread-1] INFO com.easy.javaBio.ServerThread - 客戶端:客戶端給服務端發消息測試

BIOClient控制台輸出:

Connected to the target VM, address: '127.0.0.1:64355', transport: 'socket'
17:29:52.527 [main] INFO com.easy.javaBio.BIOClient - 服務器:客戶端給服務端發消息測試
Disconnected from the target VM, address: '127.0.0.1:64355', transport: 'socket'

這表示我們實現了一個最簡單的BIO通信了

這種方式為每個客戶端開啟一個線程,高並發時消耗資源較多,容易浪費,甚至導致服務端崩潰,對性能造成負面影響,高並發下不推薦使用。

NIO(New I/O)方案

NIO通信模型圖如下

NIO是一種同步非阻塞的I/O模型,在Java 1.4 中引入了 NIO 框架,對應 java.nio 包,提供了 Channel , Selector,Buffer等抽象。

NIO中的N可以理解為Non-blocking,不單純是New。它支持面向緩沖的,基於通道的I/O操作方法。 NIO提供了與傳統BIO模型中的 Socket 和 ServerSocket 相對應的 SocketChannel 和 ServerSocketChannel 兩種不同的套接字通道實現,兩種通道都支持阻塞和非阻塞兩種模式。阻塞模式使用就像傳統中的支持一樣,比較簡單,但是性能和可靠性都不好;非阻塞模式正好與之相反。對於低負載、低並發的應用程序,可以使用同步阻塞I/O來提升開發速率和更好的維護性;對於高負載、高並發的(網絡)應用,應使用 NIO 的非阻塞模式來開發。

NIO服務端

NIOServer.java

package com.easy.javaBio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

@Slf4j
public class NIOServer {
    private InetAddress addr;
    private int port;
    private Selector selector;

    private static int BUFF_SIZE = 1024;

    public NIOServer(InetAddress addr, int port) throws IOException {
        this.addr = addr;
        this.port = port;
        startServer();
    }

    private void startServer() throws IOException {
        // 獲得selector及通道(socketChannel)
        this.selector = Selector.open();
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);

        // 綁定地址及端口
        InetSocketAddress listenAddr = new InetSocketAddress(this.addr, this.port);
        serverChannel.socket().bind(listenAddr);
        serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);

        log.info("NIOServer運行中...按下Ctrl-C停止服務");

        while (true) {
            log.info("服務器等待新的連接和selector選擇…");
            this.selector.select();

            // 選擇key工作
            Iterator keys = this.selector.selectedKeys().iterator();
            while (keys.hasNext()) {
                SelectionKey key = (SelectionKey) keys.next();

                // 防止出現重復的key,處理完需及時移除
                keys.remove();

                //無效直接跳過
                if (!key.isValid()) {
                    continue;
                }
                if (key.isAcceptable()) {
                    this.accept(key);
                } else if (key.isReadable()) {
                    this.read(key);
                } else if (key.isWritable()) {
                    this.write(key);
                } else if (key.isConnectable()) {
                    this.connect(key);
                }
            }
        }
    }

    private void connect(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();
        if (channel.finishConnect()) {
            // 成功
            log.info("成功連接了");
        } else {
            // 失敗
            log.info("失敗連接");
        }
    }

    private void accept(SelectionKey key) throws IOException {
        ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
        SocketChannel channel = serverChannel.accept();
        channel.configureBlocking(false);
        channel.register(this.selector, SelectionKey.OP_READ);

        Socket socket = channel.socket();
        SocketAddress remoteAddr = socket.getRemoteSocketAddress();
        log.info("連接到: " + remoteAddr);
    }

    private void read(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();

        ByteBuffer buffer = ByteBuffer.allocate(BUFF_SIZE);
        int numRead = channel.read(buffer);
        if (numRead == -1) {
            log.info("關閉客戶端連接: " + channel.socket().getRemoteSocketAddress());
            channel.close();
            return;
        }
        String msg = new String(buffer.array()).trim();
        log.info("得到了: " + msg);

        // 回復客戶端
        String reMsg = msg + " 你好,這是BIOServer給你的回復消息:" + System.currentTimeMillis();
        channel.write(ByteBuffer.wrap(reMsg.getBytes()));
    }

    private void write(SelectionKey key) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(BUFF_SIZE);
        byteBuffer.flip();
        SocketChannel clientChannel = (SocketChannel) key.channel();
        while (byteBuffer.hasRemaining()) {
            clientChannel.write(byteBuffer);
        }
        byteBuffer.compact();
    }

    public static void main(String[] args) throws IOException {
        new NIOServer(null, 10002);
    }
}

使用NIO, 可以用Selector最終決定哪一組注冊的socket准備執行I/O

NIO客戶端

NIOClient.java

package com.easy.javaBio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;

@Slf4j
public class NIOClient {
    private static int BUFF_SIZE = 1024;

    public static void main(String[] args) throws IOException, InterruptedException {

        InetSocketAddress socketAddress = new InetSocketAddress("0.0.0.0", 10002);
        SocketChannel socketChannel = SocketChannel.open(socketAddress);

        log.info("連接 BIOServer 服務,端口:10002...");

        ArrayList<String> companyDetails = new ArrayList<>();

        // 創建消息列表
        companyDetails.add("騰訊");
        companyDetails.add("阿里巴巴");
        companyDetails.add("京東");
        companyDetails.add("百度");
        companyDetails.add("google");

        for (String companyName : companyDetails) {
            socketChannel.write(ByteBuffer.wrap(companyName.getBytes()));
            log.info("發送: " + companyName);

            ByteBuffer buffer = ByteBuffer.allocate(BUFF_SIZE);
            buffer.clear();
            socketChannel.read(buffer);
            String result = new String(buffer.array()).trim();
            log.info("收到NIOServer回復的消息:" + result);

            // 等待2秒鍾再發送下一條消息
            Thread.sleep(2000);
        }

        socketChannel.close();
    }
}

運行示例

首先運行我們的NIOServer,然后再運行NIOClient,觀察控制台輸出

NIOServer控制台輸出

17:35:40.921 [main] INFO com.easy.javaBio.NIOServer - NIOServer運行中...按下Ctrl-C停止服務
17:35:40.924 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:29.188 [main] INFO com.easy.javaBio.NIOServer - 連接到: /192.168.9.110:64443
17:36:29.188 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:29.194 [main] INFO com.easy.javaBio.NIOServer - 得到了: 騰訊
17:36:29.194 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:31.194 [main] INFO com.easy.javaBio.NIOServer - 得到了: 阿里巴巴
17:36:31.195 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:33.195 [main] INFO com.easy.javaBio.NIOServer - 得到了: 京東
17:36:33.195 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:35.196 [main] INFO com.easy.javaBio.NIOServer - 得到了: 百度
17:36:35.197 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:37.197 [main] INFO com.easy.javaBio.NIOServer - 得到了: google
17:36:37.198 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…
17:36:39.198 [main] INFO com.easy.javaBio.NIOServer - 關閉客戶端連接: /192.168.9.110:64443
17:36:39.198 [main] INFO com.easy.javaBio.NIOServer - 服務器等待新的連接和selector選擇…

NIOClient控制台輸出

17:36:29.189 [main] INFO com.easy.javaBio.NIOClient - 連接 BIOServer 服務,端口:10002...
17:36:29.194 [main] INFO com.easy.javaBio.NIOClient - 發送: 騰訊
17:36:29.194 [main] INFO com.easy.javaBio.NIOClient - 收到NIOServer回復的消息:騰訊 你好,這是BIOServer給你的回復消息:1576229789194
17:36:31.194 [main] INFO com.easy.javaBio.NIOClient - 發送: 阿里巴巴
17:36:31.195 [main] INFO com.easy.javaBio.NIOClient - 收到NIOServer回復的消息:阿里巴巴 你好,這是BIOServer給你的回復消息:1576229791194
17:36:33.195 [main] INFO com.easy.javaBio.NIOClient - 發送: 京東
17:36:33.196 [main] INFO com.easy.javaBio.NIOClient - 收到NIOServer回復的消息:京東 你好,這是BIOServer給你的回復消息:1576229793195
17:36:35.196 [main] INFO com.easy.javaBio.NIOClient - 發送: 百度
17:36:35.197 [main] INFO com.easy.javaBio.NIOClient - 收到NIOServer回復的消息:百度 你好,這是BIOServer給你的回復消息:1576229795197
17:36:37.197 [main] INFO com.easy.javaBio.NIOClient - 發送: google
17:36:37.198 [main] INFO com.easy.javaBio.NIOClient - 收到NIOServer回復的消息:google 你好,這是BIOServer給你的回復消息:1576229797198

NIO服務端每隔兩秒會收到客戶端的請求,並對客戶端的消息做出回復。

直接使用Java NIO API構建應用程序是可以的,但要做到正確和安全並不容易。特別是在高負載下,可靠和高效地處理和調度I/O操作是一項繁瑣而且容易出錯的任務。可以選中Netty, Apache Mina等高性能網絡編程框架。

Netty 構建 NIO 通信服務 方案

使用JDK原生網絡應用程序API,會存在的問題

  • NIO的類庫和API繁雜,使用麻煩,你需要熟練掌握Selector、ServerSocketChannel、SocketChannel、ByteBuffer等

  • 需要具備其它的額外技能做鋪墊,例如熟悉Java多線程編程,因為NIO編程涉及到Reactor模式,你必須對多線程和網路編程非常熟悉,才能編寫出高質量的NIO程序

  • 可靠性能力補齊,開發工作量和難度都非常大。例如客戶端面臨斷連重連、網絡閃斷、半包讀寫、失敗緩存、網絡擁塞和異常碼流的處理等等,NIO編程的特點是功能開發相對容易,但是可靠性能力補齊工作量和難度都非常大

Netty對JDK自帶的NIO的API進行封裝,解決上述問題,主要特點有

  • 高並發

Netty是一款基於NIO(Nonblocking I/O,非阻塞IO)開發的網絡通信框架,對比於BIO(Blocking I/O,阻塞IO),他的並發性能得到了很大提高 。

  • 傳輸快

Netty的傳輸快其實也是依賴了NIO的一個特性——零拷貝。

  • 封裝好

Netty封裝了NIO操作的很多細節,提供易於使用的API。

Netty框架的優勢

  • API使用簡單,開發門檻低;
  • 功能強大,預置了多種編解碼功能,支持多種主流協議;
  • 定制能力強,可以通過ChannelHandler對通信框架進行靈活地擴展;
  • 性能高,通過與其他業界主流的NIO框架對比,Netty的綜合性能最優;
  • 成熟、穩定,Netty修復了已經發現的所有JDK NIO BUG,業務開發人員不需要再為NIO的BUG而煩惱;
  • 社區活躍,版本迭代周期短,發現的BUG可以被及時修復,同時,更多的新功能會加入;
  • 經歷了大規模的商業應用考驗,質量得到驗證。在互聯網、大數據、網絡游戲、企業應用、電信軟件等眾多行業得到成功商用,證明了它已經完全能夠滿足不同行業的商業應用了。

代碼實現

pom.xml依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.easy</groupId>
    <artifactId>netty</artifactId>
    <version>0.0.1</version>
    <name>netty</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <encoding>UTF-8</encoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.43.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <modules>
        <module>java-tcp</module>
        <module>netty-server</module>
        <module>netty-client</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

搭建 Netty 服務端

NettyServer.java

package com.easy.nettyServer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

@Component
@Slf4j
public class NettyServer {
    /**
     * boss 線程組用於處理連接工作
     */
    private EventLoopGroup boss = new NioEventLoopGroup();
    /**
     * work 線程組用於數據處理
     */
    private EventLoopGroup work = new NioEventLoopGroup();

    @Value("${netty.port}")
    private Integer port;

    /**
     * 啟動Netty Server
     *
     * @throws InterruptedException
     */
    @PostConstruct
    public void start() throws InterruptedException {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boss, work)
                // 指定Channel
                .channel(NioServerSocketChannel.class)
                //使用指定的端口設置套接字地址
                .localAddress(new InetSocketAddress(port))

                //服務端可連接隊列數,對應TCP/IP協議listen函數中backlog參數
                .option(ChannelOption.SO_BACKLOG, 1024)

                //設置TCP長連接,一般如果兩個小時內沒有數據的通信時,TCP會自動發送一個活動探測數據報文
                .childOption(ChannelOption.SO_KEEPALIVE, true)

                //將小的數據包包裝成更大的幀進行傳送,提高網絡的負載
                .childOption(ChannelOption.TCP_NODELAY, true)

                .childHandler(new ServerChannelInitializer());
        ChannelFuture future = bootstrap.bind().sync();
        if (future.isSuccess()) {
            log.info("啟動 Netty Server");
        }
    }

    @PreDestroy
    public void destory() throws InterruptedException {
        boss.shutdownGracefully().sync();
        work.shutdownGracefully().sync();
        log.info("關閉Netty");
    }
}

NettyServerHandler.java

package com.easy.nettyServer;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    /**
     * 客戶端連接會觸發
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("Channel active......");
    }

    /**
     * 客戶端發消息會觸發
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("服務器收到消息: {}", msg.toString());
        ctx.write("我是服務端,我收到你的消息了!");
        ctx.flush();
    }

    /**
     * 發生異常觸發
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

ServerChannelInitializer.java

package com.easy.nettyServer;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //添加編解碼
        socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast(new NettyServerHandler());
    }
}

創建 Netty 客戶端

NettyClient.java

package com.easy.nettyClient;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class NettyClient {

    private EventLoopGroup group = new NioEventLoopGroup();

    @Value("${netty.port}")
    private Integer port;

    @Value("${netty.host}")
    private String host;

    private SocketChannel socketChannel;

    /**
     * 發送消息
     */
    public void sendMsg(String msg) {
        socketChannel.writeAndFlush(msg);
    }

    @PostConstruct
    public void start() {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .remoteAddress(host, port)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new NettyClientInitializer());
        ChannelFuture future = bootstrap.connect();
        //客戶端斷線重連邏輯
        future.addListener((ChannelFutureListener) future1 -> {
            if (future1.isSuccess()) {
                log.info("連接Netty服務端成功");
            } else {
                log.info("連接失敗,進行斷線重連");
                future1.channel().eventLoop().schedule(() -> start(), 20, TimeUnit.SECONDS);
            }
        });
        socketChannel = (SocketChannel) future.channel();
    }
}

NettyClientHandler.java

package com.easy.nettyClient;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客戶端Active .....");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("客戶端收到消息: {}", msg.toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

NettyClientInitializer.java

package com.easy.nettyClient;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline().addLast("decoder", new StringDecoder());
        socketChannel.pipeline().addLast("encoder", new StringEncoder());
        socketChannel.pipeline().addLast(new NettyClientHandler());
    }
}

運行示例

打開瀏覽器,地址欄輸入:http://localhost:8091/send?msg=你好,觀察服務端和客戶端控制台

服務端控制台輸出

2019-12-13 18:01:37.901  INFO 11288 --- [           main] com.easy.nettyServer.NettyServer         : 啟動 Netty Server
2019-12-13 18:01:45.834  INFO 11288 --- [ntLoopGroup-3-1] com.easy.nettyServer.NettyServerHandler  : Channel active......
2019-12-13 18:02:07.858  INFO 11288 --- [ntLoopGroup-3-1] com.easy.nettyServer.NettyServerHandler  : 服務器收到消息: 你好

客戶端控制台輸出

2019-12-13 18:01:45.822  INFO 11908 --- [ntLoopGroup-2-1] com.easy.nettyClient.NettyClient         : 連接Netty服務端成功
2019-12-13 18:01:45.822  INFO 11908 --- [ntLoopGroup-2-1] com.easy.nettyClient.NettyClientHandler  : 客戶端Active .....
2019-12-13 18:02:08.005  INFO 11908 --- [ntLoopGroup-2-1] com.easy.nettyClient.NettyClientHandler  : 客戶端收到消息: 我是服務端,我收到你的消息了!

表示使用Netty實現了我們的NIO通信了

Netty 模塊組件

Bootstrap、ServerBootstrap

一個Netty應用通常由一個Bootstrap開始,主要作用是配置整個Netty程序,串聯各個組件,Netty中Bootstrap類是客戶端程序的啟動引導類,ServerBootstrap是服務端啟動引導類。

Future、ChannelFuture

在Netty中所有的IO操作都是異步的,不能立刻得知消息是否被正確處理,但是可以過一會等它執行完成或者直接注冊一個監聽,具體的實現就是通過Future和ChannelFuture,他們可以注冊一個監聽,當操作執行成功或失敗時監聽會自動觸發注冊的監聽事件。

Channel

Netty網絡通信組件,能夠用於執行網絡I/O操作。Channel為用戶提供:

  • 當前網絡連接的通道的狀態(例如是否打開?是否已連接?)
  • 網絡連接的配置參數 (例如接收緩沖區大小)
  • 提供異步的網絡I/O操作(如建立連接,讀寫,綁定端口),異步調用意味着任何I/O調用都將立即返回,並且不保證在調用結束時所請求的I/O操作已完成。調用立即返回一個ChannelFuture實例,通過注冊監聽器到ChannelFuture上,可以I/O操作成功、失敗或取消時回調通知調用方。
  • 支持關聯I/O操作與對應的處理程序

不同協議、不同阻塞類型的連接都有不同的 Channel 類型與之對應,下面是一些常用的 Channel 類型

  • NioSocketChannel,異步的客戶端 TCP Socket 連接
  • NioServerSocketChannel,異步的服務器端 TCP Socket 連接
  • NioDatagramChannel,異步的 UDP 連接
  • NioSctpChannel,異步的客戶端 Sctp 連接
  • NioSctpServerChannel,異步的 Sctp 服務器端連接

Selector

Netty基於Selector對象實現I/O多路復用,通過 Selector, 一個線程可以監聽多個連接的Channel事件, 當向一個Selector中注冊Channel 后,Selector 內部的機制就可以自動不斷地查詢(select) 這些注冊的Channel是否有已就緒的I/O事件(例如可讀, 可寫, 網絡連接完成等),這樣程序就可以很簡單地使用一個線程高效地管理多個 Channel

NioEventLoop

NioEventLoop中維護了一個線程和任務隊列,支持異步提交執行任務,線程啟動時會調用NioEventLoop的run方法,執行I/O任務和非I/O任務:

  • I/O任務 即selectionKey中ready的事件,如accept、connect、read、write等,由processSelectedKeys方法觸發。
  • 非IO任務 添加到taskQueue中的任務,如register0、bind0等任務,由runAllTasks方法觸發。

兩種任務的執行時間比由變量ioRatio控制,默認為50,則表示允許非IO任務執行的時間與IO任務的執行時間相等。

NioEventLoopGroup

NioEventLoopGroup,主要管理eventLoop的生命周期,可以理解為一個線程池,內部維護了一組線程,每個線程(NioEventLoop)負責處理多個Channel上的事件,而一個Channel只對應於一個線程。

ChannelHandler

ChannelHandler是一個接口,處理I/O事件或攔截I/O操作,並將其轉發到其ChannelPipeline(業務處理鏈)中的下一個處理程序。

ChannelHandlerContext

保存Channel相關的所有上下文信息,同時關聯一個ChannelHandler對象

ChannelPipline

保存ChannelHandler的List,用於處理或攔截Channel的入站事件和出站操作。 ChannelPipeline實現了一種高級形式的攔截過濾器模式,使用戶可以完全控制事件的處理方式,以及Channel中各個的ChannelHandler如何相互交互。

資料

Spring Boot、Cloud 學習項目


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM