use of com.alipay.sofa.rpc.server.http.HttpServerHandler in project sofa-rpc by sofastack.
the class AbstractHttp2ServerTransport method start.
@Override
public boolean start() {
if (serverBootstrap != null) {
return true;
}
synchronized (this) {
if (serverBootstrap != null) {
return true;
}
boolean flag = false;
SslContext sslCtx = SslContextBuilder.build();
// Configure the server.
EventLoopGroup bossGroup = NettyHelper.getServerBossEventLoopGroup(transportConfig);
HttpServerHandler httpServerHandler = (HttpServerHandler) transportConfig.getServerHandler();
bizGroup = NettyHelper.getServerBizEventLoopGroup(transportConfig, httpServerHandler.getBizThreadPool());
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, bizGroup).channel(transportConfig.isUseEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, transportConfig.getBacklog()).option(ChannelOption.SO_REUSEADDR, transportConfig.isReuseAddr()).option(ChannelOption.RCVBUF_ALLOCATOR, NettyHelper.getRecvByteBufAllocator()).option(ChannelOption.ALLOCATOR, NettyHelper.getByteBufAllocator()).childOption(ChannelOption.SO_KEEPALIVE, transportConfig.isKeepAlive()).childOption(ChannelOption.TCP_NODELAY, transportConfig.isTcpNoDelay()).childOption(ChannelOption.SO_RCVBUF, 8192 * 128).childOption(ChannelOption.SO_SNDBUF, 8192 * 128).handler(new LoggingHandler(LogLevel.DEBUG)).childOption(ChannelOption.ALLOCATOR, NettyHelper.getByteBufAllocator()).childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(transportConfig.getBufferMin(), transportConfig.getBufferMax())).childHandler(new Http2ServerChannelInitializer(bizGroup, sslCtx, httpServerHandler, transportConfig.getPayload()));
// 绑定到全部网卡 或者 指定网卡
ChannelFuture future = serverBootstrap.bind(new InetSocketAddress(transportConfig.getHost(), transportConfig.getPort()));
ChannelFuture channelFuture = future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("HTTP/2 Server bind to {}:{} success!", transportConfig.getHost(), transportConfig.getPort());
}
} else {
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_HTTP2_BIND, transportConfig.getHost(), transportConfig.getPort()));
stop();
}
}
});
try {
channelFuture.await();
if (channelFuture.isSuccess()) {
flag = Boolean.TRUE;
} else {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_START_SERVER, "HTTP/2"), future.cause());
}
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
return flag;
}
}
Aggregations