一、Redis 哨兵模式(Redis Sentinel) 在Redis高可用方案中,哨兵模式(Redis Sentinel)是最经典且官方的选择。它直接解决单机Redis宕机后如何快速恢复的问题:人工重启耗时较长,主从手动切换也不现实。Sentinel作为7×24小时监控集群的“监视员”,持续监控主
在Redis高可用方案中,哨兵模式(Redis Sentinel)是最经典且官方的选择。它直接解决单机Redis宕机后如何快速恢复的问题:人工重启耗时较长,主从手动切换也不现实。Sentinel作为7×24小时监控集群的“监视员”,持续监控主从节点状态,自动完成故障切换,并通知客户端新的主节点地址。借助哨兵,主节点宕机后的接管工作完全自动化,不再需要人工干预。

长期稳定更新的攒劲资源: >>>点此立即查看<<<
配套客户端Lettuce是基于Netty构建的高性能异步Redis客户端,也是Spring Boot 2.x及以上版本的默认Redis客户端。使用Lettuce时,只需配置哨兵节点地址和主节点名称,无需关心主从节点的具体机器——哨兵会动态告知Lettuce当前的主节点,实现无缝连接。
在实际项目中,Lettuce与Redisson并非对立关系,而是相互补充的协同组合。简单来说:Lettuce负责底层高性能基础通信(如命令发送、连接池管理),Redisson则基于Lettuce封装了大量开箱即用的分布式数据结构(如分布式锁、Map、队列等)。两者并用,既能享受Lettuce的极致性能,又能利用Redisson降低业务代码的复杂度。
org.springframework.boot spring-boot-starter-data-redis org.redisson redisson-spring-boot-starter 3.23.0
redis.lettuce.client.enable=true # 启用Lettuce客户端 # 连接池配置(核心) redis.lettuce.client.pool.minIdle=6 # 最小空闲连接(保活) redis.lettuce.client.pool.maxIdle=21 # 最大空闲连接 redis.lettuce.client.pool.maxTotal=100 # 最大连接数 redis.lettuce.client.pool.maxWaitMillis=4000 # 最大等待时间 redis.lettuce.client.sentinel.mastername=master # 监控的主节点名称 redis.lettuce.client.sentinel.password=*** redis.lettuce.client.sentinel.database=0 redis.lettuce.client.sentinel.nodeInfo[0]=10.193.114.0:26379 # 哨兵节点列表 redis.lettuce.client.sentinel.nodeInfo[0]=10.193.114.2:26379 # 哨兵节点列表 redis.lettuce.client.sentinel.nodeInfo[0]=10.193.114.3:26379 # 哨兵节点列表
场景 1:用 Lettuce 做基础缓存读写(Spring RedisTemplate)
Spring Data Redis已将Lettuce封装为熟悉的RedisTemplate,日常缓存读写场景直接使用即可。代码简洁,符合大多数项目的标准做法:
@Component
public class CacheService {
@Resource
private RedisTemplate redisTemplate;
public void setTestInfo(String testKey, Object testInfo) {
redisTemplate.opsForValue().set(
testKey,
testInfo,
1,
TimeUnit.HOURS
);
}
public Object getTestInfo(String testKey) {
return redisTemplate.opsForValue().get(testKey);
}
// 异步操作——高并发场景可多用
public void setTestInfoAsync(String testKey, Object testInfo) {
redisTemplate.opsForValue().setAsync(testKey, testInfo);
}
}
场景 2:用 Redisson 操作分布式 Map(对应 INFO:TEST_KEY)
Redisson提供的RMap相比Lettuce原生Hash操作更加方便——它自动处理序列化和反序列化,直接像Java Map一样使用:
@Component
public class TestMapService {
@Resource
private RedissonClient redissonClient;
public void batchSa veTestInfo(List testList) {
RMap testMap =
redissonClient.getMap("INFO:TEST_KEY");
testList.forEach(d -> {
String key = ...;
testMap.put(key, d);
});
}
public TestInfoPO getTestInfo(String testId) {
RMap testMap = redissonClient.getMap("INFO:TEST_KEY");
String key = ...;
return testMap.get(key);
}
}
场景 3:用 Redisson 实现分布式锁(防并发修改)
Redisson的分布式锁功能比手动使用SETNX加过期时间更可靠——它内置了看门狗机制,自动续期,避免死锁风险:
@Component
public class UpdateService {
@Resource
private RedissonClient redissonClient;
public void updateTestInfo(String testId, TestInfoPO newInfo) {
String lockKey = "lock:test:update:" + testId;
RLock lock = redissonClient.getLock(lockKey);
try {
boolean locked = lock.tryLock(5, 10, TimeUnit.SECONDS);
if (!locked) {
throw new RuntimeException("正在修改中,请稍后重试");
}
// 执行业务逻辑
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
}
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer
侠游戏发布此文仅为了传递信息,不代表侠游戏网站认同其观点或证实其描述