root@localhost redis]# mkdir -p 6379/conf 6379/data 6379/log
[root@localhost redis]# mkdir -p 6380/conf 6380/data 6380/log
[root@localhost redis]# mkdir -p 6381/conf 6381/data 6381/log
redis
|6379
| conf
-- redis.conf
| data
| log
-- redis.log (777)
|6380
| conf
-- redis.conf
| data
| log
-- redis.log (777)
|6381
| conf
-- redis.conf
| data
| log
-- redis.log (777)
修改redis.conf文件
改为0.0.0.0【所有ip均可访问】
改为no【不开启保护模式】
改为debug和/var/log/redis.log【设置日志打印级别和容器内的日志地址】
改为yes【启动时有图标】
设置自己的ip地址和端口
docker run -it \
--name redis_6379 \
--privileged \
-p 6379:6379 \
--network wn_docker_net \
--ip 172.18.12.10 \
--sysctl net.core.somaxconn=1024 \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
-v /usr/local/software/redis/6379/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6379/data/:/data \
-v /usr/local/software/redis/6379/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
配置文件
从:6380
docker run -it \
--name redis_6380 \
--privileged \
-p 6380:6379 \
--network wn_docker_net \
--ip 172.18.12.11 \
--sysctl net.core.somaxconn=1024 \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
-v /usr/local/software/redis/6380/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6380/data/:/data \
-v /usr/local/software/redis/6380/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
从:6381【配置文件同6380:改下端口号】
docker run -it \
--name redis_6381 \
--privileged \
-p 6381:6379 \
--network wn_docker_net \
--ip 172.18.12.12 \
--sysctl net.core.somaxconn=1024 \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
-v /usr/local/software/redis/6381/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6381/data/:/data \
-v /usr/local/software/redis/6381/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
进入主服务器查看
docker exec -it redis_6379 bash
redis-cli
info replication
修改redis.conf文件
26379:
docker run -it \
--name sentinel_26379 \
--privileged \
--network wn_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.70 \
-p 26379:26379 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/redis_sentinel/26379/conf:/usr/local/etc/redis/conf/ \
-v /usr/local/software/redis_sentinel/26379/log/sentinel.log:/var/log/sentinel.log \
-d redis \
redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
26380:
docker run -it \
--name sentinel_26380 \
--privileged \
--network wn_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.71 \
-p 26380:26380 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/redis_sentinel/26380/conf:/user/local/etc/redis/conf/ \
-v /usr/local/software/redis_sentinel/26380/log/sentinel.log:/var/log/sentinel.log \
-d redis \
redis-sentinel /user/local/etc/redis/conf/sentinel.conf
26381:
docker run -it \
--name sentinel_26381 \
--privileged \
--network wn_docker_net \
--sysctl net.core.somaxconn=1024 \
--ip 172.18.12.72 \
-p 26381:26379 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/redis_sentinel/26381/conf:/usr/local/etc/redis/conf/ \
-v /usr/local/software/redis_sentinel/26381/log/sentinel.log:/var/log/sentinel.log \
-d redis \
redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
[root@hao ~]# docker exec -it sentinel_26379 bash
root@be2d68a1ff59:/data# redis-cli -p 26379 -c
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.133.100:6379,slaves=2,sentinels=3
spring:
redis:
sentinel:
master: mymaster
nodes: 192.168.133.100:26379,192.168.133.100:26380,192.168.133.100:26381
application:
name: smart-replication
需要导入依赖
!!!该依赖会和knife4j-openapi2-spring-boot-starter的依赖造成冲突,所以需排除spring-boot-starter-actuator
!!!该依赖不能使用最新版本,最新版本不兼容springboot3.x以下
<redisson.version>3.17.7redisson.version>
<dependency>
<groupId>org.redissongroupId>
<artifactId>redisson-spring-boot-starterartifactId>
<version>${redisson.version}version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-actuatorartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSentinelServers()
.setMasterName("mymaster")
.addSentinelAddress("redis://192.168.201.81:26379")
.addSentinelAddress("redis://192.168.201.81:26380")
.addSentinelAddress("redis://192.168.201.81:26381");
return Redisson.create(config);
}
}