Search in sources :

Example 1 with HttpServerHandler

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;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) SslContext(io.netty.handler.ssl.SslContext) HttpServerHandler(com.alipay.sofa.rpc.server.http.HttpServerHandler)

Aggregations

SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 HttpServerHandler (com.alipay.sofa.rpc.server.http.HttpServerHandler)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)1 EpollServerSocketChannel (io.netty.channel.epoll.EpollServerSocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 SslContext (io.netty.handler.ssl.SslContext)1 InetSocketAddress (java.net.InetSocketAddress)1