#如何访问哨兵模式实例
本指南演示如何使用行业标准的客户端库建立到 Redis 哨兵模式实例的连接。示例涵盖 go-redis、Jedis、Lettuce 和 Redisson 的配置细节。有关其他客户端选项,请参阅 Connect with Redis client API libraries。
#认证要求
Redis 哨兵模式实例支持以下认证选项:
- 密码认证:配置了密码时,所有客户端连接都必须提供有效凭据
- 无密码访问:如果在创建实例时禁用了 Set Password 选项,客户端可以在不进行认证的情况下连接
安全最佳实践
对于生产环境,强烈建议启用密码认证以保护您的数据。有关配置和维护安全凭据的详细说明,请参阅 User Management。
#连接端点参考
#集群内访问
对于部署在同一 Kubernetes 集群内的应用,可通过 Access Method 选项卡中 Access within the Cluster 部分获取内部访问端点。
| 参数 | 描述 |
|---|---|
| Connection Address | Redis 哨兵模式的 Kubernetes 服务名称和端口组合。 |
#集群外访问
对于从 Kubernetes 环境外部连接的应用,如果在实例创建期间已配置外部访问端点,则可在 Access Method 选项卡中 Access from outside the Cluster 部分找到这些端点。
| 参数 | 描述 |
|---|---|
| Sentinel Node Access Address | Redis 哨兵模式中 sentinel 的 Pod 的外部 IP 地址和端口,用于从 Kubernetes 网络外部建立连接。 |
#交互式调试
在实例详情页中,单击右上角的 Terminal Console,然后使用 redis-cli 命令连接到每个 Redis 节点。
redis-cli -h <internal-routing-ip> -p 6379下面显示了一个调试示例。 以下示例演示了 set/get 调试会话:
192.168.0.10:6379> set a 1
OK
192.168.0.10:6379> get a
"1"
192.168.0.10:6379>#客户端集成示例
以下示例演示了使用各种客户端库连接到 Redis 哨兵模式实例的最佳实践。
注意:在哨兵模式中注册的主从集群名称固定为
mymaster。
go-redis
Jedis
Lettuce
Redisson
package main
import (
"context"
"fmt"
"time"
// It is recommended to periodically upgrade to the latest version of the client for the latest bug fixes.
"github.com/redis/go-redis/v9"
)
func main() {
client := redis.NewFailoverClient(&redis.FailoverOptions{
SentinelAddrs: []string{"<address>"},
MasterName: "mymaster",
Password: "<password>",
OnConnect: func(ctx context.Context, conn *redis.Conn) error {
ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
return conn.Ping(ctx).Err()
},
// Client name for identification and tracking
ClientName: "go-demo",
// Using Context for timeout control
ContextTimeoutEnabled: true,
// Maximum number of retries
MaxRetries: 3,
// Minimum backoff time for retries
MinRetryBackoff: 20 * time.Millisecond,
// Maximum backoff time for retries
MaxRetryBackoff: 200 * time.Millisecond,
// Connection timeout
DialTimeout: 3 * time.Second,
// Read timeout
ReadTimeout: 5 * time.Second,
// Write timeout
WriteTimeout: 10 * time.Second,
// Connection pool size for each node
PoolSize: 100,
// Maximum wait time for available connections in the pool
PoolTimeout: time.Second,
// Minimum idle connections for each node
MinIdleConns: 5,
// Maximum idle connections for each node
MaxIdleConns: 10,
// Maximum active connections for each node
MaxActiveConns: 100,
// Maximum idle time for connections
ConnMaxIdleTime: time.Minute * 5,
})
defer client.Close()
if val, err := client.Get(context.TODO(), "test").Result(); err != nil {
panic(err)
} else {
fmt.Println(val)
}
}有关更详细的配置,请参阅 community documentation。
package io.alauda.demo.redis;
// It is recommended to periodically upgrade to the latest version of the client for the latest bug fixes.
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.HostAndPort;
import java.time.Duration;
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String []args) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
// Set the maximum size of the connection pool, -1 means unbounded. If the current number of connections exceeds this value, new connections will fail.
poolConfig.setMaxTotal(200);
// Set the maximum number of idle connections; idle connections exceeding this number will be released immediately.
poolConfig.setMaxIdle(10);
// Set the minimum number of idle connections; if the number falls below this, new idle connections will be created.
poolConfig.setMinIdle(3);
// Maximum wait time for business when there are no available connections and the maximum number of connections has been reached.
poolConfig.setMaxWait(Duration.ofSeconds(1));
// Validate the connection by pinging it when borrowing from the connection pool.
poolConfig.setTestOnBorrow(true);
// Test idle connections, finding and releasing invalid connections; this configuration only takes effect if timeBetweenEvictionRunsMillis is greater than 1ms.
poolConfig.setTestWhileIdle(true);
// Set the minimum idle time for connections; connections older than this will be released; -1 means do not release, this configuration only takes effect if timeBetweenEvictionRunsMillis is greater than 0. Default value is 30m.
poolConfig.setMinEvictableIdleDuration(Duration.ofMinutes(5));
// Number of connections validated on each idle connection check; -n means 1/n of the connections.
poolConfig.setNumTestsPerEvictionRun(-1);
// Eviction interval of time; -1 means disabled.
poolConfig.setTimeBetweenEvictionRuns(Duration.ofMinutes(1));
DefaultJedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
// Client name for debugging and tracking connection source.
.clientName("demo-jedis")
// TCP connection timeout
.connectionTimeoutMillis(2000)
// Command timeout
.timeoutMillis(10000)
.password("<password>")
.build();
DefaultJedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.connectionTimeoutMillis(2000)
.timeoutMillis(10000)
.build();
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("<ip1>", <port1>));
nodes.add(new HostAndPort("<ip2>", <port2>));
JedisSentinelPool pool = new JedisSentinelPool("mymaster", nodes, poolConfig, clientConfig, sentinelConfig);
try {
try (Jedis jedis = pool.getResource()) {
String val = jedis.get("test");
System.out.printf("%s", val);
}
} catch (Exception e) {
e.printStackTrace();
}
pool.close();
}
}有关更详细的配置,请参阅 community documentation。
package io.alauda.demo.redis;
// It is recommended to periodically upgrade to the latest version of the client for the latest bug fixes.
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import java.time.Duration;
import java.util.List;
public class Main {
public static void main(String[] args) {
RedisURI.Builder redisUriBuilder = RedisURI.builder();
redisUriBuilder.withSentinel(RedisURI.create("<ip1>", <port1>));
redisUriBuilder.withSentinel(RedisURI.create("<ip2>", <port2>));
redisUriBuilder.withPassword("<password>");
// Client name for debugging and tracking connection source.
redisUriBuilder.withClientName("demo-lettuce");
redisUriBuilder.withSentinelMasterId("mymaster");
// Set the client connection timeout
redisUriBuilder.withTimeout(Duration.ofSeconds(2));
RedisURI redisUri = redisUriBuilder.build();
// Configure SocketOptions
SocketOptions socketOptions = SocketOptions.builder()
// Set connection timeout
.connectTimeout(Duration.ofSeconds(10))
.tcpNoDelay(true)
// Enable TCP keepalive for quick detection of broken connections
// This value is not strictly necessary for non-pub/sub type requests; connectivity can be maintained through timely release of idle connections.
.keepAlive(true)
.build();
ClientOptions clientOptions = ClientOptions.builder()
.socketOptions(socketOptions)
// Connection auto-reconnect
.autoReconnect(true)
// Configure the client's behavior when a connection is disconnected
// DEFAULT - when autoReconnect = true, commands remaining will not be rejected; otherwise, commands will be rejected
.disconnectedBehavior(ClientOptions.DisconnectedBehavior.DEFAULT)
.build();
RedisClient redisClient = RedisClient.create(redisUri);
redisClient.setOptions(clientOptions);
// Pool configuration
GenericObjectPoolConfig<StatefulRedisConnection<String, String>> poolConfig = new GenericObjectPoolConfig<>();
// Connection pool size should not be too large, otherwise it may waste server connection resources, or even exhaust connection numbers leading to health check failures for the instance itself.
// Estimate your business concurrency; a suggested concurrency level is 1.2-1.5 times your business scale.
poolConfig.setMaxTotal(200);
// Minimum idle connections reserved to speed up business response.
poolConfig.setMinIdle(3);
// Maximum idle connections; idle connections exceeding this number will be released immediately.
poolConfig.setMaxIdle(10);
// Set the minimum idle time for connections; connections older than this will be released; -1 indicates no release; this configuration only takes effect if timeBetweenEvictionRunsMillis is greater than 0. Default value is 30m.
poolConfig.setMinEvictableIdleDuration(Duration.ofMinutes(5));
// Number of connections validated for each idle connection check; -n indicates 1/n of the connections.
poolConfig.setNumTestsPerEvictionRun(3);
// Eviction interval of time; -1 means disabled.
poolConfig.setTimeBetweenEvictionRuns(Duration.ofMinutes(1));
// Maximum wait time for business when there are no available connections and the maximum connection number has been reached.
poolConfig.setMaxWait(Duration.ofSeconds(1));
// Validate a connection by pinging it when borrowing from the connection pool.
poolConfig.setTestOnBorrow(true);
// Test idle connections, finding and releasing invalid connections; this configuration only takes effect if timeBetweenEvictionRunsMillis is greater than 1ms.
poolConfig.setTestWhileIdle(true);
GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(redisClient::connect, poolConfig);
try {
try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
String val = connection.sync().get("test");
System.out.printf("%s", val);
}
} catch (Exception e) {
e.printStackTrace();
}
pool.close();
}
}有关更详细的配置,请参阅 community documentation。
package io.alauda.demo.redis;
// It is recommended to periodically upgrade to the latest version of the client for the latest bug fixes.
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> nodes = new ArrayList<>();
nodes.add("redis://<ip1>:<port1>");
nodes.add("redis://<ip2>:<port2>");
Config config = new Config();
config.setNettyThreads(64)
.useSentinelServers()
.addSentinelAddress(nodes.toArray(new String[0]))
.setPassword("<password>")
.setMasterName("mymaster")
// Enable discovery for sentinel nodes.
.setSentinelsDiscovery(true)
// Test the connection every 30s for availability.
// If KeepAlive is enabled, this value can be correspondingly increased.
.setPingConnectionInterval(30000)
// Interval for scanning the cluster topology.
.setScanInterval(2000)
// Connection timeout.
.setConnectTimeout(10000)
// Command timeout.
.setTimeout(10000)
// Idle connection timeout, set to 60s to avoid default being too short.
.setIdleConnectionTimeout(60000)
// Connection pool size, should not be too large to avoid wasting server connection resources or exhausting connection numbers, leading to health check failures for the instance itself.
// Estimate your business concurrency; a suggested concurrency level is 1.2-1.5 times your business scale.
.setMasterConnectionPoolSize(200)
// Minimum idle connections reserved to speed up business response.
.setMasterConnectionMinimumIdleSize(3)
// Number of retries for failed commands.
.setRetryAttempts(3)
// Retry interval for failed commands.
.setRetryInterval(1500)
// Enable TCP KeepAlive mechanism for quick detection of broken connections.
.setKeepAlive(true)
// Enable TCP no delay.
.setTcpNoDelay(true)
// Client name for debugging and tracking connection source.
.setClientName("demo-redisson");
RedissonClient redissonClient = Redisson.create(config);
System.out.printf("%s", redissonClient.getBucket("test").get().toString());
redissonClient.shutdown();
}
}有关更详细的配置,请参阅 community documentation。