使用Netty做一个服务端,在SpringBoot启动后用FutureTask进行异步启动Netty。负责与物联网设备通信,有时候客户端与服务端通信时,造成服务端假死,网页访问速度慢的跟没有一样,那么发生假死的原因是是什么,怎么从解决。而且假死之前,没有抛出异常。不考虑在物联网设备上排查问题的方式。
由于服务端的handler的内包含的代码信息量有点大,我就简要说明一下,目前的handler只有一个的继承于ChannelInboundHandlerAdapter的类,没有其他的handler类。服务端有登录记录。若未登录,并且发出的找不到处理函数(java方法名)的命令,则建立连接发送消抛出异常时会断开连接,但登录后的客户端,若发出的找不到处理函数的命令,则会抛出异常,但不会断开连接。有几次设备登录后,不断发送数据,过了很长时间才有假死现象,有时候刚启动web程序不久,测试的物联网设备与服务端建立连接了几个消息一会就假死了。
这些假死是什么原因造成的,怎么从服务端避免这个情况发生?是不是漏掉flush?或者writeFlush?还是更复杂的原因?有相关的大佬能给一些提示和原因吗,并且有什么解决方法?
以下是服务端代码,和SpringBoot启动后异步启动Netty的代码
public class BaseNetty {
static EventLoopGroup bossGroup = new NioEventLoopGroup(); // 用来接收进来的连接
static EventLoopGroup workerGroup = new NioEventLoopGroup(); // 用来处理已经被接收的连接
private int port;
private Integer timeout=30;
public BaseNetty() {
}
public BaseNetty(int port) {
this.port = port;
}
public Integer getTimeout() {
return timeout;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public void StartServer() throws Exception {
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler( new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("监听到数据流");
if(timeout!=0){
ch.pipeline().addLast(new ReadTimeoutHandler(timeout));
}
ch.pipeline().addLast(new BaseNettyHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收进来的连接
ChannelFuture f = b.bind(port).sync();
// 等待服务器socket关闭
f.channel().closeFuture().sync();
} catch (Exception e) {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
啊
不知道怎么搞的,发断代码都内容违规。
