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实战》第二章