netty 学习 (1)

来源:互联网 发布:小米手机移动网络开关 编辑:程序博客网 时间:2024/04/29 14:15
摘要: 本文是学习Netty的第一篇文章,主要对Netty的Server和Client间的通讯机制进行验证。
Server与Client建立连接后,会执行以下的步骤: 
1、Client向Server发送消息:Are you ok? 
2、Server接收客户端发送的消息,并打印出来。 
3、Server端向客户端发送消息:I am ok! 
4、Client接收Server端发送的消息,并打印出来,通讯结束。 

涉及到的类有4个: 
1、HelloServer :server类,启动Netty server 
2、HelloServerInHandler:server的handler,接收客户端消息,并向客户端发送消息 
3、HelloClient:client类,建立于Netty server的连接 

4、HelloClientIntHandler:client的handler,接收server端的消息,并向服务端发送消息

一、先加入必要的类库:

二、HelloServer代码如下:

package com.yao.netty;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;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.NioServerSocketChannel;public class HelloServer {public void start(int port) throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch)throws Exception {// 注册handlerch.pipeline().addLast(new HelloServerInHandler());}}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);ChannelFuture f = b.bind(port).sync();f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {HelloServer server = new HelloServer();server.start(8000);}}


三、 HelloServerInHandler代码如下: 
package com.yao.netty;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;// 该handler是InboundHandler类型public class HelloServerInHandler extends ChannelInboundHandlerAdapter {private static Log logger = LogFactory.getLog(HelloServerInHandler.class);@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {logger.info("HelloServerInHandler.channelRead");ByteBuf result = (ByteBuf) msg;byte[] result1 = new byte[result.readableBytes()];// msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中result.readBytes(result1);String resultStr = new String(result1);// 接收并打印客户端的信息System.out.println("Client said:" + resultStr);// 释放资源,这行很关键result.release();// 向客户端发送消息String response = "I am ok!";// 在当前场景下,发送的数据必须转换成ByteBuf数组ByteBuf encoded = ctx.alloc().buffer(4 * response.length());encoded.writeBytes(response.getBytes());ctx.write(encoded);ctx.flush();}@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {ctx.flush();}}


四、HelloClient代码如下:

package com.yao.netty;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;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;public class HelloClient {public void connect(String host, int port) throws Exception {EventLoopGroup workerGroup = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new HelloClientIntHandler());}});// Start the client.ChannelFuture f = b.connect(host, port).sync();// Wait until the connection is closed.f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {HelloClient client = new HelloClient();client.connect("127.0.0.1", 8000);}}


五、 HelloClientIntHandler代码如下:

package com.yao.netty;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class HelloClientIntHandler extends ChannelInboundHandlerAdapter {private static Log logger = LogFactory.getLog(HelloClientIntHandler.class);// 接收server端的消息,并打印出来@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {logger.info("HelloClientIntHandler.channelRead");ByteBuf result = (ByteBuf) msg;byte[] result1 = new byte[result.readableBytes()];result.readBytes(result1);System.out.println("Server said:" + new String(result1));result.release();}// 连接成功后,向server发送消息@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {logger.info("HelloClientIntHandler.channelActive");String msg = "Are you ok?";ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());encoded.writeBytes(msg.getBytes());ctx.write(encoded);ctx.flush();}}


六、还有log4j.xml文件:

<?xml version="1.0"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern" value="[%-5p] [%d] [%t] [%c] %m%n"/>        </layout>    </appender>        <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">        <param name="File" value="./log/netty.log"/>        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern" value="[%-5p] [%d] [%t] [%c] %m%n"/>        </layout>    </appender>        <appender name="FILE_ERR" class="org.apache.log4j.DailyRollingFileAppender">        <param name="File" value="./log/netty_err.log"/>        <param name="Threshold" value="ERROR" />        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern" value="[%-5p] [%d] [%t] [%c] %m%n"/>        </layout>    </appender>        <logger name="io.netty" additivity="false"><level value="INFO,DEBUG" /><appender-ref ref="FILE" /><appender-ref ref="FILE_ERR" /><appender-ref ref="CONSOLE" /></logger><logger name="com.yao" additivity="false"><level value="INFO,DEBUG" /><appender-ref ref="FILE" /><appender-ref ref="FILE_ERR" /><appender-ref ref="CONSOLE" /></logger>        <root>          <level value="debug"/>        <appender-ref ref="FILE"/>        <appender-ref ref="CONSOLE"/>        <appender-ref ref="FILE_ERR" />    </root></log4j:configuration>

总结: 
通过上面简单的实例可以发现: 
 1、在没有任何encoder、decoder的情况下,Netty发送接收数据都是按照ByteBuf的形式,其它形式都是不合法的。 
2、接收发送数据操作都是通过handler实现的,handler在netty中占据了非常重要的位置。 
 3、netty的handler是基于事件触发的,例如当client连接server成功后,client中的HelloClientIntHandler的channelActive方法会自动调用。 
0 0
原创粉丝点击