Netty的核心組件:java
Channel
Channel能夠看作是傳入(入站)或者傳出(出站)數據的載體,,能夠被打開或者關閉,鏈接或者斷開鏈接;apache
回調
一個回調其實就是一個方法,一個指向已經被提供給另一個方法的方法的引用。這使得接受回調的方法能夠在適當的時候調用前者。當一個回調被觸發時,相關的事件能夠被一個interface-ChannelHandler的實現處理。bootstrap
Future
Future提供了另外一種在操做完成時通知應用程序的方式。這個對象能夠看作是一個異步操做的結果的佔位符,它將在將來的某個時刻完成,並提供對其結果的訪問。
ChannelFuture提供了額外幾個方法,使得咱們可以註冊一個或者多個ChannelFutureListener實例。由ChannelFutureListener實例提供的通知機制消除了手動檢查對應的操做是否完成的必要。安全
事件和 ChannelHandler
Netty使用不一樣的事件來通知咱們狀態的改變或者是操做的狀態。這使得咱們可以基於已經發生的事件來觸發適當的動做。每一個事件均可以被分發給ChannelHandler類中的某個用戶實現的方法。服務器
整個項目的目錄結構以下:異步
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-client 和 echo-serveroop
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()方法時將會嘗試寫出到套接字。
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(); } }
總結:
引導過程以下:
Echo客戶端將會:
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(); } }
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(); } } }
總結:
進入到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實戰》第二章