前言
因为以前在项目中使用过Mina框架,感受到了该框架的强大之处。于是在业余时间也学习了一下Netty。因为Netty的主要版本是Netty3和Netty4(Netty5已经被取消了),所以我就直接学习Netty4。在本文中演示的就是Netty的一个简单demo。
开发准备
Netty4的官方网站是:http://netty.io/ 。 本文使用的是Netty4.1。 由于Netty4.1版本需要JDK1.7,在使用JDK1.6时,会有一些bug,所以推荐使用JDK1.7。
因为Netty和Mina都是Trustin Lee的作品,所以在很多方面都十分相似,他们线程模型也是基本一致,采用了Reactors in threads模型,即Main Reactor + Sub Reactors的模式。
所以在开发上,很多都可以相互借鉴。
服务端
首先完成Netty服务端相关开发,和Mina一样。 主要由两个部分组成: 配置服务端的基本信息以及实现业务逻辑处理。 如果需要,还有filter过滤器。
服务端代码
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
*
* Title: NettyServer
* Description: Netty服务端
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public class NettyServer {
private static final int port =
6789;
private static EventLoopGroup group =
new NioEventLoopGroup();
private static ServerBootstrap b =
new ServerBootstrap();
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
**/
public static void main(String[] args)
throws InterruptedException {
try {
b.group(group);
b.channel(NioServerSocketChannel.class);
b.childHandler(
new NettyServerFilter());
ChannelFuture f = b.bind(port).sync();
System.out.println(
"服务端启动成功...");
f.channel().closeFuture().sync();
}
finally {
group.shutdownGracefully();
}
}
}
服务端业务逻辑
import java.net.InetAddress;
import java.util.Date;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
*
* Title: HelloServerHandler
* Description: 服务端业务逻辑
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public
class NettyServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)
throws Exception {
System.out.println(
"服务端接受的消息 : " + msg);
if(
"quit".equals(msg)){
ctx.close();
}
Date date=
new Date();
ctx.writeAndFlush(date+
"\n");
}
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
System.out.println(
"连接的客户端地址:" + ctx.channel().remoteAddress());
ctx.writeAndFlush(
"客户端"+ InetAddress.getLocalHost().getHostName() +
"成功与服务端建立连接! \n");
super.channelActive(ctx);
}
}
服务端过滤器
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
*
* Title: HelloServerInitializer
* Description: Netty 服务端过滤器
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public
class NettyServerFilter extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch)
throws Exception {
ChannelPipeline ph = ch.pipeline();
ph.addLast(
"framer",
new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
ph.addLast(
"decoder",
new StringDecoder());
ph.addLast(
"encoder",
new StringEncoder());
ph.addLast(
"handler",
new NettyServerHandler());
}
}
客户端
客户端的主要工作是 1,连接到服务端 2,向服务端发送数据数据 3,处理服务端返回的数据 4,关闭连接 而且客户端相关代码也和服务端类似。
客户端
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.io.IOException;
/**
*
* Title: NettyClient
* Description: Netty客户端
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public class NettyClient {
public static String host =
"127.0.0.1";
public static int port =
6789;
private static EventLoopGroup group =
new NioEventLoopGroup();
private static Bootstrap b =
new Bootstrap();
private static Channel ch;
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
**/
public static void main(String[] args)
throws InterruptedException, IOException {
System.out.println(
"客户端成功启动...");
b.group(group);
b.channel(NioSocketChannel.class);
b.handler(
new NettyClientFilter());
ch = b.connect(host, port).sync().channel();
star();
}
public static void star()
throws IOException{
String str=
"Hello Netty";
ch.writeAndFlush(str+
"\r\n");
System.out.println(
"客户端发送数据:"+str);
}
}
客户端业务逻辑实现
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
*
* Title: NettyClientHandler
* Description: 客户端业务逻辑实现
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public
class NettyClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)
throws Exception {
System.out.println(
"客户端接受的消息: " + msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
System.out.println(
"正在连接... ");
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx)
throws Exception {
System.out.println(
"连接关闭! ");
super.channelInactive(ctx);
}
}
客户端过滤器
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
*
* Title: NettyClientFilter
* Description: Netty客户端 过滤器
* Version:1.0.0
* @author Administrator
* @date 2017-8-31
*/
public
class NettyClientFilter extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch)
throws Exception {
ChannelPipeline ph = ch.pipeline();
ph.addLast(
"framer",
new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
ph.addLast(
"decoder",
new StringDecoder());
ph.addLast(
"encoder",
new StringEncoder());
ph.addLast(
"handler",
new NettyClientHandler());
}
}
效果实现图
服务端
客户端
demo教程到这里就结束了,如果有什么问题,欢迎提出!
该项目我放在github上了,有兴趣的可以看看!https://github.com/xuwujing/Netty