目录
哨兵搭建
windios环境下(对应windows安装目录)
(41条消息) Windows 下 redis 哨兵模式(一主一从两哨兵)_喊我小王吧!的博客-优快云博客
linux环境下(对应linux安装目录)
Redis哨兵(Sentinel)模式 - 简书 (jianshu.com)
配置文件里一定要加上
bind 0.0.0.0
(41条消息) bind 0.0.0.0的作用是什么呢?_weixin_33754913的博客-优快云博客
客户端操作(获取连接)
lettuce
参考(44条消息) Redis高级客户端Lettuce详解_huayang183的博客-优快云博客_lettuce
中高可用和分片章节
@Test
public void testDynamicSentinel() throws Exception {
RedisURI redisUri = RedisURI.builder()
.withPassword("你的密码")
.withSentinel("localhost", 26379)
.withSentinelMasterId("哨兵Master的ID")
.build();
RedisClient redisClient = RedisClient.create();
StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave.connect(redisClient, new Utf8StringCodec(), redisUri);
// 只容许从从节点读取数据
connection.setReadFrom(ReadFrom.SLAVE);
RedisCommands<String, String> command = connection.sync();
SetArgs setArgs = SetArgs.Builder.nx().ex(5);
command.set("name", "throwable", setArgs);
}
新版lettuce客户端方法有写不同(原方法被删除,用另外两个类替代)
(44条消息) Redis【有与无】【Lettuce】L4.Redis Sentinel_琴韵悠悠的博客-优快云博客
SpringBoot 连接
注意:在主节点挂掉后,哨兵节点进行主从切换时,客户端不能连接哨兵节点。
添加配置文件即可
spring:
redis:
# Redis数据库索引(默认为0)
database: 10
# Redis服务器地址
#host: 192.168.203.220
# Redis服务器连接端口
#port: 6379
# Redis服务器连接密码(默认为空)
password: redis2018
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 200
# 连接池中的最大空闲连接
max-idle: 20
# 连接池中的最小空闲连接
min-idle: 0
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
# 连接超时时间(毫秒)默认是2000ms
timeout: 2000ms
sentinel:
#哨兵监听的master名称
master: mymaster
#哨兵地址列表,多个以,分割
nodes: 192.168.203.205:26479
哨兵原理
大佬文章链接
(46条消息) Redis哨兵集群中哨兵挂了,主从库还能切换吗?_码农架构的博客-优快云博客_redis 哨兵挂了(包含原理)
(44条消息) 《Redis的哨兵机制》 模式 原理详解,其实很简单_詠聖wK的博客-优快云博客_redis哨兵模式原理
(44条消息) Redis的哨兵机制 或者心跳机制 模式 原理详解_allsmallpig的博客-优快云博客_redis哨兵模式原理