Netty系列之一:回显服务端和客户端

Netty是一款基于Java NIO的框架,能够建立通道、
处理事件、编解码和异常处理等,为上层应用提供了清晰、简洁的开发接口:减少用户的编码和错误,使应用开发者能够把注意力集中在业务逻辑上。

下面以回显功能为例:

[size=medium][color=brown]一、服务端:[/color][/size]

[size=medium]1. 实例化引导类[/size]

抽象类为AbstractBootstrap,服务端使用ServerBootstrap:

ServerBootstrap b = new ServerBootstrap();


[size=medium]2. 设置参数[/size]

Netty将EventLoopGroup, Channel, Address, Handler以及其它配置都放到了AbstractBootstrap中,统一设置:


[color=blue]a. 设置事件组[/color]

Netty是基于事件处理的,EventLoopGroup是接口,实现类有NioEventLoopGroup和OioEventLoopGroup (Old IO即Java IO) 等:


EventLoopGroup bossGroup = new NioEventLoopGroup(); // 接受连接事件组
EventLoopGroup workerGroup = new NioEventLoopGroup(); // 处理每个连接业务事件组
b.group(bossGroup, workerGroup); // 可以只使用一个group,分成两个group的好处是:业务耗时较长导致阻塞时,不会对接受连接造成影响。


[color=blue]b. 设置通道类型[/color]

接口为ServerSocketChannel,实现类有NioServerSocketChannel和OioServerSocketChannel。这里使用NioServerSocketChannel

b.channel(NioServerSocketChannel.class);


[color=blue]c. 设置服务启动绑定的地址[/color]

传入一个SocketAddress实例,指定端口即可(如8080):

b.localAddress(new InetSocketAddress(8080))

当然,也可以在真正绑定的时候设置:

ChannelFuture f = b.bind(new InetSocketAddress(8080)).sync();

调用sync()会等待前面的方法执行完毕,后面会有很多这样的写法。
ChannelFuture就是Channel的Future类,可以拿到执行结果。

[color=blue]d. 设置childHandler[/color]

实现ChannelInitializer接口的initChannel方法,将处理器(业务逻辑)加到通道管道的末尾:

b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
});


[size=medium]3. 绑定操作[/size]

和Socket的绑定类似,如果地址已经通过localAddress设定,这里就可以调用无参方法:

ChannelFuture f = b.bind().sync();


[size=medium]4. 等待结束[/size]

f.channel().closeFuture().sync();


[size=medium]5. 最终[/size]

在finally块中关闭事件组以及线程池等资源:

group.shutdownGracefully().sync();



[size=medium][color=brown]二、回显客户端:[/color][/size]

和服务端大体类似。不同的地方:

[size=medium]1'. 设置客户端引导类:[/size]

Bootstrap b = new Bootstrap();


[size=medium]2'. 设置参数:[/size]

[color=blue]a'. 设置事件组[/color]

一个客户端只有一个通道,一个group就够了:


EventLoopGroup group = new NioEventLoopGroup();
b.group(group);


[color=blue]b'. 设置通道类型[/color]

根据使用要求,也可以使用其它类型的客户端通道,如OioSocketChannel:

b.channel(NioSocketChannel.class);


[color=blue]c'. 设置连接地址:[/color]

因为是客户端,需要指定连接的服务端主机和端口:

b.remoteAddress(new InetSocketAddress(host, port));


[color=blue]d'. 设置事件处理类:[/color]

注意这里的方法是handler

b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});


[size=medium]3'. 连接:[/size]

这里是连接,而不是绑定。


ChannelFuture f = b.connect().sync();


如果远程地址没有在remoteAddress中设定,需要在连接时设置:

ChannelFuture f = b.connect("localhost", 8080);


[size=medium][color=brown]三、事件处理类[/color][/size]

EchoServerHandler,EchoClientHandler都是先往引导程序注册,事件发生时触发相应处理方法:

[color=blue]i. 服务器事件处理类[/color]

EchoServerHandler扩展io.netty.channel.[color=red]ChannelInboundHandlerAdapter[/color]类,重写下面三个方法,当然,可以根据需要重写更多的方法:

channelRead: 服务端收到客户端发来的数据

channelReadComplete: 服务端读取客户端数据完毕

exceptionCaught: 发生异常,比如客户端关闭连接时

[color=blue]ii. 客户端事件处理类[/color]

EchoClientHandler扩展io.netty.channel.[color=red]SimpleChannelInboundHandler[/color]类,重写下面三个方法:

channelActive: 连接已建立,这时可以向服务端发送数据

channelRead0: 服务端向客户端发送数据,可以读取

exceptionCaught: 发生异常,比如服务端关闭连接时

[size=medium][color=brown]四、代码[/color][/size]

[color=darkblue]回显服务端[/color]


public class EchoServer {

private final int port; // 服务端绑定端口

public EchoServer(int port) {
this.port = port;
}

public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // 接受连接事件组
EventLoopGroup workerGroup = new NioEventLoopGroup(); // 每个连接业务处理事件组

try {
ServerBootstrap b = new ServerBootstrap(); // 引导器
// 指定事件组,通道、绑定地址、业务处理器
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() { // 虽然EchoServerHandler和ChannelInitializer都是ChannelHandler的实现类,但这里不能直接传入EchoServerHandler,否则会导致业务处理器无法使用

@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 将业务处理器加到通道管理线(处理器队列)的末尾
ch.pipeline().addLast(new EchoServerHandler());
}
});

ChannelFuture f = b.bind().sync(); // 绑定指定端口
System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully().sync(); // 释放资源和线程池
}
}

public static void main(String[] args) throws Exception {
args = new String[1];
args[0] = "8180";

if (args.length != 1) {
System.err.println("?Usage: ?" + EchoServer.class.getSimpleName() + "<port>?");
}

int port = Integer.parseInt(args[0]);
new EchoServer(port).start();
}
}


[color=darkblue]客户端[/color]

public class EchoClient {
private final String host; // 服务器地址
private final int port; // 服务器端口

public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}

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());
}
});

// 连接到服务端,sync()阻塞直到连接过程结束
ChannelFuture f = b.connect().sync();

// 等待通道关闭
f.channel().closeFuture().sync();
} finally {
// 关闭引导器并释放资源,包括线程池
group.shutdownGracefully().sync();
}
}

public static void main(String[] args) throws Exception {
args = new String[2];
args[0] = "mysit.cnsuning.com";
args[1] = "8180";

if (args.length != 2) {
System.err.println("Usage: " + EchoClient.class.getSimpleName() + " <host> <port>");
return;
}

final String host = args[0];
final int port = Integer.parseInt(args[1]);
new EchoClient(host, port).start();
}
}


[color=orange]服务端处理[/color]


public class EchoServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println("Server received: " + ByteBufUtil.hexDump(buf.readBytes(buf.readableBytes()))); 缓冲内部存储读写位置,readBytes将指针后移
// System.out.println("?Server received: ?" + msg);
buf.resetReaderIndex(); // 重置读写位置,如果省略这一句,ctx.write(msg)往客户端发送的数据为空
ctx.write(msg);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// 读写完毕,调用flush将数据真正发送到客户端
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace(); // 打印异常
ctx.close(); // 关闭通道
}
}


[color=orange]客户端处理[/color]

@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 连接建立,向服务端发送数据
ctx.write(Unpooled.copiedBuffer("Hello Netty!", CharsetUtil.UTF_8));

// 注意:需要调用flush将数据发送到服务端
ctx.flush();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 打印异常并关闭通道
cause.printStackTrace();
ctx.close();
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
// 读取服务端返回的数据并打印
System.out.println("Client received: " + ByteBufUtil.hexDump(msg.readBytes(msg.readableBytes())));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值