服务端代码
除了Netty内置Handler以外,其余均为封装好的自定义Handler:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import netty.protocol.ProtocolFrameDecoder;
import netty.protocol.SharableMessageCodec;
import netty.server.handler.ChatRequestMessageHandler;
import netty.server.handler.LoginRequestMessageHandler;
@Slf4j
public class ChatServer {
public static void main(String[] args) {
NioEventLoopGroup boss = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
SharableMessageCodec MESSAGE_CODEC = new SharableMessageCodec();
LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler();
ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.group(boss, worker);
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new ProtocolFrameDecoder())
.addLast(LOGGING_HANDLER)
.addLast(MESSAGE_CODEC)
.addLast(LOGIN_HANDLER)
.addLast(CHAT_HANDLER);
}
});
Channel channel = serverBootstrap.bind(8080).sync().channel();
channel.closeFuture().sync();
} catch (Exception e) {
log.error("server error", e);
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}
问题描述
调试过程中发现,若同时启用多个客户端或者终止客户端之后再重新启动客户端,连接服务端时总是连接后立即断开,Debug级别下的命令行输出示例如下:
- 服务端:
09:36:23 [DEBUG] [nioEventLoopGroup-3-2] i.n.h.l.LoggingHandler - [id: 0x532b1d84, L:0.0.0.0/0.0.0.0:8080 ! R:/127.0.0.1:51432] REGISTERED
09:36:23 [DEBUG] [nioEventLoopGroup-3-2] i.n.h.l.LoggingHandler - [id: 0x532b1d84, L:0.0.0.0/0.0.0.0:8080 ! R:/127.0.0.1:51432] INACTIVE
09:36:23 [DEBUG] [nioEventLoopGroup-3-2] i.n.h.l.LoggingHandler - [id: 0x532b1d84, L:0.0.0.0/0.0.0.0:8080 ! R:/127.0.0.1:51432] UNREGISTERED
- 客户端:
09:36:22 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9] REGISTERED
09:36:22 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9] CONNECT: localhost/127.0.0.1:8080
09:36:23 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9, L:/127.0.0.1:51432 - R:localhost/127.0.0.1:8080] ACTIVE
09:36:23 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9, L:/127.0.0.1:51432 - R:localhost/127.0.0.1:8080] READ COMPLETE
09:36:23 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9, L:/127.0.0.1:51432 ! R:localhost/127.0.0.1:8080] INACTIVE
09:36:23 [DEBUG] [nioEventLoopGroup-2-1] i.n.h.l.LoggingHandler - [id: 0x3ea867b9, L:/127.0.0.1:51432 ! R:localhost/127.0.0.1:8080] UNREGISTERED
解决过程
服务端无法供多次连接,可能和添加进流水线的单实例Handler无法多次使用有关,对每个自定义Handler进行检查,发现问题所在:
@ChannelHandler.Sharable
public class SharableMessageCodec extends MessageToMessageCodec<ByteBuf, Message> { ... }
在此Handler类上加上@Sharable注解,再次启动客户端,问题解决。
其他注意事项
导入Maven项目时,需要注意标明作用的域以及需要使用的域是否对应,例如,引入LogBack时的依赖时,pom.xml代码如下:
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
若在运行客户端和服务端进行调试时,不是在test域下调用log引用进行输出,则需要注意调整其中的scope属性,否则会导致log的所有方法均无法调用。