仅作为自己的笔记
redis
单机docker部署redis
docker run -d --name redis -p 6379:6379 redis --requirepass "password"
客户端:
docker exec -ti 89a179305875 redis-cli -h 172.17.0.2 -p 6379 -a yourpassword
string 查看所有key keys *
hashmap 查看锁 hgetall lockKey
项目中redisTemplate设置的key,redis客户端上查询不到的问题
发现redisTemplate实际存进去的key会多了几个字符
原因:程序中对key没做序列化存储
如果你用redis客户端查询你想要的key,redisTemplate下面添加这两个属性
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
docker模式 redis集群搭建
一主两从三哨兵
主机目录
[root@test3 /]# tree /data/redis
/data/redis
├── redis.conf
├── redis-data
│ ├── dump.rdb
│ └── redis.log
├── sentinel.conf
└── sentinel-data
└── sentinel.log
一主两从
配置 redis.conf
[root@test1 redis]# pwd
/data/redis
[root@test1 redis]# cat redis.conf
port 6379
daemonize no
logfile "redis.log"
dbfilename "dump.rdb"
dir "/data/"
masterauth yourpassword
[root@test2 redis]# pwd
/data/redis
[root@test2 redis]# cat redis.conf
port 6379
daemonize no
logfile "redis.log"
dbfilename "dump.rdb"
dir "/data/"
slaveof test1_ip 6379
masterauth yourpassword
[root@test3 redis]# pwd
/data/redis
[root@test3 redis]# cat redis.conf
port 6379
daemonize no
logfile "redis.log"
dbfilename "dump.rdb"
dir "/data/"
slaveof test1_ip 6379
masterauth yourpassword
tips:容器化部署时配置daemonize为no,使容器不会因为无事可做退出
docker run --restart always -p 6379:6379 -v /data/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /data/redis/redis-data:/data -d --name redis-master redis:5.0.12 redis-server /usr/local/etc/redis/redis.conf --requirepass yourpassword
docker run --restart always -p 6379:6379 -v /data/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /data/redis/redis-data:/data -d --name redis-slave1 redis:5.0.12 redis-server /usr/local/etc/redis/redis.conf --requirepass yourpassword
docker run --restart always -p 6379:6379 -v /data/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /data/redis/redis-data:/data -d --name redis-slave2 redis:5.0.12 redis-server /usr/local/etc/redis/redis.conf --requirepass yourpassword
(1)客户端进入
docker exec -ti 28cf31620dae redis-cli -h 172.17.0.4 -p 6379 -a yourpassword
(2)进入容器后redis-cli 输入密码 auth yourpassword
哨兵集群
配置sentinel.conf
[root@test1 redis]# cat sentinel.conf
port 26379
sentinel monitor mymaster test1_ip 6379 2
sentinel auth-pass mymaster yourpassword
sentinel parallel-syncs mymaster 1
daemonize no
requirepass yourpassword
logfile "sentinel.log"
[root@test2 redis]# cat sentinel.conf
port 26379
sentinel monitor mymaster test1_ip 6379 2
sentinel auth-pass mymaster yourpassword
sentinel parallel-syncs mymaster 1
daemonize no
requirepass yourpassword
logfile "sentinel.log"
[root@test3 redis]# cat sentinel.conf
port 26379
sentinel monitor mymaster test1_ip 6379 2
sentinel auth-pass mymaster yourpassword
sentinel parallel-syncs mymaster 1
daemonize no
requirepass yourpassword
logfile "sentinel.log"
三份配置文件的sentinel monitor都写当前redis master的ip
sentinel使用host模式
docker run --restart always --net host -v /data/redis/sentinel.conf:/usr/local/etc/redis/sentinel.conf -v /data/redis/sentinel-data:/data -d --name redis-sentinel1 redis:5.0.12 redis-sentinel /usr/local/etc/redis/sentinel.conf --requirepass yourpassword
docker run --restart always --net host -v /data/redis/sentinel.conf:/usr/local/etc/redis/sentinel.conf -v /data/redis/sentinel-data:/data -d --name redis-sentinel2 redis:5.0.12 redis-sentinel /usr/local/etc/redis/sentinel.conf --requirepass yourpassword
docker run --restart always --net host -v /data/redis/sentinel.conf:/usr/local/etc/redis/sentinel.conf -v /data/redis/sentinel-data:/data -d --name redis-sentinel3 redis:5.0.12 redis-sentinel /usr/local/etc/redis/sentinel.conf --requirepass yourpassword
查看哨兵
$ docker exec -it redis-sentinel bash$ redis-cli -p 26379-> info
问题:
redis1为master启动哨兵,过一段时间redis2变成master.哨兵机器故障重启,但配置文件中还是redis1的ip.
答:版本问题,哨兵集群完成后,会识别出真正的master
查看所有配置 config get *
禁用持久化:
用过Redis的朋友都知道,这玩意有个比较强大的功能叫做持久化,就是在结束服务的时候把缓存中的内容保存到磁盘上,再启动服务的时候它自动从保存的磁盘文件中恢复服务停止之前的缓存内容,就好像服务从来没停止过一样。这个功能在生产服务器上确实挺方便的,重启也不会丢失缓存内容,但在开发环境中就不方便,每天开机启动调试环境的时候,它都自动加载前一天的缓存内容,有时候数据都改了很多,它还是旧数据。
于是想禁用这个持久化的功能,查了资料知道修改redis.conf,找到save配置,改为save "" 即可。改了之后也没多想,后来发现还是有旧数据的缓存,感觉有点奇怪,运行flushall命令就没有旧数据了,但隔天重启电脑,又显示很多旧数据,真灵异了!后来反复排查才发现redis.conf中还有个dir配置,就是持久化的磁盘文件存放的目录,打开相应的目录,删除目录中的*.rdb文件,再重启redis服务,果然再也没有旧数据了!
坑:
- (1)redis集群版本采用5.0版本,不要用6.0版本,6.0版本哨兵集群的配置文件在运行中没有动态变化
- (2)redisson客户端使用3.15.0及以上版本才会有setSentinelPassword选项,当redis哨兵集群有密码时,需要这个选项才能连接上
- (3)redis哨兵集群在docker中使用非host模式启动时,sentinel.conf中显示的是容器ip,客户端连接时报 SENTINEL SENTINELS command returns less than 2 nodes! At least two sentinels should be defined in Redis configuration. Set checkSentinelsList = false to avoid this check.表示只识别出一个sentinel.当配置了checkSentinelsList(false),当此唯一的sentinel挂掉时,客户端连接就会中断
- (4)redis哨兵集群在docker中使用host模式启动时,客户端没有报错,可正常使用.当有3台哨兵时可以允许2台哨兵挂掉,当哨兵重新上线,客户端会感知到并把哨兵加回来
- (5)只有一个哨兵时,redis主挂了无法进行故障转移,恢复至2台哨兵时,可继续故障转移