更新时间:2025-09-28 GMT+08:00
使用Sentinel兼容模式连接实例
GeminiDB Redis采用自研的高可用服务HA组件,无需依赖Sentinel(哨兵)。但为了减少用户的代码改动,提高实例的兼容性,GeminiDB Redis兼容Redis Sentinel访问方式。开启Sentinel兼容模式,您可以像连接开源Redis Sentinel一样连接GeminiDB Redis实例。
前提条件
- 已成功创建GeminiDB Redis集群版或主备版实例,且实例状态为“正常”。
- 已创建弹性云服务器,创建弹性云服务器的方法,请参见《弹性云服务器快速入门》中“购买弹性云服务器”章节。
- 弹性云服务器上已经安装GCC等编译工具。
- 创建的弹性云服务器与GeminiDB Redis实例要保证区域、可用区、VPC和安全组一致。
- 使用Sentinel模式连接实例,必须要先开启Sentinel兼容模式。
开启Sentinel兼容模式
- 登录云数据库GeminiDB控制台。
- 在“实例管理”页面,选择指定的实例,单击实例名称,进入“基本信息”页面。
- 在左侧导航树,单击“参数修改”。
- 检查修改参数 “CompatibleMode”值 ,单击“保存”。
- 如果是集群版实例,修改参数“CompatibleMode”值为3。
- 如果是主备版实例,修改参数“CompatibleMode”值为2。
图1 设置参数
Sentinel模式连接实例
本示例以Java为例,通过开源库Redisson和Jedis访问GeminiDB Redis实例。
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import static org.redisson.config.ReadMode.MASTER;
public class SingleServerTests {
public static void testSentinel() {
Config config = new Config();
// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
String password = System.getenv("EXAMPLE_PASSWORD_ENV");
config.useSentinelServers()
.setMasterName("mymaster")
.setCheckSentinelsList(false)
.setReadMode(ReadMode.MASTER)
.setPassword(password)
.addSentinelAddress("redis://172.xx.xx.xx:6379");
RedissonClient redisson = Redisson.create(config);
execute(redisson); // send requests to database
redisson.shutdown();
}
public static void main(String[] args) {
testSentinel();
}
}
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class TestJedisSentinel {
public static void main(String[] args) {
Set<String> sentinels = new HashSet<>();
sentinels.add("192.xx.xx.xx:6379");
GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(100);
poolConfig.setMaxWaitMillis(10000);
poolConfig.setTestOnBorrow(true);
int connectionTimeout = 5000;
int soTimeout = 5000;
int database = 0;
// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
String password = System.getenv("EXAMPLE_PASSWORD_ENV");
try (JedisSentinelPool jspool = new JedisSentinelPool("mymaster", sentinels, poolConfig,
connectionTimeout, soTimeout, password, database)) {
Jedis jedis = jspool.getResource();
jedis.mset("testkey", "AAA", "b", "BBB");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- MasterName:为固定字符串“mymaster”。
- CheckSentinelsList:固定为“false”。
- ReadMode:使用ReadMode.MASTER。
- Password:实例对应的密码。
- SentinelAddress:为GeminiDB Redis实例的负载均衡地址,具体请以实际IP地址和Port为准。
您可以通过单击实例名称,进入“基本信息”页面,在连接信息区域获取“负载均衡地址”。
图2 查看负载均衡地址
GeminiDB Redis只是实现了Sentinel模式的接入方式,不采用原生Sentinel的可用性功能。所以样例代码中,“master_name”固定为“mymaster”,必须设置“CheckSentinelsList”为“false”,必须设置“ReadMode”为“MASTER”。