tencent cloud

腾讯云分布式缓存数据库(兼容 Redis)

动态与公告
产品动态
公告
新手指引
产品简介
产品概述
产品优势
应用场景
存储引擎
产品系列
产品版本
规格与性能
读写分离
多可用区部署
地域和可用区
名词解释
购买指南
计费概述
定价中心
购买实例
续费说明(包年包月)
退费说明(包年包月)
欠费说明
按量转包年包月
快速入门
快速创建实例
连接 Redis 实例
操作指南
操作总览
连接数据库实例
管理实例
升级实例
管理节点(Redis/ValKey 版)
管理多可用区
备份与恢复
账号管理
参数配置
慢查询
访问管理
网络与安全
监控与告警
事件管理(Redis/ValKey 版)
数据迁移
Redis 版全球复制
数据库审计
诊断优化
Sentinel 模式
开发准则
命名规则
基本使用准则
Key 与 Value 设计原则
命令使用准则
客户端程序设计准则
连接池配置
命令参考
命令参考概览
Redis 版与 Valkey 版命令兼容性
大版本命令使用差异
Proxy 架构与直连模式的使用差异
命令更多操作(Redis/Valkey 版)
Memcached 版命令兼容性
实践教程
基于 Spring Boot 搭建 Redis 客户端监控
Redis 客户端连接配置策略与实践
集群架构全局 SCAN 使用指南
实例安全下线
热 Key 与 大 key
可用区迁移方案
故障处理
连接异常
Redisson 客户端超时重连异常分析及解决方案
性能排查与调优
API 文档
History
Introduction
API Category
Making API Requests
Instance APIs
Parameter Management APIs
Other APIs
Backup and Restoration APIs
Region APIs
Monitoring and Management APIs
Log APIs
Data Types
Error Codes
常见问题
使用常见问题
连接登录问题
购买相关问题
相关协议
服务等级协议
Terms of Service
词汇表
联系我们

Sentinel 模式

PDF
聚焦模式
字号
最后更新时间: 2026-03-17 18:10:57

操作场景

Sentinel(哨兵)是一个独立运行的进程,用于监控腾讯云分布式缓存数据库集群中主从节点的状态,主节点异常时,Sentinel 可以在从节点选举出新的主节点,自动替代原主节点,保障业务平稳运行,是一种高可用解决方案。
说明:
Memcached 版不支持 Sentinel 模式。

Sentinel 相关命令

腾讯云分布式缓存数据库 4.0及以上版本均默认支持 Sentinel(哨兵)模式,您可以使用如下 Sentinel 相关命令。

SENTINEL sentinels

列出所监控的 master 相关的 sentinels 信息。

命令格式

SENTINEL sentinels <任意名称>

使用示例



SENTINEL get-master-addr-by-name

获取 master-name 相关的 ip addr 的信息。

命令格式

SENTINEL get-master-addr-by-name <任意名称>

使用示例



Sentinel 模式连接示例

准备工作

腾讯云分布式缓存数据库Redis 版本为4.0 或 5.0。
数据库实例运行状态正常,处于运行中
腾讯云分布式缓存数据库控制台实例详情页面的网络信息区域,获取连接数据库的内网 IPv4 地址及端口。具体信息,请参见 查看实例详情
已获取访问数据库的账号与密码。具体操作,请参见 管理账号
下载客户端 Jedis,推荐使用最新版本。

连接示例

下述示例代码以 Jedis 客户端的3.6.0版本为例,推荐使用最新版本。
Jedis 为3.6.0版本及以上。
Lettuce 为5.3.0.RELEASE 版本及以上。
Spring Data Redis 为2.5.1版本及以上,Spring Data Redis 需要配置 spring.redis.sentinel.password 参数。
您需要根据注释修改参数:连接数据库的 IP、端口及账号密码信息。

通过 Java 方式连接

package com.example.demo;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

public class Main {
public static void main(String[] args) {
String masterName = "test";
Set<String> sentinels = new HashSet<>();
//如下您需要配置数据库实例的内网IPv4地址及端口
sentinels.add("XX.XX.XX.XX:6379");
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
String dbPassword = "root:xxx";//您需替换访问数据库的密码
String sentinelPassword = "root:xxx";//您需替换访问数据库的密码

JedisSentinelPool jedisSentinelPool =
new JedisSentinelPool(masterName, sentinels, poolConfig,
2000, 2000, dbPassword,
0, null, 2000, 2000,
sentinelPassword, null);
System.out.println("jedisSentinelPool.getResource().ping() = " + jedisSentinelPool.getResource().ping());
jedisSentinelPool.close();
}
}

通过 Spring Data 框架连接

package com.example.demo;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@SpringBootApplication

public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

@Configuration
class RedisConfig {
@Bean
@Qualifier("jedisConnectionFactory")
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("test")
.sentinel("XX.XX.XX.XX", 6379);//您需要替换为数据库实例的内网IPv4地址及端口
sentinelConfig.setPassword(RedisPassword.of("xxx"));//您需替换访问数据库的密码
sentinelConfig.setSentinelPassword(RedisPassword.of("xxx"));//您需替换访问数据库的密码
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
connectionFactory.afterPropertiesSet();
return connectionFactory;
}

@Bean
@ConditionalOnBean(JedisConnectionFactory.class)
public RedisTemplate<String, String> redisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.afterPropertiesSet();
//test
template.opsForValue().set("test", "test1");
System.out.println("template.opsForValue().get(\\"test\\") = " + template.opsForValue().get("test"));
return template;
}

}

帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈