netty依赖调整,新增web服务启动逻辑

This commit is contained in:
aoshiguchen
2022-07-12 17:29:52 +08:00
parent 7f1488b570
commit d8690bc042
6 changed files with 63 additions and 9 deletions
+4
View File
@@ -13,6 +13,10 @@
<artifactId>neutrino-core</artifactId>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
@@ -549,7 +549,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
getOrNew(b);
b.run(getEnvironment().getMainArgs());
} catch (Exception e) {
// ignore
e.printStackTrace();
}
});
}
@@ -129,7 +129,7 @@ public class BeanWrapper implements LifeCycle {
return false;
}
public void run(String[] args) {
public void run(String[] args) throws Exception {
if (BeanStatus.INIT != status) {
return;
}
@@ -29,6 +29,6 @@ package fun.asgc.neutrino.core.context;
*/
public interface ApplicationRunner {
void run(String[] args);
void run(String[] args) throws Exception;
}
@@ -23,10 +23,21 @@ package fun.asgc.neutrino.core.web;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.Destroy;
import fun.asgc.neutrino.core.annotation.Init;
import fun.asgc.neutrino.core.context.ApplicationConfig;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import fun.asgc.neutrino.core.util.NumberUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;
/**
@@ -39,12 +50,55 @@ import lombok.extern.slf4j.Slf4j;
public class WebApplicationServer implements ApplicationRunner {
@Autowired
private ApplicationConfig rootApplicationConfig;
@Override
public void run(String[] args) {
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ServerBootstrap serverBootstrap;
private ChannelFuture channelFuture;
private byte[] faviconBytes;
@Init
public void init() {
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
if (StringUtil.notEmpty(http.getMaxContentLengthDesc()) && null == http.getMaxContentLength()) {
http.setMaxContentLength(NumberUtil.descriptionToSize(http.getMaxContentLengthDesc(), 64 * 1024));
}
this.bossGroup = new NioEventLoopGroup();
this.workerGroup = new NioEventLoopGroup();
this.serverBootstrap = new ServerBootstrap();
}
@Override
public void run(String[] args) throws Exception {
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
this.serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpObjectAggregator(http.getMaxContentLength().intValue()));
pipeline.addLast(new SimpleChannelInboundHandler<FullHttpRequest>() {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest request) throws Exception {
String uri = request.uri();
log.info("request: {}", uri);
}
});
}
});
channelFuture = this.serverBootstrap.bind(http.getPort()).sync();
log.info("HTTP服务启动,端口:{} context-path{}", http.getPort(), http.getContextPath());
}
@Destroy
public void destroy() {
this.channelFuture.channel().close();
}
}
-4
View File
@@ -13,10 +13,6 @@
<artifactId>neutrino-proxy-core</artifactId>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>fun.asgc.neutrino</groupId>
<artifactId>neutrino-core</artifactId>