安装编译依赖
sudo apt install gcc
sudo apt-get install pkg-config
sudo apt-get install build-essential
遇到错误:
jemalloc/jemalloc.h: No such file or directory
解决方法:
make distclean && make
sentinel 配置
sentinel节点配置
源码下有sentinel配置文件示例,自带说明:sentinel.conf
最简单的配置:
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5
配置项 |
参数类型 |
作用 |
port |
数字 |
哨兵进程的端口 |
dir |
绝对路径 |
临时文件夹。如果一个机器部署多个sentinel,需要配置不同的路径 |
sentinel down-after-milliseconds |
<服务名称><毫秒> |
单个哨兵认为的主观下线时间 |
sentinel parallel-syncs |
<服务名称><服务器数> |
多少个Redis服务同步新的主机 |
sentinel failover-timeout |
<服务名称><毫秒数> |
指定故障切换允许的毫秒数,超过这个时间,就认为故障切换失败 |
sentinel notification-script |
<服务名称><脚本路径> |
指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本 |
sentinel 部署模型
M: master
R: replica
S: sentinel
C: client
- 基本模型
+----+
| M1 |
| S1 |
+----+
|
+----+ | +----+
| R2 |----+----| R3 |
| S2 | | S3 |
+----+ +----+
Configuration: quorum = 2
如果是M和R之间异步复制,当M宕机的时候存在丢失数据的可能。通过设置备份策略防止这种情况。master必须将数据至少写到一个R上才算成功。
min-replicas-to-write 1
min-replicas-max-lag 10
- sentinel 和client在同一台机器上
当部署Redis的机器只有两台,可以将sentinel和client放在一起。
+----+ +----+
| M1 |----+----| R1 |
| | | | |
+----+ | +----+
|
+------------+------------+
| | |
| | |
+----+ +----+ +----+
| C1 | | C2 | | C3 |
| S1 | | S2 | | S3 |
+----+ +----+ +----+
Configuration: quorum = 2
- 客户端少于三个
如果Redis机器和客户端都是少于两个,可以考虑将sentinel放在Redis和client节点。
+----+ +----+
| M1 |----+----| R1 |
| S1 | | | S2 |
+----+ | +----+
|
+------+-----+
| |
| |
+----+ +----+
| C1 | | C2 |
| S3 | | S4 |
+----+ +----+
Configuration: quorum = 3
启动sentinel
redis-sentinel /path/to/sentinel.conf
或者
redis-server /path/to/sentinel.conf --sentinel
测试
redis 自带客户端测试工具连接redis:
src/redis-cli -h localhost -p 6600
min-replicas-to-write配置
如果只启动的master, 没有启动slave。
localhost:6600> SET yunzhong yunzhongvalue
(error) NOREPLICAS Not enough good replicas to write.
启动slave后,设置成功:
localhost:6600> SET yunzhong yunzhongvalue
OK
localhost:6600> GET yunzhong
"yunzhongvalue"
spring boot redis 配置
spring boot pom:
<!-- redis 支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring boot data 底层的依赖已经变成:lettuce。