Optimized code

This commit is contained in:
2021-01-19 18:39:42 +08:00
parent d34eddd492
commit e06dcecd75
9 changed files with 61 additions and 38 deletions

View File

@@ -11,8 +11,6 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import java.net.InetSocketAddress;
/**
* Http服务器使用Netty中的Http协议栈
* 实现中支持多条请求路径对于不存在的请求路径返回404状态码
@@ -33,12 +31,11 @@ public class HttpServer extends BaseServer {
super.init();
serverBootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE, false)
// .option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_BACKLOG, 1024)
.localAddress(new InetSocketAddress(port))
// .localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(defLoopGroup,
@@ -49,15 +46,17 @@ public class HttpServer extends BaseServer {
);
}
});
}
@Override
public void start() {
try {
channelFuture = serverBootstrap.bind().sync();
InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
logger.info("HttpServer start success, port is:{}", addr.getPort());
channelFuture = serverBootstrap.bind(port).sync();
logger.info("HttpServer start success, port is:{}", port);
// channelFuture = serverBootstrap.bind().sync();
// InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
// logger.info("HttpServer start success, port is:{}", addr.getPort());
} catch (InterruptedException e) {
logger.error("HttpServer start fail,", e);
}

View File

@@ -8,8 +8,6 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
/**
* @author ehlxr
*/
@@ -26,12 +24,11 @@ public class SdkServer extends BaseServer {
super.init();
serverBootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE, true)
// .option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_BACKLOG, 1024)
.localAddress(new InetSocketAddress(port))
// .localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(defLoopGroup,
@@ -46,9 +43,12 @@ public class SdkServer extends BaseServer {
@Override
public void start() {
try {
channelFuture = serverBootstrap.bind().sync();
InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
logger.info("SdkServer start success, port is:{}", addr.getPort());
channelFuture = serverBootstrap.bind(port).sync();
logger.info("SdkServer start success, port is:{}", port);
// channelFuture = serverBootstrap.bind().sync();
// InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
// logger.info("SdkServer start success, port is:{}", addr.getPort());
} catch (InterruptedException e) {
logger.error("SdkServer start fail,", e);
}

View File

@@ -32,11 +32,10 @@ public class SdkServerHandler extends SimpleChannelInboundHandler<SdkProto> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, SdkProto sdkProto) throws Exception {
// if (msg instanceof SdkProto) {
// SdkProto sdkProto = (SdkProto) msg;
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
try {
sdkProto.setDid(snowFlake.nextId());
ctx.channel().writeAndFlush(sdkProto).addListener((ChannelFutureListener) channelFuture -> semaphore.release());
} catch (Exception e) {
semaphore.release();
@@ -50,7 +49,6 @@ public class SdkServerHandler extends SimpleChannelInboundHandler<SdkProto> {
logger.warn(info);
throw new Exception(info);
}
// }
}
@Override