use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap 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();
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project bgpcep by opendaylight.
the class BGPDispatcherImpl method createClient.
@VisibleForTesting
public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.getKeyMapping(), reuseAddress);
clientBootStrap.localAddress(localAddress);
return createClient(remoteAddress, retryTimer, clientBootStrap);
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project bgpcep by opendaylight.
the class BmpDispatcherImpl method createClient.
@Override
public ChannelFuture createClient(final InetSocketAddress remoteAddress, final BmpSessionListenerFactory slf, final KeyMapping keys) {
final Bootstrap bootstrap = createClientBootstrap(this.sessionFactory, this.hf, BmpDispatcherUtil::createChannelWithDecoder, slf, remoteAddress, this.workerGroup, CONNECT_TIMEOUT, keys);
final ChannelFuture channelPromise = bootstrap.connect();
channelPromise.addListener(new BootstrapListener(bootstrap, remoteAddress, slf, keys));
LOG.debug("Initiated BMP Client {} at {}.", channelPromise, remoteAddress);
return channelPromise;
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project bgpcep by opendaylight.
the class BmpDispatcherUtil method createClientBootstrap.
public static Bootstrap createClientBootstrap(@Nonnull final BmpSessionFactory sessionFactory, @Nonnull final BmpHandlerFactory hf, @Nonnull CreateChannel createChannel, @Nonnull final BmpSessionListenerFactory slf, @Nonnull final InetSocketAddress remoteAddress, @Nullable final SocketAddress localAddress, @Nonnull final EventLoopGroup workerGroup, final int connectTimeout, @Nonnull final KeyMapping keys, boolean reuseAddress, boolean tryEpollSocket) {
final Bootstrap bootstrap = new Bootstrap();
bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
bootstrap.group(workerGroup);
bootstrap.handler(createChannel.create(sessionFactory, hf, slf));
if (localAddress != null) {
bootstrap.localAddress(localAddress);
}
bootstrap.remoteAddress(remoteAddress);
if (!tryEpollSocket) {
bootstrap.channel(NioSocketChannel.class);
} else {
if (Epoll.isAvailable()) {
bootstrap.channel(EpollSocketChannel.class);
} else {
bootstrap.channel(NioSocketChannel.class);
}
if (!keys.isEmpty()) {
if (Epoll.isAvailable()) {
bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
} else {
throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
}
}
}
return bootstrap;
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project incubator-pulsar by apache.
the class DiscoveryServiceTest method connectToService.
/**
* creates ClientHandler channel to connect and communicate with server
*
* @param serviceUrl
* @param latch
* @return
* @throws URISyntaxException
*/
public static NioEventLoopGroup connectToService(String serviceUrl, CountDownLatch latch, boolean tls) throws URISyntaxException {
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (tls) {
SslContextBuilder builder = SslContextBuilder.forClient();
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
X509Certificate[] certificates = SecurityUtility.loadCertificatesFromPemFile(TLS_CLIENT_CERT_FILE_PATH);
PrivateKey privateKey = SecurityUtility.loadPrivateKeyFromPemFile(TLS_CLIENT_KEY_FILE_PATH);
builder.keyManager(privateKey, (X509Certificate[]) certificates);
SslContext sslCtx = builder.build();
ch.pipeline().addLast("tls", sslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast(new ClientHandler(latch));
}
});
URI uri = new URI(serviceUrl);
InetSocketAddress serviceAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
b.connect(serviceAddress).addListener((ChannelFuture future) -> {
if (!future.isSuccess()) {
throw new IllegalStateException(future.cause());
}
});
return workerGroup;
}
Aggregations