云数据库 GAUSSDB NOSQL-通过Redisson连接实例:使用Sentinel模式连接实例

时间:2023-11-10 12:13:53

使用Sentinel模式连接实例

样例代码:

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();
    config.useSentinelServers()
            .setMasterName(master_name)
            .setCheckSentinelsList(false)
            .setReadMode(MASTER)
            .setPassword(password)
            .addSentinelAddress("redis://172.xx.xx.xx:8635");
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);      // send requests to database
    redisson.shutdown();
}

  public static void main(String[] args) {
    testSentinel();
  }
}
  • 上述代码中“172.xx.xx.xx”为待连接GaussDB(for Redis)实例的负载均衡地址。

    您可以单击实例名称,进入“基本信息”页面,在网络信息区域获取“负载均衡地址”。

    图2 查看负载均衡地址
  • 上述代码中的“8635”为待连接GaussDB(for Redis)实例的端口,具体请以实际端口为准。获取端口的方法请参见查看IP地址和端口
  • GaussDB(for Redis)只是实现了Sentinel模式的接入方式,不采用原生sentinel的可用性功能。所以样例代码中,master_name固定为“mymaster”,必须设置“CheckSentinelsList”为fasle,必须设置ReadMode为MASTER。
  • GaussDB(for Redis)支持与限制的命令,详情请参见开发规范
  • 由于GaussDB(for Redis)和开源Redis集群计算hash算法不同,因此GaussDB(for Redis)的部分命令需要给对应的key打上hash tag,否则会出现非预期的行为。hash tag使用建议参考开发规范
support.huaweicloud.com/productdesc-nosql/nosql_11_0202.html