本文整理汇总了Java中io.netty.bootstrap.Bootstrap.remoteAddress方法的典型用法代码示例。如果您正苦于以下问题:Java Bootstrap.remoteAddress方法的具体用法?Java Bootstrap.remoteAddress怎么用?Java Bootstrap.remoteAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.bootstrap.Bootstrap
的用法示例。
在下文中一共展示了Bootstrap.remoteAddress方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newPool
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
@Override
protected FastdfsPool newPool(InetSocketAddress addr) {
if (LOG.isDebugEnabled()) {
LOG.debug("channel pool created : {}", addr);
}
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(loopGroup);
bootstrap.remoteAddress(addr);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
return new FastdfsPool(
bootstrap,
readTimeout,
idleTimeout,
maxConnPerHost
);
}
示例2: connectAsync
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) {
ethereumListener.trace("Connecting to: " + host + ":" + port);
EthereumChannelInitializer ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, remoteId);
ethereumChannelInitializer.setPeerDiscoveryMode(discoveryMode);
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.remoteAddress(host, port);
b.handler(ethereumChannelInitializer);
// Start the client.
return b.connect();
}
示例3: init
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
/**
* 初始化连接池
*/
public void init() {
bootstrap = new Bootstrap();
eventLoopGroup = new NioEventLoopGroup();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler());
//所有的公用一个eventloopgroup, 对于客户端来说应该问题不大!
poolMap = new AbstractChannelPoolMap<InetSocketAddress, FixedChannelPool>() {
@Override
protected FixedChannelPool newPool(InetSocketAddress key) {
return new FixedChannelPool(bootstrap.remoteAddress(key), new FixedChannelPoolHandler(), 2);
}
};
//预先建立好链接
serverListConfig.getAddressList().stream().forEach(address -> {
poolMap.get(address);
});
}
示例4: connectAsync
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) {
ethereumListener.trace("Connecting to: " + host + ":" + port);
EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance(remoteId);
ethereumChannelInitializer.setPeerDiscoveryMode(discoveryMode);
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.remoteAddress(host, port);
b.handler(ethereumChannelInitializer);
// Start the client.
return b.connect();
}
示例5: startClient
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
private void startClient(String hostName, int port, int id) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.attr(channelId, id)
.channel(NioSocketChannel.class)
.handler(clientChannelInitializer);
if (hostName != null && !"".equals(hostName)) {
bootstrap.remoteAddress(new InetSocketAddress(hostName, port));
} else {
bootstrap.remoteAddress(new InetSocketAddress(port));
}
try {
bootstrap.connect().sync().addListener(future -> logger.info("Channel「" + id + "」" + "已连接"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
示例6: main
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
final Bootstrap cb = new Bootstrap();
cb.group(group).channel(NioSocketChannel.class);
InetSocketAddress addr1 = new InetSocketAddress("10.0.0.10", 8888);
InetSocketAddress addr2 = new InetSocketAddress("10.0.0.11", 8888);
//连接池map
ChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
@Override
protected SimpleChannelPool newPool(InetSocketAddress key) {
return new SimpleChannelPool(cb.remoteAddress(key), new TestChannelPoolHandler());
}
};
final SimpleChannelPool pool1 = poolMap.get(addr1);//取出連接addr1地址的连接池
final SimpleChannelPool pool2 = poolMap.get(addr2);//取出連接addr2地址的连接池
Future<Channel> f1 = pool1.acquire();//获取一个连接
f1.addListener(new FutureListener<Channel>() {
@Override
public void operationComplete(Future<Channel> f) {
if (f.isSuccess()) {
Channel ch = f.getNow();
//连接地址1的某个channel
//使用连接发送消息
// ch.write(msg)
//用完释放
pool1.release(ch);
}
}
});
}
示例7: newPool
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
@Override
protected FastdfsPool newPool(InetSocketAddress addr) {
if (LOG.isDebugEnabled()) {
LOG.debug("channel pool created : {}", addr);
}
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(loopGroup);
bootstrap.remoteAddress(addr);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
return new FastdfsPool(bootstrap, readTimeout, idleTimeout, maxConnPerHost);
}
示例8: registerChannel
import io.netty.bootstrap.Bootstrap; //导入方法依赖的package包/类
/**
* 为服务提供者地址socketAddress注册新的Channel
*
* @param socketAddress
* @return
*/
public Channel registerChannel(InetSocketAddress socketAddress) {
try {
EventLoopGroup group = new NioEventLoopGroup(10); //这个设置的值后期优化
Bootstrap bootstrap = new Bootstrap();
bootstrap.remoteAddress(socketAddress);
bootstrap.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
//注册Netty编码器
ch.pipeline().addLast(new RpcEncoderHandler(serializeType));
//注册Netty解码器
ch.pipeline().addLast(new RpcDecoderHandler(RpcResponse.class, serializeType));
//注册客户端业务逻辑处理handler
ch.pipeline().addLast(new NettyClientInvokeHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect().sync();
final Channel newChannel = channelFuture.channel();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final List<Boolean> isSuccessHolder = Lists.newArrayListWithCapacity(1);
//监听Channel是否建立成功
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
//若Channel建立成功,保存建立成功的标记
if (future.isSuccess()) {
isSuccessHolder.add(Boolean.TRUE);
} else {
//若Channel建立失败,保存建立失败的标记
future.cause().printStackTrace();
isSuccessHolder.add(Boolean.FALSE);
}
connectedLatch.countDown();
}
});
connectedLatch.await();
//如果Channel建立成功,返回新建的Channel
if (isSuccessHolder.get(0)) {
return newChannel;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}