use of io.lettuce.core.protocol.DefaultEndpoint in project lettuce-core by lettuce-io.
the class RedisClient method doConnectSentinelAsync.
private <K, V> ConnectionFuture<StatefulRedisSentinelConnection<K, V>> doConnectSentinelAsync(RedisCodec<K, V> codec, RedisURI redisURI, Duration timeout, String clientName) {
ConnectionBuilder connectionBuilder;
if (redisURI.isSsl()) {
SslConnectionBuilder sslConnectionBuilder = SslConnectionBuilder.sslConnectionBuilder();
sslConnectionBuilder.ssl(redisURI);
connectionBuilder = sslConnectionBuilder;
} else {
connectionBuilder = ConnectionBuilder.connectionBuilder();
}
connectionBuilder.clientOptions(ClientOptions.copyOf(getOptions()));
connectionBuilder.clientResources(getResources());
DefaultEndpoint endpoint = new DefaultEndpoint(getOptions(), getResources());
RedisChannelWriter writer = endpoint;
if (CommandExpiryWriter.isSupported(getOptions())) {
writer = new CommandExpiryWriter(writer, getOptions(), getResources());
}
if (CommandListenerWriter.isSupported(getCommandListeners())) {
writer = new CommandListenerWriter(writer, getCommandListeners());
}
StatefulRedisSentinelConnectionImpl<K, V> connection = newStatefulRedisSentinelConnection(writer, codec, timeout);
ConnectionState state = connection.getConnectionState();
state.apply(redisURI);
if (LettuceStrings.isEmpty(state.getClientName())) {
state.setClientName(clientName);
}
connectionBuilder.connectionInitializer(createHandshake(state));
logger.debug("Connecting to Redis Sentinel, address: " + redisURI);
connectionBuilder.endpoint(endpoint).commandHandler(() -> new CommandHandler(getOptions(), getResources(), endpoint)).connection(connection);
connectionBuilder(getSocketAddressSupplier(redisURI), connectionBuilder, connection.getConnectionEvents(), redisURI);
ConnectionFuture<?> sync = initializeChannelAsync(connectionBuilder);
return sync.thenApply(ignore -> (StatefulRedisSentinelConnection<K, V>) connection).whenComplete((ignore, e) -> {
if (e != null) {
logger.warn("Cannot connect Redis Sentinel at " + redisURI + ": " + e);
connection.closeAsync();
}
});
}
use of io.lettuce.core.protocol.DefaultEndpoint in project lettuce-core by lettuce-io.
the class RedisClusterClient method connectClusterAsync.
/**
* Create a clustered pub/sub connection with command distributor.
*
* @param codec Use this codec to encode/decode keys and values, must not be {@code null}
* @param <K> Key type
* @param <V> Value type
* @return a new connection
*/
private <K, V> CompletableFuture<StatefulRedisClusterConnection<K, V>> connectClusterAsync(RedisCodec<K, V> codec) {
if (partitions == null) {
return Futures.failed(new IllegalStateException("Partitions not initialized. Initialize via RedisClusterClient.getPartitions()."));
}
topologyRefreshScheduler.activateTopologyRefreshIfNeeded();
logger.debug("connectCluster(" + initialUris + ")");
DefaultEndpoint endpoint = new DefaultEndpoint(getClusterClientOptions(), getResources());
RedisChannelWriter writer = endpoint;
if (CommandExpiryWriter.isSupported(getClusterClientOptions())) {
writer = new CommandExpiryWriter(writer, getClusterClientOptions(), getResources());
}
if (CommandListenerWriter.isSupported(getCommandListeners())) {
writer = new CommandListenerWriter(writer, getCommandListeners());
}
ClusterDistributionChannelWriter clusterWriter = new ClusterDistributionChannelWriter(writer, getClusterClientOptions(), topologyRefreshScheduler);
PooledClusterConnectionProvider<K, V> pooledClusterConnectionProvider = new PooledClusterConnectionProvider<>(this, clusterWriter, codec, topologyRefreshScheduler);
clusterWriter.setClusterConnectionProvider(pooledClusterConnectionProvider);
StatefulRedisClusterConnectionImpl<K, V> connection = newStatefulRedisClusterConnection(clusterWriter, pooledClusterConnectionProvider, codec, getFirstUri().getTimeout());
connection.setReadFrom(ReadFrom.UPSTREAM);
connection.setPartitions(partitions);
Supplier<CommandHandler> commandHandlerSupplier = () -> new CommandHandler(getClusterClientOptions(), getResources(), endpoint);
Mono<SocketAddress> socketAddressSupplier = getSocketAddressSupplier(connection::getPartitions, TopologyComparators::sortByClientCount);
Mono<StatefulRedisClusterConnectionImpl<K, V>> connectionMono = Mono.defer(() -> connect(socketAddressSupplier, endpoint, connection, commandHandlerSupplier));
for (int i = 1; i < getConnectionAttempts(); i++) {
connectionMono = connectionMono.onErrorResume(t -> connect(socketAddressSupplier, endpoint, connection, commandHandlerSupplier));
}
return connectionMono.doOnNext(c -> connection.registerCloseables(closeableResources, clusterWriter, pooledClusterConnectionProvider)).map(it -> (StatefulRedisClusterConnection<K, V>) it).toFuture();
}
use of io.lettuce.core.protocol.DefaultEndpoint in project LinkAgent by shulieTech.
the class LettuceMethodInterceptor method appendEndPoint.
private void appendEndPoint(Object target, final SpanRecord spanRecord) {
try {
final Object connection = Reflect.on(target).get(LettuceConstants.REFLECT_FIELD_CONNECTION);
final Object t = Reflect.on(connection).get(LettuceConstants.REFLECT_FIELD_CHANNEL_WRITER);
;
DefaultEndpoint endpoint = null;
if ("io.lettuce.core.masterslave.MasterSlaveChannelWriter".equals(t.getClass().getName())) {
try {
/**
* 这是主从的
*/
MasterSlaveConnectionProvider provider = Reflect.on(t).get("masterSlaveConnectionProvider");
RedisURI redisUri = Reflect.on(provider).get("initialRedisUri");
spanRecord.setRemoteIp(redisUri.getHost());
spanRecord.setPort(redisUri.getPort());
} catch (Throwable thx) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
return;
/**
* 这是哨兵的
*/
} else if ("io.lettuce.core.masterslave.SentinelConnector$1".equals(t.getClass().getName())) {
try {
Object sentinelConnector = Reflect.on(t).get("this$0");
RedisURI redisURI = Reflect.on(sentinelConnector).get("redisURI");
List<RedisURI> sentinels = redisURI.getSentinels();
RedisURI current = sentinels.get(0);
spanRecord.setRemoteIp(current.getHost());
spanRecord.setPort(current.getPort());
} catch (Throwable thx) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
return;
}
if (t instanceof DefaultEndpoint) {
endpoint = (DefaultEndpoint) t;
} else {
try {
endpoint = Reflect.on(t).get(LettuceConstants.REFLECT_FIELD_DEFAULT_WRITER);
} catch (Throwable w) {
endpoint = Reflect.on(t).get(LettuceConstants.REFLECT_FIELD_WRITER);
}
}
if (endpoint == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
Channel channel = Reflect.on(endpoint).get(LettuceConstants.REFLECT_FIELD_CHANNEL);
if (channel == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
SocketAddress socketAddress = channel.remoteAddress();
if (socketAddress == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
if (!(socketAddress instanceof InetSocketAddress)) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
spanRecord.setRemoteIp(inetSocketAddress.getAddress().getHostAddress());
spanRecord.setPort(inetSocketAddress.getPort());
} catch (Throwable e) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
}
use of io.lettuce.core.protocol.DefaultEndpoint in project lettuce-core by lettuce-io.
the class RedisClient method connectStandaloneAsync.
private <K, V> ConnectionFuture<StatefulRedisConnection<K, V>> connectStandaloneAsync(RedisCodec<K, V> codec, RedisURI redisURI, Duration timeout) {
assertNotNull(codec);
checkValidRedisURI(redisURI);
logger.debug("Trying to get a Redis connection for: {}", redisURI);
DefaultEndpoint endpoint = new DefaultEndpoint(getOptions(), getResources());
RedisChannelWriter writer = endpoint;
if (CommandExpiryWriter.isSupported(getOptions())) {
writer = new CommandExpiryWriter(writer, getOptions(), getResources());
}
if (CommandListenerWriter.isSupported(getCommandListeners())) {
writer = new CommandListenerWriter(writer, getCommandListeners());
}
StatefulRedisConnectionImpl<K, V> connection = newStatefulRedisConnection(writer, endpoint, codec, timeout);
ConnectionFuture<StatefulRedisConnection<K, V>> future = connectStatefulAsync(connection, endpoint, redisURI, () -> new CommandHandler(getOptions(), getResources(), endpoint));
future.whenComplete((channelHandler, throwable) -> {
if (throwable != null) {
connection.closeAsync();
}
});
return future;
}
Aggregations