1. 程式人生 > >Java網路程式設計 -- NIO非阻塞網路程式設計

Java網路程式設計 -- NIO非阻塞網路程式設計

從Java1.4開始,為了替代Java IO和網路相關的API,提高程式的執行速度,Java提供了新的IO操作非阻塞的API即Java NIO。NIO中有三大核心元件:Buffer(緩衝區),Channel(通道),Selector(選擇器)。NIO基於Channel(通道)和Buffer(緩衝區))進行操作,資料總是從通道讀取到緩衝區中,或者從緩衝區寫入到通道中,而Selector(選擇器)主要用於監聽多個通道的事件,實現單個執行緒可以監聽多個數據通道。

Buffer(緩衝區)

緩衝區本質上是一個可以寫入資料的記憶體塊(類似陣列),然後可以再次讀取。此記憶體塊包含在NIO Buffer物件中,該物件提供了一組方法,可以更輕鬆的使用記憶體塊。
相對於直接運算元組,Buffer API提供了更加容易的操作和管理,其進行資料的操作分為寫入和讀取,主要步驟如下:

  1. 將資料寫入緩衝區
  2. 呼叫buffer.flip(),轉換為讀取模式
  3. 緩衝區讀取資料
  4. 呼叫buffer.clear()或buffer.compact()清楚緩衝區

Buffer中有三個重要屬性:
capacity(容量):作為一個記憶體塊,Buffer具有一定的固定大小,也稱為容量
position(位置):寫入模式時代表寫資料的位置,讀取模式時代表讀取資料的位置
limit(限制):寫入模式等於Buffer的容量,讀取模式時等於寫入的資料量

Buffer使用程式碼示例:

public class BufferDemo {
  public static void main(String[] args) {
    // 構建一個byte位元組緩衝區,容量是4
    ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    // 預設寫入模式,檢視三個重要的指標
    System.out.println(
        String.format(
            "初始化:capacity容量:%s, position位置:%s, limit限制:%s",
            byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
    // 寫入資料
    byteBuffer.put((byte) 1);
    byteBuffer.put((byte) 2);
    byteBuffer.put((byte) 3);
    // 再次檢視三個重要的指標
    System.out.println(
        String.format(
            "寫入3位元組後後:capacity容量:%s, position位置:%s, limit限制:%s",
            byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));

    // 轉換為讀取模式(不呼叫flip方法,也是可以讀取資料的,但是position記錄讀取的位置不對)
    System.out.println("開始讀取");
    byteBuffer.flip();
    byte a = byteBuffer.get();
    System.out.println(a);
    byte b = byteBuffer.get();
    System.out.println(b);
    System.out.println(
        String.format(
            "讀取2位元組資料後,capacity容量:%s, position位置:%s, limit限制:%s",
            byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));

    // 繼續寫入3位元組,此時讀模式下,limit=3,position=2.繼續寫入只能覆蓋寫入一條資料
    // clear()方法清除整個緩衝區。compact()方法僅清除已閱讀的資料。轉為寫入模式
    byteBuffer.compact();
    // 清除了已經讀取的2位元組,剩餘1位元組,還可以寫入3位元組資料
    // 多寫的話會報java.nio.BufferOverflowException異常
    byteBuffer.put((byte) 3);
    byteBuffer.put((byte) 4);
    byteBuffer.put((byte) 5);
    System.out.println(
        String.format(
            "最終的情況,capacity容量:%s, position位置:%s, limit限制:%s",
            byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
  }
}

ByteBuffer堆外記憶體

ByteBuffer為效能關鍵型程式碼提供了直接記憶體(direct,堆外)和非直接記憶體(heap,堆)兩種實現。堆外記憶體實現將記憶體物件分配在Java虛擬機器的堆以外的記憶體,這些記憶體直接受作業系統管理,而不是虛擬機器,這樣做的結果就是能夠在一定程度上減少垃圾回收對應用程式造成的影響,提供執行的速度。

堆外記憶體的獲取方式:ByteBuffer byteBuffer = ByteBuffer.allocateDirect(noBytes)

堆外記憶體的好處:

  • 進行網路IO或者檔案IO時比heap buffer少一次拷貝。(file/socket — OS memory — jvm heap)在寫file和socket的過程中,GC會移動物件,JVM的實現中會把資料複製到堆外,再進行寫入。
  • GC範圍之外,降低GC壓力,但實現了自動管理,DirectByteBuffer中有一個Cleaner物件(PhantomReference),Cleaner被GC執行前會執行clean方法,觸發DirectByteBuffer中定義的Deallocator

堆外記憶體的使用建議:

  • 效能確實可觀的時候才去使用,分配給大型,長壽命的物件(網路傳輸,檔案讀寫等場景)
  • 通過虛擬機器引數MaxDirectMemorySize限制大小,防止耗盡整個機器的記憶體

Channel(通道)

Channel用於源節點與目標節點之間的連線,Channel類似於傳統的IO Stream,Channel本身不能直接訪問資料,Channel只能與Buffer進行互動。

Channel的API涵蓋了TCP/UDP網路和檔案IO,常用的類有FileChannel,DatagramChannel,SocketChannel,ServerSocketChannel

標準IO Stream通常是單向的(InputStream/OutputStream),而Channel是一個雙向的通道,可以在一個通道內進行讀取和寫入,可以非阻塞的讀取和寫入通道,而且通道始終讀取和寫入緩衝區(即Channel必須配合Buffer進行使用)。

SocketChannel

SocketChannel用於建立TCP網路連線,類似java.net.Socket。有兩種建立SocketChannel的形式,一個是客戶端主動發起和伺服器的連線,還有一個就是服務端獲取的新連線。SocketChannel中有兩個重要的方法,一個是write()寫方法,write()寫方法有可能在尚未寫入內容的時候就返回了,需要在迴圈中呼叫write()方法。還有一個就是read()讀方法,read()方法可能直接返回根本不讀取任何資料,可以根據返回的int值判斷讀取了多少位元組。

核心程式碼程式碼示例片段:

// 客戶端主動發起連線
SocketChannel socketChannel = SocketChannel.open();
// 設定為非阻塞模式
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
// 發生請求資料 - 向通道寫入資料
socketChannel.write(byteBuffer);
// 讀取服務端返回 - 讀取緩衝區資料
int readBytes = socketChannel.read(requestBuffer);
// 關閉連線
socketChannel.close();

ServerSocketChannel

ServerSocketChannel可以監聽新建的TCP連線通道,類似ServerSocket。ServerSocketChannel的核心方法accept()方法,如果通道處於非阻塞模式,那麼如果沒有掛起的連線,該方法將立即返回null,實際使用中必須檢查返回的SocketChannel是否為null。

核心程式碼示例片段:

// 建立網路服務端
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
// 設定為非阻塞模式
serverSocketChannel.configureBlocking(false);
// 繫結埠
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
while (true) {
  // 獲取新tcp連線通道
  SocketChannel socketChannel = serverSocketChannel.accept();
  if (socketChannel != null) {
    // tcp請求 讀取/響應
  }
}

Selector選擇器

Selector也是Java NIO核心元件,可以檢查一個或多個NIO通道,並確定哪些通道已經準備好進行讀取或寫入。實現單個執行緒可以管理多個通道,從而管理多個網路連線。

一個執行緒使用Selector可以監聽多個Channel的不同事件,其中主要有四種事件,分別對應SelectionKey中的四個常量,分別為:

  • 連線事件 SelectionKey.OP_CONNECT
  • 準備就緒事件 SelectionKey.OP_ACCEPT
  • 讀取事件 SelectionKey.OP_READ
  • 寫入事件 SelectionKey.OP_WRITE

Selector實現一個執行緒處理多個通道的核心在於事件驅動機制,非阻塞的網路通道下,開發者通過Selector註冊對於通道感興趣的事件型別,執行緒通過監聽事件來觸發相應的程式碼執行。(更底層其實是作業系統的多路複用機制)

核心程式碼示例片段:

// 構建一個Selector選擇器,並且將channel註冊上去
Selector selector = Selector.open();
// 將serverSocketChannel註冊到selector
SelectionKey selectionKey = serverSocketChannel.register(selector, 0, serverSocketChannel);
// 對serverSocketChannel上面的accept事件感興趣(serverSocketChannel只能支援accept操作)
selectionKey.interestOps(SelectionKey.OP_ACCEPT);
while (true) {
  // 用下面輪詢事件的方式.select方法有阻塞效果,直到有事件通知才會有返回
  selector.select();
  // 獲取事件
  Set<SelectionKey> keys = selector.selectedKeys();
  // 遍歷查詢結果
  Iterator<SelectionKey> iterator = keys.iterator();
  while (iterator.hasNext()) {
    // 被封裝的查詢結果
    SelectionKey key = iterator.next();
    // 判斷不同的事件型別,執行對應的邏輯處理
    if (key.isAcceptable()) {
      // 處理連線的邏輯
    }
    if (key.isReadable()) {
      //處理讀資料的邏輯
    }

    iterator.remove();
  }
}

NIO網路程式設計完整程式碼

服務端程式碼示例:

// 結合Selector實現的非阻塞服務端(放棄對channel的輪詢,藉助訊息通知機制)
public class NIOServer {

  public static void main(String[] args) throws IOException {
    // 建立網路服務端ServerSocketChannel
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    // 設定為非阻塞模式
    serverSocketChannel.configureBlocking(false);

    // 構建一個Selector選擇器,並且將channel註冊上去
    Selector selector = Selector.open();
    // 將serverSocketChannel註冊到selector
    SelectionKey selectionKey = serverSocketChannel.register(selector, 0, serverSocketChannel);
    // 對serverSocketChannel上面的accept事件感興趣(serverSocketChannel只能支援accept操作)
    selectionKey.interestOps(SelectionKey.OP_ACCEPT);

    // 繫結埠
    serverSocketChannel.socket().bind(new InetSocketAddress(8080));
    System.out.println("啟動成功");

    while (true) {
      // 不再輪詢通道,改用下面輪詢事件的方式.select方法有阻塞效果,直到有事件通知才會有返回
      selector.select();
      // 獲取事件
      Set<SelectionKey> keys = selector.selectedKeys();
      // 遍歷查詢結果
      Iterator<SelectionKey> iterator = keys.iterator();
      while (iterator.hasNext()) {
        // 被封裝的查詢結果
        SelectionKey key = iterator.next();
        iterator.remove();
        // 關注 Read 和 Accept兩個事件
        if (key.isAcceptable()) {
          ServerSocketChannel server = (ServerSocketChannel) key.attachment();
          // 將拿到的客戶端連線通道,註冊到selector上面
          SocketChannel clientSocketChannel = server.accept();
          clientSocketChannel.configureBlocking(false);
          clientSocketChannel.register(selector, SelectionKey.OP_READ, clientSocketChannel);
          System.out.println("收到新連線 : " + clientSocketChannel.getRemoteAddress());
        }
        if (key.isReadable()) {
          SocketChannel socketChannel = (SocketChannel) key.attachment();
          try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            while (socketChannel.isOpen() && socketChannel.read(byteBuffer) != -1) {
              // 長連線情況下,需要手動判斷資料有沒有讀取結束 (此處做一個簡單的判斷: 超過0位元組就認為請求結束了)
              if (byteBuffer.position() > 0) break;
            }

            if (byteBuffer.position() == 0) continue;
            byteBuffer.flip();
            byte[] content = new byte[byteBuffer.limit()];
            byteBuffer.get(content);
            System.out.println(new String(content));
            System.out.println("收到資料,來自:" + socketChannel.getRemoteAddress());

            // 響應結果 200
            String response = "HTTP/1.1 200 OK\r\n" + "Content-Length: 11\r\n\r\n" + "Hello World";
            ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
            while (buffer.hasRemaining()) {
              socketChannel.write(buffer);
            }

          } catch (Exception e) {
            e.printStackTrace();
            key.cancel(); // 取消事件訂閱
          }
        }

        selector.selectNow();
      }
    }
  }
}

客戶端程式碼示例:

public class NIOClient {

  public static void main(String[] args) throws IOException {
    // 客戶端主動發起連線
    SocketChannel socketChannel = SocketChannel.open();
    // 設定為非阻塞模式
    socketChannel.configureBlocking(false);
    socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
    while (!socketChannel.finishConnect()) {
      // 沒連線上,則一直等待
      Thread.yield();
    }

    Scanner scanner = new Scanner(System.in);
    System.out.println("請輸入:");
    // 傳送內容
    String msg = scanner.nextLine();
    ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
    while (byteBuffer.hasRemaining()) {
      socketChannel.write(byteBuffer);
    }

    // 讀取響應
    System.out.println("收到服務端響應:");
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    while (socketChannel.isOpen() && socketChannel.read(buffer) != -1) {
      // 長連線情況下,需要手動判斷資料有沒有讀取結束 (此處做一個簡單的判斷: 超過0位元組就認為請求結束了)
      if (buffer.position() > 0) break;
    }

    buffer.flip();
    byte[] content = new byte[buffer.limit()];
    buffer.get(content);
    System.out.println(new String(content));
    scanner.close();
    socketChannel.close();
  }
}

NIO與BIO的比較

如果程式需要支撐大量的連線,使用NIO是最好的方式。
Tomcat8中已經完全移除了BIO相關的網路處理程式碼,預設採用NIO進行網路處理。