使用StackExchange.Redis连接Redis集群调用HashGetAll时遇节点不可达异常
Hey there, let's work through this issue step by step—since you can access the cluster fine with redis-cli but StackExchange.Redis is throwing a slot access error, there are a few specific fixes tailored to your Windows + Docker setup that should resolve this:
1. Fix Redis Cluster's Advertised IP Address
The most common culprit here is that your Docker-hosted Redis nodes are advertising their internal Docker network IPs (like 172.18.0.2) to clients. While redis-cli might work (if you're running it inside the Docker network or using port mappings correctly), your Windows machine can't directly reach these internal container IPs. StackExchange.Redis gets these unreachable addresses from the cluster and fails to connect.
To fix this:
- When starting each Redis cluster node in Docker, use the
--cluster-announce-ipflag to specify your Windows host's accessible IP (e.g., your local network IP like192.168.1.100) and--cluster-announce-portto specify the mapped host port. - Recreate the cluster with these advertised endpoints so StackExchange.Redis receives reachable addresses instead of internal Docker IPs.
2. Increase Connection Timeouts
StackExchange.Redis has tighter default timeouts compared to redis-cli, which can cause it to fail before it can retry or resolve node connections. Try increasing these values in your connection setup:
In code:
var config = ConfigurationOptions.Parse("your-cluster-node-1:port,your-cluster-node-2:port"); config.ConnectTimeout = 5000; // 5 seconds (up from default 500ms) config.SyncTimeout = 5000; config.ConnectRetry = 3; // Add retries for transient network issues var multiplexer = ConnectionMultiplexer.Connect(config);
Or via connection string:redis://cluster-node-1:port,cluster-node-2:port?connectTimeout=5000&syncTimeout=5000&connectRetry=3
3. Verify Cluster Mode is Enabled in StackExchange.Redis
Double-check that your connection is explicitly configured to work with Redis Cluster. StackExchange.Redis doesn't enable cluster mode automatically in all cases:
- Add
clusterConfigCacheDuration=60to your connection string to ensure the client refreshes cluster slot mappings periodically. - If using code configuration, confirm you're setting up cluster-specific options:
config.ClusterConfiguration = new ClusterConfiguration(); config.AllowAdmin = true; // Helps with cluster discovery and slot mapping
4. Ensure Docker Port Mappings Are Complete
Redis Cluster uses two ports per node: the client port (default 6379) and the cluster bus port (default 16379). Missing mappings for the cluster bus port can break node discovery and slot routing.
When running a Redis container, map both ports:docker run -d -p 6379:6379 -p 16379:16379 redis:latest redis-server --cluster-enabled yes
If you're still having issues, enable StackExchange.Redis logging to get more details about connection attempts and slot routing:
config.LogTo = message => Console.WriteLine($"Redis Log: {message}");
内容的提问来源于stack exchange,提问作者Andrii Kapinos

