Search in sources :

Example 86 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project modules-extra by CubeEngine.

the class ApiServer method start.

/**
 * Starts the server
 *
 * @return fluent interface
 */
public ApiServer start() throws ApiStartupException {
    if (!this.isRunning()) {
        final ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            this.eventLoopGroup.set(new NioEventLoopGroup(this.maxThreads.get(), tf));
            serverBootstrap.group(this.eventLoopGroup.get()).channel(NioServerSocketChannel.class).childHandler(new ApiServerInitializer(cm, am, this)).localAddress(this.bindAddress.get(), this.port.get());
            this.bootstrap.set(serverBootstrap);
            this.channel.set(serverBootstrap.bind().sync().channel());
        } catch (Exception e) {
            this.bootstrap.set(null);
            this.channel.set(null);
            this.eventLoopGroup.getAndSet(null).shutdownGracefully(2, 5, TimeUnit.SECONDS);
            throw new ApiStartupException("The API server failed to start!", e);
        }
    }
    return this;
}
Also used : ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) UnknownHostException(java.net.UnknownHostException) ApiStartupException(org.cubeengine.module.apiserver.exception.ApiStartupException) ApiStartupException(org.cubeengine.module.apiserver.exception.ApiStartupException)

Example 87 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project eat by nhnent.

the class NettyClient method initEventLoopGroup.

public static void initEventLoopGroup() {
    int cntOfRealThread = Config.obj().getCommon().getCountOfRealThread();
    groups = new EventLoopGroup[cntOfRealThread];
    for (int i = 0; i < cntOfRealThread; i++) {
        groups[i] = new NioEventLoopGroup();
    }
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 88 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project xian by happyyangyuan.

the class RpcNettyClient method lazyInit.

/**
 * @param nodeId The node's id to which you want to initialize the connection. This method is thread-safe because it is synchronized.
 * @throws info.xiancloud.plugin.distribution.exception.ApplicationInstanceOfflineException Because the destination node is offline, of cause you cannot initialize the connection.
 * @throws Exception                                                                        Other unknown exceptions.
 */
private static void lazyInit(String nodeId) throws Exception {
    lock.lock();
    String host = null;
    int port = -1;
    try {
        if (channelAvailable(nodeId)) {
            LOG.debug(String.format("RpcClient:已经存在一个与%s的长连接,不再新建连接.", nodeId));
            return;
        }
        LOG.info(String.format("RpcClient:开始新建与%s的长连接...", nodeId));
        ApplicationInstance node = ApplicationRouter.singleton.getInstance(nodeId);
        // 如果是在同一台主机内部部署的两个节点,那么避免走交换机、路由器了
        host = Objects.equals(node.getAddress(), EnvUtil.getLocalIp()) ? "127.0.0.1" : node.getAddress();
        port = node.getPort();
        final SslContext sslCtx;
        if (SSL) {
            sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            sslCtx = null;
        }
        EventLoopGroup group = new NioEventLoopGroup(1);
        Bootstrap b = new Bootstrap();
        b.group(group).option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(// 10m
        10 * 1024 * 1024, // 20m
        20 * 1024 * 1024)).channel(NioSocketChannel.class).handler(new RpcNettyClientInitializer(sslCtx, nodeId)).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100);
        Channel connectedChannel = b.connect(host, port).sync().channel();
        connectedChannel.closeFuture().addListener(future -> {
            group.shutdownGracefully();
            LOG.info("The EventLoopGroup has been terminated completely and all Channels that belong to the group have been closed.");
        });
        nodeId_to_connectedChannel_map.put(nodeId, connectedChannel);
        LOG.info(new JSONObject() {

            {
                put("toNodeId", nodeId);
                put("rpcRemoteAddress", connectedChannel.remoteAddress().toString());
                put("type", "rpcChannelConnected");
                put("description", String.format("RpcClient:与%s的长连接建立完毕, remoteAddress=%s", nodeId, connectedChannel.remoteAddress()));
            }
        }.toJSONString());
    } catch (Throwable e) {
        throw new Exception(String.format("与远程节点%s建立长连接失败:host=%s,port=%s", nodeId, host, port), e);
    } finally {
        lock.unlock();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ApplicationInstanceOfflineException(info.xiancloud.core.distribution.exception.ApplicationInstanceOfflineException) ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) JSONObject(com.alibaba.fastjson.JSONObject) Bootstrap(io.netty.bootstrap.Bootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 89 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project xian by happyyangyuan.

the class RpcNettyServer method start.

private void start() throws Exception {
    if (Node.RPC_PORT < 0) {
        LOG.error("No rpc port is specified, rpc server starting failed.");
        return;
    }
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(1);
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(// 10m
    10 * 1024 * 1024, // 20m
    20 * 1024 * 1024)).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new RpcServerInitializer(sslCtx));
    parentChannel = b.bind(Node.RPC_PORT).sync().channel();
    parentChannel.closeFuture().addListener(future -> {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
        LOG.info("The EventLoopGroup has been terminated completely and all Channels that belong to the group have been closed.");
    });
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContext(io.netty.handler.ssl.SslContext)

Example 90 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project tephra by heisedebaise.

the class ServerImpl method listen.

@Override
public void listen(ServerListener listener) {
    if (listener == null || listener.getPort() < 1)
        return;
    if (logger.isDebugEnable())
        logger.debug("启动监听服务[{}]。", listener.getPort());
    this.listener = listener;
    group = new NioEventLoopGroup(listener.getMaxThread());
    new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 2048).childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.TCP_NODELAY, true).childHandler(this).bind(listener.getPort()).syncUninterruptibly();
    if (logger.isInfoEnable())
        logger.info("监听服务[{}]已启动。", listener.getPort());
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)521 EventLoopGroup (io.netty.channel.EventLoopGroup)256 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)226 Bootstrap (io.netty.bootstrap.Bootstrap)184 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)178 SocketChannel (io.netty.channel.socket.SocketChannel)169 ChannelFuture (io.netty.channel.ChannelFuture)157 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)157 Channel (io.netty.channel.Channel)138 InetSocketAddress (java.net.InetSocketAddress)118 LoggingHandler (io.netty.handler.logging.LoggingHandler)87 ChannelPipeline (io.netty.channel.ChannelPipeline)84 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)73 SslContext (io.netty.handler.ssl.SslContext)64 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)49 IOException (java.io.IOException)49 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)47 ByteBuf (io.netty.buffer.ByteBuf)44 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)43 ChannelInitializer (io.netty.channel.ChannelInitializer)41