安装Redis Server
sudo add-apt-repository ppa:redislabs/redis
sudo apt update
sudo apt install redis-server
redis-cli -h 127.0.0.1 -p 6379 ping
sudo systemctl restart redis-server
ss -an | grep 6379
redis-server -v
Redis server v=7.0.12 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=b56540d7933227fd
配置初始化环境
cd ~
mkdir redis
sudo mkdir -p /opt/soft/redis/data
sudo rm -rf /opt/soft/redis/data
创建配置文件:
vi vi ~/redis/redis-6379.conf
port 6379
requirepass "D3v3lop!"
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/opt/soft/redis/data/"
sudo redis-server ~/redis/redis-6379.conf &
telnet localhost 6379
输入quit,回车后退出telnet
sudo redis-cli -h 127.0.0.1 -p 6379 -a D3v3lop! ping
vi ~/redis/redis-6380.conf
port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/opt/soft/redis/data/"
sudo redis-server ~/redis/redis-6380.conf &
sudo redis-cli -p 6380 ping
vi ~/redis/redis-6381.conf
port 6381
daemonize yes
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/opt/soft/redis/data/"
sudo redis-server ~/redis/redis-6381.conf &
sudo redis-cli -p 6381 ping
确认主从关系
redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2126023
slave_repl_offset:2126023
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:9a89593acd81f0f2df663d466281c63f6e743739
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2126023
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2019111
repl_backlog_histlen:106913
配置哨兵
vi ~/redis/redis-sentinel-26379.conf
port 26379
daemonize yes
logfile "26379.log"
dir "/opt/soft/redis/data"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster D3v3lop!
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sudo redis-server ~/redis/redis-sentinel-26379.conf --sentinel
sudo redis-cli -p 26379 ping
vi ~/redis/redis-sentinel-26380.conf
port 26380
daemonize yes
logfile "26380.log"
dir "/opt/soft/redis/data"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster D3v3lop!
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sudo redis-server ~/redis/redis-sentinel-26380.conf --sentinel
sudo redis-cli -p 26380 ping
vi ~/redis/redis-sentinel-26381.conf
port 26381
daemonize yes
logfile "26381.log"
dir "/opt/soft/redis/data"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster D3v3lop!
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sudo redis-server ~/redis/redis-sentinel-26381.conf --sentinel
sudo redis-cli -p 26381 ping
查看哨兵
redis-cli -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
测试从服务器自动升级为主服务器
关掉主Redis
sudo ps aux|grep redis
root 33 0.2 0.1 162028 11772 ? Ssl 09:06 0:06 redis-server *:6379
root 59 0.3 0.1 162028 11868 ? Ssl 09:08 0:06 redis-server *:6381
root 68 0.3 0.1 83812 13884 ? Ssl 09:09 0:07 redis-server *:26379 [sentinel]
root 75 0.3 0.1 83812 13812 ? Ssl 09:09 0:07 redis-server *:26380 [sentinel]
root 82 0.3 0.1 83820 13452 ? Ssl 09:10 0:06 redis-server *:26381 [sentinel]
root 114 0.3 0.1 162028 11824 ? Ssl 09:22 0:04 redis-server *:6380
sudo kill -9 33
redis-cli -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6381,slaves=2,sentinels=3
spring boot ymal配置
redis:
sentinel:
master: mymaster
nodes: 127.0.0.1:26379
password: D3v3lop!
lettuce:
pool:
max-active: 32
max-wait: 300ms
max-idle: 16
min-idle: 8
connect-timeout: 1000
port: 26379