Netty(一)建立簡單的Echo Server & Client

Netty簡介

Netty的核心組件:java

  • Channel
  • 回調
  • Future
  • 事件和ChannelHandler

Channel
Channel能夠看作是傳入(入站)或者傳出(出站)數據的載體,,能夠被打開或者關閉,鏈接或者斷開鏈接;apache

回調
一個回調其實就是一個方法,一個指向已經被提供給另一個方法的方法的引用。這使得接受回調的方法能夠在適當的時候調用前者。當一個回調被觸發時,相關的事件能夠被一個interface-ChannelHandler的實現處理。bootstrap

Future
Future提供了另外一種在操做完成時通知應用程序的方式。這個對象能夠看作是一個異步操做的結果的佔位符,它將在將來的某個時刻完成,並提供對其結果的訪問。
ChannelFuture提供了額外幾個方法,使得咱們可以註冊一個或者多個ChannelFutureListener實例。由ChannelFutureListener實例提供的通知機制消除了手動檢查對應的操做是否完成的必要。安全

事件和 ChannelHandler
Netty使用不一樣的事件來通知咱們狀態的改變或者是操做的狀態。這使得咱們可以基於已經發生的事件來觸發適當的動做。每一個事件均可以被分發給ChannelHandler類中的某個用戶實現的方法。服務器

建立Maven工程

整個項目的目錄結構以下:異步

clipboard.png

echo-parent的 pom文件socket

<?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>

    <groupId>com.nasuf.echo</groupId>
    <artifactId>echo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>../echo-client</module>
        <module>../echo-server</module>
    </modules>

    <properties>
        <echo-server.hostname>localhost</echo-server.hostname>
        <echo-server.port>9999</echo-server.port>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
            <version>4.1.10.Final</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

echo-server的 pom文件maven

<?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">
    <parent>
        <artifactId>echo-parent</artifactId>
        <groupId>com.nasuf.echo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../echo-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>echo-server</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>run-server</id>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.echo.server.handler.EchoServer</mainClass>
                    <arguments>
                        <argument>${echo-server.port}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

echo-client的 pom文件ide

<?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">
    <parent>
        <artifactId>echo-parent</artifactId>
        <groupId>com.nasuf.echo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../echo-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>echo-client</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>run-server</id>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.echo.client.handler.EchoClient</mainClass>
                    <arguments>
                        <argument>${echo-server.hostname}</argument>
                        <argument>${echo-server.port}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

而後來構建 echo-clientecho-serveroop

EchoServer

EchoServerHandler

package com.echo.server.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

// @Sharable標示一個ChannelHandler能夠被多個Channel安全共享
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        System.out.println(
                "Server received: " + in.toString(CharsetUtil.UTF_8));
        // 將接收到的消息寫給發送者,即客戶端,而不沖刷出站消息        
        ctx.write(in);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // 將未決消息沖刷到遠程節點,而且關閉該Channel
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
            .addListener(ChannelFutureListener.CLOSE);
    }

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

}

所謂「未決消息(pending message)」指的是目前暫存於ChannelOutboundBuffer中的消息,在下一次調用flush()或者writeAndFlush()方法時將會嘗試寫出到套接字。

EchoServer

package com.echo.server.handler;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class EchoServer {
    
    private final int port;
    
    public EchoServer(int port) {
        this.port = port;
    }
    
    public void start() throws Exception {
        final EchoServerHandler serverHandler = new EchoServerHandler();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                .channel(NioServerSocketChannel.class)
                .localAddress(new InetSocketAddress(port))
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(serverHandler);
                    }
                });
            // 此處綁定服務器,並等待綁定完成。對sync()方法的調用將致使當前Thread阻塞,直到綁定完成
            ChannelFuture f = b.bind().sync();
            // 因爲調用了sync()方法,程序將會阻塞等待,直到服務器的Channel關閉
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
    
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println(
                    "Usage: " + EchoServer.class.getSimpleName() + " <port>"
                    );
            return;
        }
        int port = Integer.parseInt(args[0]);
        new EchoServer(port).start();
    }

}

總結:

  • EchoServerHandler實現了業務邏輯
  • main()方法引導了服務器;

引導過程以下:

  • 建立一個ServerBoostrap的實例以引導和綁定服務器;
  • 建立並分配一個NioEventLoopGroup實例以進行事件的處理,如接受新鏈接以及讀、寫數據;
  • 指定服務器綁定本地的InetSocketAddress;
  • 使用一個EchoServerHandler的實例初始化每個新Channel;
  • 調用ServerBootstrap.bind()方法以綁定服務器;

EchoClient

Echo客戶端將會:

  • 鏈接到服務器;
  • 發送一個或多個消息;
  • 對於每一個消息,等待並接收從服務器返回的相同的消息;
  • 關閉鏈接

EchoClientHandler

package com.echo.client.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));
    }

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

EchoCleint

package com.echo.client.handler;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class EchoClient {
    
    private final String host;
    private final int port;
    
    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }
    
    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println(
                    "Usage: " + EchoClient.class.getSimpleName() +
                    " <host> <port>");
            return;
        }
        String host = args[0];
        int port = Integer.parseInt(args[1]);
        new EchoClient(host, port).start();
    }
    
    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .remoteAddress(new InetSocketAddress(host, port))
             .handler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new EchoClientHandler());
                }
                 
             });
            ChannelFuture f = b.connect().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

}

總結:

  • 爲初始化客戶端,建立了一個Bootstrap實例;
  • 爲進行事件處理分配了一個NioEventLoopGroup實例,其中事件處理包括建立新的鏈接以及處理入站和出站數據;
  • 爲服務器鏈接建立了一個InetSocketAddress實例;
  • 當鏈接被創建時,一個EchoClientHandler實例會被安裝到(該Channel的)ChannelPipeline中;
  • 在一切都設置完成後,調用Bootstrap.connect()方法鏈接到遠程節點;

運行測試

進入到echo-parent目錄下執行:

mvn clean package

而後分別在服務端和客戶端執行:

mvn exec:java

能夠在服務端看到:

$ mvn exec:java
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-server 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ echo-server ---

在客戶端看到:

$ mvn exec:java
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-client 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ echo-client ---
Client received: Netty rocks!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.903 s
[INFO] Finished at: 2018-11-18T20:54:08+08:00
[INFO] Final Memory: 11M/309M
[INFO] ------------------------------------------------------------------------

客戶端執行完畢並退出,回到服務端窗口能夠看到信息以下:

Server received: Netty rocks!
《Netty實戰》第二章
相關文章
相關標籤/搜索