FixBug NETTY

This commit is contained in:
limqhz
2020-05-24 23:28:52 +08:00
parent eb2cc8050d
commit 9ff67fa63d
2 changed files with 14 additions and 28 deletions

View File

@@ -69,7 +69,7 @@ public class BootService {
private Map<ChannelOption, Object> channelOptions; private Map<ChannelOption, Object> channelOptions;
@Autowired @Autowired
private ChannelHandler channelHandler; private ServerProtocolInitalizer serverProtocolInitalizer;
/** /**
* 初始化netty启动配置 * 初始化netty启动配置
@@ -79,17 +79,18 @@ public class BootService {
bossGroup = new NioEventLoopGroup(); bossGroup = new NioEventLoopGroup();
channelOptions = Maps.newHashMap(); channelOptions = Maps.newHashMap();
try { try {
channelOptions.put(ChannelOption.SO_KEEPALIVE,keepalive); // channelOptions.put(ChannelOption.SO_KEEPALIVE,keepalive);
channelOptions.put(ChannelOption.SO_BACKLOG,backlog); channelOptions.put(ChannelOption.SO_BACKLOG,backlog);
channelOptions.put(ChannelOption.TCP_NODELAY,TCP_NODELAY); // channelOptions.put(ChannelOption.TCP_NODELAY,TCP_NODELAY);
channelOptions.put(ChannelOption.SO_REUSEADDR,reuseaddr); channelOptions.put(ChannelOption.SO_REUSEADDR,reuseaddr);
bootstrap = new ServerBootstrap(); bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
bootstrap.channel(NioServerSocketChannel.class); .childHandler(serverProtocolInitalizer);
bootstrap.childHandler(channelHandler);
for (Map.Entry<ChannelOption, Object> entry : channelOptions.entrySet()) { for (Map.Entry<ChannelOption, Object> entry : channelOptions.entrySet()) {
bootstrap.option(entry.getKey(), entry.getValue()); bootstrap.option(entry.getKey(), entry.getValue());
} }
bootstrap.childOption(ChannelOption.TCP_NODELAY,nodelay);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive);
serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync(); serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync();
logger.info("成功bind端口:" + port); logger.info("成功bind端口:" + port);
@@ -106,7 +107,8 @@ public class BootService {
* 开启单独的线程运行netty服务,避免和spring mvc冲突 * 开启单独的线程运行netty服务,避免和spring mvc冲突
*/ */
public void run() { public void run() {
messageExecutor = Executors.newFixedThreadPool(1); // messageExecutor = Executors.newFixedThreadPool(1);
messageExecutor = Executors.newSingleThreadExecutor();
messageExecutor.execute(() -> init()); messageExecutor.execute(() -> init());
} }
@@ -123,10 +125,4 @@ public class BootService {
public void setChannelOptions(Map<ChannelOption, Object> channelOptions) { public void setChannelOptions(Map<ChannelOption, Object> channelOptions) {
this.channelOptions = channelOptions; this.channelOptions = channelOptions;
} }
public void setChannelHandler(ChannelHandler channelHandler) {
this.channelHandler = channelHandler;
}
} }

View File

@@ -8,10 +8,9 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.nio.charset.Charset;
/** /**
* 服务器端协议初始化适配器 * 服务器端协议初始化适配器
* *
@@ -21,11 +20,10 @@ import java.nio.charset.Charset;
@Component @Component
public class ServerProtocolInitalizer extends ChannelInitializer<SocketChannel> { public class ServerProtocolInitalizer extends ChannelInitializer<SocketChannel> {
private ChannelHandler handler; private final int IDLE_TIME = 30; //连接检测空闲时间
private final int IDLE_TIME = 15; //连接检测空闲时间 private final int READ_TIME = 30; //读超时时间
private final int READ_TIME = 15; //超时时间 private final int WRITE_TIME = 30; //超时时间
private final int WRITE_TIME = 15; //超时时间 private final int READ_TIMEOUT = 600; //超时时间
private final int READ_TIMEOUT = 300; //读超时时间
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
@@ -39,12 +37,4 @@ public class ServerProtocolInitalizer extends ChannelInitializer<SocketChannel>
} }
public ChannelHandler getHandler() {
return handler;
}
public void setHandler(ChannelHandler handler) {
this.handler = handler;
}
} }