https://www.cnblogs.com/xsj891107/p/7486209.html https://blog.csdn.net/u010648555/article/details/79427606 redis.conf
port 6379 #bind 127.0.0.1 protected-mode no #从节点也就可以进行写的操作 slave-read-only nosentinel.conf
port 26379 #1表示在sentinel集群中只要有两个节点检测到redis主节点出故障就进行切换,单sentinel节点无效(自己测试发现的) #如果3s内mymaster无响应,则认为mymaster宕机了 #如果10秒后,mysater仍没活过来,则启动failover sentinel monitor mymaster 127.0.0.1 6379 1 sentinel down-after-milliseconds mymaster 3000 sentinel failover-timeout mymaster 10000 daemonize yes #指定工作目录 #dir "/home/redis/sentinel-work" protected-mode no #logfile "/home/redis/sentinellog/sentinel.log" #redis主节点密码 sentinel auth-pass mymaster 123456 masterauth 123456 requirepass 123456master通过requirepass设置自身的密码,不提供密码无法连接到这个master。 slave通过masterauth来设置访问master时的密码。
当使用了sentinel时,由于一个master可能会变成一个slave,一个slave也可能会变成master,所以需要在master 和slave的配置文件中同时都要设置上述两个配置项,才能多次切换,否则就有可能只能切换一次。 運行命令:
redis-server.exe redis.conf redis-server.exe sentinel.conf --sentinel 查看站點信息 info Replicationspring-boot配置:
redis: #数据库索引 database: 1 #host: 127.0.0.1 #port: 6379 password: 123456 #连接超时时间 timeout: 5000 lettuce: pool: #最大连接数 max-active: 50 #最大阻塞等待时间(负数表示无限制) max-wait: -1 #最大空闲 max-idle: 20 #最小空闲 min-idle: 10 #修改 sentinel: master: mymaster nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381springboot pom.xml配置
<!-- springboot 1.4以上版本--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--spring boot 集成redis所需common-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>${commons-pool2.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>