Search in sources :

Example 1 with StatefulRedisClusterPubSubConnection

use of io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection in project lettuce-core by lettuce-io.

the class RedisClusterClient method connectClusterPubSubAsync.

/**
 * Create a clustered 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<StatefulRedisClusterPubSubConnection<K, V>> connectClusterPubSubAsync(RedisCodec<K, V> codec) {
    if (partitions == null) {
        return Futures.failed(new IllegalStateException("Partitions not initialized. Initialize via RedisClusterClient.getPartitions()."));
    }
    topologyRefreshScheduler.activateTopologyRefreshIfNeeded();
    logger.debug("connectClusterPubSub(" + initialUris + ")");
    PubSubClusterEndpoint<K, V> endpoint = new PubSubClusterEndpoint<>(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);
    ClusterPubSubConnectionProvider<K, V> pooledClusterConnectionProvider = new ClusterPubSubConnectionProvider<>(this, clusterWriter, codec, endpoint.getUpstreamListener(), topologyRefreshScheduler);
    StatefulRedisClusterPubSubConnectionImpl<K, V> connection = new StatefulRedisClusterPubSubConnectionImpl<>(endpoint, pooledClusterConnectionProvider, clusterWriter, codec, getFirstUri().getTimeout());
    clusterWriter.setClusterConnectionProvider(pooledClusterConnectionProvider);
    connection.setPartitions(partitions);
    Supplier<CommandHandler> commandHandlerSupplier = () -> new PubSubCommandHandler<>(getClusterClientOptions(), getResources(), codec, endpoint);
    Mono<SocketAddress> socketAddressSupplier = getSocketAddressSupplier(connection::getPartitions, TopologyComparators::sortByClientCount);
    Mono<StatefulRedisClusterPubSubConnectionImpl<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 -> (StatefulRedisClusterPubSubConnection<K, V>) it).toFuture();
}
Also used : StatefulRedisClusterPubSubConnection(io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection) SocketAddress(java.net.SocketAddress) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) CommandExpiryWriter(io.lettuce.core.protocol.CommandExpiryWriter) Futures(io.lettuce.core.internal.Futures) LettuceAssert(io.lettuce.core.internal.LettuceAssert) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) Duration(java.time.Duration) Map(java.util.Map) PubSubEndpoint(io.lettuce.core.pubsub.PubSubEndpoint) URI(java.net.URI) CommandHandler(io.lettuce.core.protocol.CommandHandler) LettuceLists(io.lettuce.core.internal.LettuceLists) Exceptions(io.lettuce.core.internal.Exceptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) RedisCodec(io.lettuce.core.codec.RedisCodec) StatefulRedisClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection) TopologyRefreshEvent(io.lettuce.core.cluster.event.TopologyRefreshEvent) StatefulRedisPubSubConnectionImpl(io.lettuce.core.pubsub.StatefulRedisPubSubConnectionImpl) ClusterTopologyChangedEvent(io.lettuce.core.cluster.event.ClusterTopologyChangedEvent) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) DefaultEndpoint(io.lettuce.core.protocol.DefaultEndpoint) InternalLogger(io.netty.util.internal.logging.InternalLogger) KeyValueStreamingChannel(io.lettuce.core.output.KeyValueStreamingChannel) io.lettuce.core(io.lettuce.core) NodeSelectionSupport(io.lettuce.core.cluster.api.NodeSelectionSupport) ClusterTopologyRefresh(io.lettuce.core.cluster.topology.ClusterTopologyRefresh) ClientResources(io.lettuce.core.resource.ClientResources) TopologyComparators(io.lettuce.core.cluster.topology.TopologyComparators) StringCodec(io.lettuce.core.codec.StringCodec) PushHandler(io.lettuce.core.protocol.PushHandler) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) RedisAdvancedClusterAsyncCommands(io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) NodeConnectionFactory(io.lettuce.core.cluster.topology.NodeConnectionFactory) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisAdvancedClusterCommands(io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection) PubSubCommandHandler(io.lettuce.core.pubsub.PubSubCommandHandler) Mono(reactor.core.publisher.Mono) EventRecorder(io.lettuce.core.event.jfr.EventRecorder) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Closeable(java.io.Closeable) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Collections(java.util.Collections) TopologyComparators(io.lettuce.core.cluster.topology.TopologyComparators) CommandExpiryWriter(io.lettuce.core.protocol.CommandExpiryWriter) CommandHandler(io.lettuce.core.protocol.CommandHandler) PubSubCommandHandler(io.lettuce.core.pubsub.PubSubCommandHandler) PubSubEndpoint(io.lettuce.core.pubsub.PubSubEndpoint) DefaultEndpoint(io.lettuce.core.protocol.DefaultEndpoint) StatefulRedisClusterPubSubConnection(io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection) PubSubCommandHandler(io.lettuce.core.pubsub.PubSubCommandHandler) SocketAddress(java.net.SocketAddress)

Example 2 with StatefulRedisClusterPubSubConnection

use of io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceClusterPubSubConnection.

/**
 * 获取集群方式发布订阅连接
 *
 * @param lettuceClusterConnectSourceConfig
 * @return
 */
public StatefulRedisClusterPubSubConnection<String, String> getLettuceClusterPubSubConnection(LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(lettuceClusterConnectSourceConfig.getHosts().toString(), 0, lettuceClusterConnectSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), lettuceClusterConnectSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    List<RedisURI> nodeConfigs = new ArrayList<>(lettuceClusterConnectSourceConfig.getNodes().size());
    for (LettuceConnectSourceConfig redisSourceConfig : lettuceClusterConnectSourceConfig.getNodes()) {
        nodeConfigs.add(this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    }
    RedisClusterClient client = RedisClusterClient.create(res, nodeConfigs);
    client.setDefaultTimeout(Duration.ofMillis(lettuceClusterConnectSourceConfig.getSoTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClusterClientOptions(lettuceClusterConnectSourceConfig));
    return (StatefulRedisClusterPubSubConnection<String, String>) this.execute(() -> {
        return client.connectPubSub();
    });
}
Also used : RedisURI(io.lettuce.core.RedisURI) StatefulRedisClusterPubSubConnection(io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection) ArrayList(java.util.ArrayList) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Aggregations

StatefulRedisClusterPubSubConnection (io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection)2 ArrayList (java.util.ArrayList)2 LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)1 io.lettuce.core (io.lettuce.core)1 RedisURI (io.lettuce.core.RedisURI)1 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 NodeSelectionSupport (io.lettuce.core.cluster.api.NodeSelectionSupport)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 RedisAdvancedClusterAsyncCommands (io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands)1 RedisAdvancedClusterCommands (io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands)1 ClusterTopologyChangedEvent (io.lettuce.core.cluster.event.ClusterTopologyChangedEvent)1 TopologyRefreshEvent (io.lettuce.core.cluster.event.TopologyRefreshEvent)1 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)1 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)1 ClusterTopologyRefresh (io.lettuce.core.cluster.topology.ClusterTopologyRefresh)1 NodeConnectionFactory (io.lettuce.core.cluster.topology.NodeConnectionFactory)1 TopologyComparators (io.lettuce.core.cluster.topology.TopologyComparators)1 RedisCodec (io.lettuce.core.codec.RedisCodec)1 StringCodec (io.lettuce.core.codec.StringCodec)1