Search in sources :

Example 81 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project BRFS by zhangnianli.

the class NettyHttpServer method start.

@Override
public void start() throws InterruptedException {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup);
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.childHandler(handlerInitializer);
    // 积压数量
    serverBootstrap.option(ChannelOption.SO_BACKLOG, httpConfig.getBacklog());
    // 连接超时时间(毫秒)
    serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, httpConfig.getConnectTimeoutMillies());
    // 保持连接
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, httpConfig.isKeepAlive());
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, httpConfig.isTcpNoDelay());
    serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator());
    serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    InetSocketAddress address = (httpConfig.getHost() == null ? new InetSocketAddress(httpConfig.getPort()) : new InetSocketAddress(httpConfig.getHost(), httpConfig.getPort()));
    serverBootstrap.bind(address).sync();
}
Also used : AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 82 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project BRFS by zhangnianli.

the class TcpServer method start.

@Override
public void start() throws InterruptedException {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup);
    serverBootstrap.channel(NioServerSocketChannel.class);
    // 积压数量
    serverBootstrap.option(ChannelOption.SO_BACKLOG, config.getBacklog());
    // 保持连接
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator());
    serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    serverBootstrap.childHandler(channelInitializer);
    InetSocketAddress address = (config.getHost() == null ? new InetSocketAddress(config.getPort()) : new InetSocketAddress(config.getHost(), config.getPort()));
    serverBootstrap.bind(address).sync();
}
Also used : AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 83 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project brave by openzipkin.

the class ITNettyHttpTracing method init.

@Override
protected void init() {
    stop();
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(NettyHttpTracing.create(httpTracing).serverHandler());
            p.addLast(new TestHandler(httpTracing));
        }
    });
    try {
        Channel ch = b.bind(0).sync().channel();
        port = ((InetSocketAddress) ch.localAddress()).getPort();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new AssertionError(e);
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 84 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project brave by openzipkin.

the class NettyHttpServerBenchmarks method initServer.

@Override
protected int initServer() throws InterruptedException {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(new TracingDispatchHandler());
            p.addLast(new HelloWorldHandler());
        }
    });
    Channel ch = b.bind(0).sync().channel();
    return ((InetSocketAddress) ch.localAddress()).getPort();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RunnerException(org.openjdk.jmh.runner.RunnerException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 85 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project benchmark by seelunzi.

the class WebsocketChatServer method run.

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new WebsocketChatServerInitializer()).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        System.out.println("WebsocketChatServer 启动了");
        // 绑定端口,开始接收进来的连接
        ChannelFuture f = b.bind(port).sync();
        // 等待服务器  socket 关闭 。
        // 在这个例子中,这不会发生,但你可以优雅地关闭你的服务器。
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
        System.out.println("WebsocketChatServer 关闭了");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)448 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)246 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)239 Channel (io.netty.channel.Channel)187 ChannelFuture (io.netty.channel.ChannelFuture)183 EventLoopGroup (io.netty.channel.EventLoopGroup)168 Bootstrap (io.netty.bootstrap.Bootstrap)134 SocketChannel (io.netty.channel.socket.SocketChannel)126 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)115 InetSocketAddress (java.net.InetSocketAddress)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)98 ChannelPipeline (io.netty.channel.ChannelPipeline)88 LoggingHandler (io.netty.handler.logging.LoggingHandler)82 LocalServerChannel (io.netty.channel.local.LocalServerChannel)76 LocalChannel (io.netty.channel.local.LocalChannel)73 CountDownLatch (java.util.concurrent.CountDownLatch)66 Test (org.junit.jupiter.api.Test)66 LocalAddress (io.netty.channel.local.LocalAddress)61 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)60 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)57