redis的四种模式部署应用

这里写目录标题

redis应用

redis单机部署

powershell 复制代码
关闭防火墙

[root@zyq ~]#: yum -y install wget make gcc gcc-c++
......
[root@zyq ~]#: wget https://download.redis.io/redis-stable.tar.gz
--2024-01-01 19:41:14--  https://download.redis.io/redis-stable.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3477620 (3.3M) [application/octet-stream]
Saving to: 'redis-stable.tar.gz'

redis-stable.tar.gz         100%[===========================================>]   3.32M  3.61MB/s    in 0.9s    

2024-01-01 19:41:15 (3.61 MB/s) - 'redis-stable.tar.gz' saved [3477620/3477620]
[root@zyq ~]#: ls
anaconda-ks.cfg  redis-stable.tar.gz
[root@zyq ~]#: tar xf redis-stable.tar.gz 
[root@zyq ~]#: ls
anaconda-ks.cfg  redis-stable  redis-stable.tar.gz
[root@zyq ~]#: cd redis-stable/
[root@zyq redis-stable]#: ls
00-RELEASENOTES     CONTRIBUTING.md  INSTALL    README.md   runtest-cluster    SECURITY.md    tests
BUGS                COPYING          Makefile   redis.conf  runtest-moduleapi  sentinel.conf  TLS.md
CODE_OF_CONDUCT.md  deps             MANIFESTO  runtest     runtest-sentinel   src            utils


[root@zyq redis-stable]#: make
[root@zyq redis-stable]#: make install
cd src && make install
make[1]: Entering directory '/root/redis-stable/src'
    CC Makefile.dep

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: Leaving directory '/root/redis-stable/src'


[root@zyq redis-stable]#: mkdir /etc/redis
[root@zyq redis-stable]#: cp redis.conf /etc/redis/
[root@zyq redis-stable]#: echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
[root@zyq redis-stable]#: cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.overcommit_memory = 1
[root@zyq redis-stable]#: echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@zyq redis-stable]#: cd


[root@zyq ~]#: redis-server /etc/redis/redis.conf 
7862:C 01 Jan 2024 19:57:38.943 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7862:C 01 Jan 2024 19:57:38.943 * Redis version=7.2.3, bits=64, commit=00000000, modified=0, pid=7862, just started
7862:C 01 Jan 2024 19:57:38.943 * Configuration loaded
7862:M 01 Jan 2024 19:57:38.944 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7862:M 01 Jan 2024 19:57:38.944 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7862
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

7862:M 01 Jan 2024 19:57:38.945 * Server initialized
7862:M 01 Jan 2024 19:57:38.945 * Ready to accept connections tcp


[root@zyq ~]#: ss -antl
State       Recv-Q       Send-Q             Local Address:Port             Peer Address:Port      Process      
LISTEN      0            128                      0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0            511                    127.0.0.1:6379                  0.0.0.0:*                      
LISTEN      0            128                         [::]:22                       [::]:*                      
LISTEN      0            511                        [::1]:6379                     [::]:*                      
[root@zyq ~]#: redis-cli 
127.0.0.1:6379> 

使redis在后台运行需要修改配置文件/etc/redis/redis.conf,将daemonize值设为yes

默认情况下,redis连接是不需要经过认证的,而redis承载了企业大部分的请求流量,如果不经过认证很有可能在互联网上遭受攻击,所以需要在配置文件中为redis设置密码认证,且密码最好随机生成,保证没有规律可言。

powershell 复制代码
[root@zyq ~]#: vim /etc/redis/redis.conf 
更改一下配置
bind 192.168.227.153 将127.0.0.1改为本机ip
daemonize yes  no改为yes
# requirepass foobared  取消这行注释
requirepass hhh123   或者添加自定义密码

再重新启动
[root@zyq ~]#: redis-server /etc/redis/redis.conf 
[root@zyq ~]#: ss -antl
State       Recv-Q      Send-Q              Local Address:Port             Peer Address:Port      Process      
LISTEN      0           128                       0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0           511               192.168.227.153:6379                  0.0.0.0:*                      
LISTEN      0           128                          [::]:22                       [::]:*                      
[root@zyq ~]#: redis-cli -h 192.168.227.153
192.168.227.153:6379> key *
(error) ERR unknown command 'key', with args beginning with: '*' 
192.168.227.153:6379> auth hhh123
OK

redis主从

powershell 复制代码
[root@zyq ~]#: cd /etc/redis/
[root@zyq redis]#: ls
redis.conf
[root@zyq redis]#: mv redis.conf 6379.conf
[root@zyq redis]#: cp 6379.conf 6380.conf

更改端口号

port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis6380.log"
replicaof 192.168.227.153 6379  在从上认主(由于使用一台虚拟机来配置,所以要修改端口号)
masterauth hhh123  设置密码


[root@zyq redis]#: redis-server /etc/redis/6379.conf 
[root@zyq redis]#: redis-server /etc/redis/6380.conf 
[root@zyq redis]#: ss -antl
State       Recv-Q      Send-Q              Local Address:Port             Peer Address:Port      Process      
LISTEN      0           128                       0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0           511               192.168.227.153:6379                  0.0.0.0:*                      
LISTEN      0           511               192.168.227.153:6380                  0.0.0.0:*                      
LISTEN      0           128                          [::]:22                       [::]:*                      
[root@zyq redis]#: ps -ef | grep redis
root        7967       1  0 20:14 ?        00:00:01 redis-server 192.168.227.153:6379
root        7978    1467  0 20:16 pts/0    00:00:00 vim /etc/redis/redis.conf
root        8012       1  0 20:30 ?        00:00:00 redis-server 192.168.227.153:6380
root        8030    7881  0 20:31 pts/1    00:00:00 grep --color=auto redis


测试登录从库
[root@zyq ~]#: redis-cli -h 192.168.227.153 -p 6380
192.168.227.153:6380> auth hhh123
OK
192.168.227.153:6380> keys *
(empty array)
192.168.227.153:6380> 

登录主库
[root@zyq redis]#: redis-cli -h 192.168.227.153 -p 6379
192.168.227.153:6379> auth hhh123
OK
192.168.227.153:6379> keys *
(empty array)
192.168.227.153:6379> 
在主库上添加
192.168.227.153:6379> set age 20
OK
192.168.227.153:6379> keys *
1) "age"

在从库上给查看
192.168.227.153:6380> keys *
1) "age"

查看从库信息
192.168.227.153:6380> info replication
# Replication
role:slave
master_host:192.168.227.153
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:571
slave_repl_offset:571
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23aecbcf1431e181f485ef5cea33a424fc0c39d2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:571
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:557

查看主库信息
192.168.227.153:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.227.153,port=6380,state=online,offset=655,lag=1
master_failover_state:no-failover
master_replid:23aecbcf1431e181f485ef5cea33a424fc0c39d2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:655
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:655

redis哨兵

powershell 复制代码
删除上面配置的只从环境
[root@zyq redis]#: ls
6379.conf  6380.conf  dump.rdb
[root@zyq redis]#: rm -rf dump.rdb 
[root@zyq redis]#: rm -rf 6380.conf
[root@zyq redis]#: cd
[root@zyq ~]#: ls
anaconda-ks.cfg  dump.rdb  redis-stable  redis-stable.tar.gz
[root@zyq ~]#: rm -rf dump.rdb 



配置三个主
[root@zyq redis]#: ls
6379.conf
[root@zyq redis]#: cp 6379.conf 6381.conf
[root@zyq redis]#: cp 6379.conf 6380.conf
[root@zyq redis]#: ls
6379.conf  6380.conf  6381.conf
[root@zyq redis]#: vim 6380.conf  638
2 files to edit
更改以下参数
port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis6380.log"

[root@zyq redis]#: vim 6381.conf
更改以下参数
port 6381
pidfile /var/run/redis_6381.pid
logfile "/var/log/redis6381.log"

配置三个从
[root@zyq redis]#: cp 6379.conf 6389.conf
[root@zyq redis]#: cp 6380.conf 6390.conf
[root@zyq redis]#: cp 6381.conf 6391.conf

[root@zyq redis]#: vim 6389.conf
pidfile /var/run/redis_6389.pid
logfile "/var/log/redis6389.log"
replicaof 192.168.227.153 6379  在从上认主(由于使用一台虚拟机来配置,所以要修改端口号)
masterauth hhh123  设置密码

[root@zyq redis]#: vim 6390.conf
pidfile /var/run/redis_6390.pid
logfile "/var/log/redis6390.log"
replicaof 192.168.227.153 6380 

[root@zyq redis]#: vim 6391.conf
pidfile /var/run/redis_6390.pid
logfile "/var/log/redis6390.log"
replicaof 192.168.227.153 6381


配置哨兵
[root@zyq ~]#: cd redis-stable/
[root@zyq redis-stable]#: ls
00-RELEASENOTES     CONTRIBUTING.md  INSTALL    README.md   runtest-cluster    SECURITY.md    tests
BUGS                COPYING          Makefile   redis.conf  runtest-moduleapi  sentinel.conf  TLS.md
CODE_OF_CONDUCT.md  deps             MANIFESTO  runtest     runtest-sentinel   src            utils
[root@zyq redis-stable]#: cp sentinel.conf /etc/redis/
[root@zyq redis-stable]#: cd /etc/redis/
[root@zyq redis]#: ls
6379.conf  6380.conf  6381.conf  6389.conf  6390.conf  6391.conf  sentinel.conf
[root@zyq redis]#: vi /etc/redis/sentinel.conf
添加以下配置
sentinel monitor mymaster 192.168.227.153 6379 2
sentinel monitor mymaster 192.168.227.153 6380 2
sentinel monitor mymaster 192.168.227.153 6381 2

sentinel monitor myslave 192.168.227.153 6389 2
sentinel monitor myslave 192.168.227.153 6390 2
sentinel monitor myslave 192.168.227.153 6391 2

sentinel auth-pass mymaster1 hhh123
sentinel auth-pass mymaster2 hhh123
sentinel auth-pass mymaster3 hhh123

sentinel auth-pass myslave1 hhh123
sentinel auth-pass myslave2 hhh123
sentinel auth-pass myslave3 hhh123

配置3个哨兵配置文件
更改名字好区分
[root@zyq redis]#: ls
6379.conf  6380.conf  6381.conf  6389.conf  6390.conf  6391.conf  sentinel.conf
[root@zyq redis]#: mv sentinel.conf 26380.conf
修改添加
port 26380
.....
pidfile /var/run/redis-sentinel2.pid
......
sentinel down-after-milliseconds mymaster1 30000
sentinel down-after-milliseconds mymaster2 30000
sentinel down-after-milliseconds mymaster3 30000

sentinel down-after-milliseconds myslave1 30000
sentinel down-after-milliseconds myslave2 30000
sentinel down-after-milliseconds myslave3 30000
......
sentinel parallel-syncs mymaster1 1
sentinel parallel-syncs mymaster2 1
sentinel parallel-syncs mymaster3 1

sentinel parallel-syncs myslave1 1
sentinel parallel-syncs myslave2 1
sentinel parallel-syncs myslave3 1
......
sentinel failover-timeout mymaster1 180000
sentinel failover-timeout mymaster2 180000
sentinel failover-timeout mymaster3 180000

sentinel failover-timeout myslave1 180000
sentinel failover-timeout myslave2 180000
sentinel failover-timeout myslave3 180000
......
SENTINEL master-reboot-down-after-period mymaster1 0
SENTINEL master-reboot-down-after-period mymaster2 0
SENTINEL master-reboot-down-after-period mymaster3 0

SENTINEL master-reboot-down-after-period myslave1 0
SENTINEL master-reboot-down-after-period myslave2 0
SENTINEL master-reboot-down-after-period myslave3 0

[root@zyq redis]#: cp 26380.conf  26379.conf
[root@zyq redis]#: vim 26379.conf
port 26379
.....
pidfile /var/run/redis-sentinel1.pid
.....

[root@zyq redis]#: cp 26380.conf  26381.conf
[root@zyq redis]#: vim 26381.conf
port 26381
.....
pidfile /var/run/redis-sentinel3.pid
......


启动并查看哨兵
[root@zyq redis]#: redis-sentinel  26379.conf
[root@zyq redis]#: redis-sentinel  26380.conf 
[root@zyq redis]#: redis-sentinel  26381.conf 
[root@zyq redis]#: ps -ef|grep redis
root        8115    1467  0 21:23 pts/0    00:00:00 /usr/bin/vim /etc/redis/sentinel.conf
root        8141       1  0 21:43 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root        8150       1  0 21:43 ?        00:00:00 redis-sentinel *:26380 [sentinel]
root        8156       1  0 21:43 ?        00:00:00 redis-sentinel *:26381 [sentinel]
root        8162    7881  0 21:43 pts/1    00:00:00 grep --color=auto redis

启动主从
[root@zyq redis]#: redis-server 6379.conf
[root@zyq redis]#: redis-server 6380.conf
[root@zyq redis]#: redis-server 6381.conf
[root@zyq redis]#: redis-server 6389.conf
[root@zyq redis]#: redis-server 6390.conf
[root@zyq redis]#: redis-server 6391.conf
[root@zyq redis]#: ps -ef|grep redis
root        8115    1467  0 21:23 pts/0    00:00:00 /usr/bin/vim /etc/redis/sentinel.conf
root        8141       1  0 21:43 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root        8150       1  0 21:43 ?        00:00:00 redis-sentinel *:26380 [sentinel]
root        8156       1  0 21:43 ?        00:00:00 redis-sentinel *:26381 [sentinel]
root        8202       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6379
root        8211       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6380
root        8217       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6381
root        8223       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6389
root        8229       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6390
root        8237       1  0 21:47 ?        00:00:00 redis-server 192.168.227.153:6391
root        8246    7881  0 21:47 pts/1    00:00:00 grep --color=auto redis

这时是3个哨兵监控3个主和从

在sentinel集群环境下需要多个sentinel互相沟通来确认某个master是否真的死了,quorum这个参数是进行客观下线的一个依据,意思是至少有quorum个sentinel认为这个master有故障,才会对这个master进行下线以及故障转移。因为有的时候,某个sentinel节点可能因为自身网络原因,导致无法连接master,而此时master并没有出现故障,所以,这就需要多个sentinel都一致认为该master有问题,才可以进行下一步操作,这就保证了公平性和高可用。

Cluster模式

主从模式或哨兵模式每个节点存储的数据都是全量的数据,数据量过大时,就需要对存储的数据进行分片后存储到多个redis实例上。此时就要用到Redis Sharding技术。

redis在3.0上加入了 Cluster 集群模式,实现了 Redis 的分布式存储,也就是说每台 Redis 节点上存储不同的数据。cluster模式为了解决单机Redis容量有限的问题,将数据按一定的规则分配到多台机器,内存/QPS不受限于单机,可受益于分布式集群高扩展性。

Redis Cluster是一种服务器Sharding技术(分片和路由都是在服务端实现),采用多主多从,每一个分区都是由一个Redis主机和多个从机组成,片区和片区之间是相互平行的。Redis Cluster集群采用了P2P的模式,完全去中心化

powershell 复制代码
[root@zyq ~]#: ss -antl
State       Recv-Q      Send-Q             Local Address:Port             Peer Address:Port      Process      
LISTEN      0           128                      0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0           128                         [::]:22                       [::]:*  
[root@zyq ~]#: cd /etc/redis/
[root@zyq redis]#: ls
6379.conf  6380.conf  6381.conf  6389.conf  6390.conf  6391.conf 

[root@zyq redis]#: vim /usr/bin/redis-daemon
[root@zyq redis]#: cat /usr/bin/redis-daemon 
#!/bin/bash

/usr/local/bin/redis-server /etc/redis/6379.conf
/usr/local/bin/redis-server /etc/redis/6380.conf
/usr/local/bin/redis-server /etc/redis/6381.conf
/usr/local/bin/redis-server /etc/redis/6389.conf
/usr/local/bin/redis-server /etc/redis/6390.conf
/usr/local/bin/redis-server /etc/redis/6391.conf
[root@zyq redis]#: chmod +x /usr/bin/redis-daemon
[root@zyq redis]#: ll /usr/bin/redis-daemon 
-rwxr-xr-x 1 root root 307 Jan  2 15:35 /usr/bin/redis-daemon

[root@zyq redis]#: ss -antl
State       Recv-Q      Send-Q             Local Address:Port             Peer Address:Port      Process      
LISTEN      0           511              192.168.227.153:6379                  0.0.0.0:*                      
LISTEN      0           511              192.168.227.153:6380                  0.0.0.0:*                      
LISTEN      0           511              192.168.227.153:6381                  0.0.0.0:*                      
LISTEN      0           511              192.168.227.153:6389                  0.0.0.0:*                      
LISTEN      0           511              192.168.227.153:6390                  0.0.0.0:*                      
LISTEN      0           128                      0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0           511              192.168.227.153:6391                  0.0.0.0:*                      
LISTEN      0           128                         [::]:22                       [::]:*                      
设置开机自启
[root@zyq redis]#: cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/redis.service
[root@zyq redis]#: vim /usr/lib/systemd/system/redis.service
[root@zyq redis]#: cat  /usr/lib/systemd/system/redis.service
[Unit]
Description=redis replication server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/bin/redis-daemon
ExecStop=/usr/bin/pkill redis 
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@zyq redis]#: systemctl daemon-reload 
[root@zyq redis]#: systemctl enable --now redis.service 
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.


[root@zyq ~]#: cd redis-stable
[root@zyq redis-stable]#: ls
00-RELEASENOTES     CONTRIBUTING.md  INSTALL    README.md   runtest-cluster    SECURITY.md    tests
BUGS                COPYING          Makefile   redis.conf  runtest-moduleapi  sentinel.conf  TLS.md
CODE_OF_CONDUCT.md  deps             MANIFESTO  runtest     runtest-sentinel   src            utils
[root@zyq redis-stable]#: cp redis.conf /etc/redis/
[root@zyq redis]#: mv redis.conf 6379.修改配置文件
[root@zyq redis]#: vim 6379.conf 
取消注释
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes  no改为yes
daemonize yes  no改为yes
logfile "/var/log/redis6379.log"

[root@zyq redis]#: cp 6379.conf 6380.conf
[root@zyq redis]#: cp 6379.conf 6381.conf
[root@zyq redis]#: cp 6379.conf 6389.conf
[root@zyq redis]#: cp 6379.conf 6390.conf
[root@zyq redis]#: cp 6379.conf 6391.conf


[root@zyq redis]#: sed -i 's/6379/6380/g' 6380.conf 
[root@zyq redis]#: sed -i 's/6379/6381/g' 6381.conf 
[root@zyq redis]#: sed -i 's/6379/6389/g' 6389.conf 
[root@zyq redis]#: sed -i 's/6379/6390/g' 6390.conf 
[root@zyq redis]#: sed -i 's/6379/6391/g' 6391.conf

重启redis服务
[root@zyq redis]#: systemctl daemon-reload 
[root@zyq redis]#: systemctl restart redis.service 

启动cluster模式
[root@zyq redis-stable]#: redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1:6391 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6390 to 127.0.0.1:6379
Adding replica 127.0.0.1:6391 to 127.0.0.1:6380
Adding replica 127.0.0.1:6389 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 80daef8a00d8d95d123d807f8ea21ccb9f188cb7 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
M: 3ec104631e7087b99b25311d40935deb6a9c163a 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
M: 96a64c539a0b801746cb1f8f6426c085786e5485 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
S: a4f77673a5f853a393272af74f3408dccca626e2 127.0.0.1:6389
   replicates 96a64c539a0b801746cb1f8f6426c085786e5485
S: de7f8a76c211788fbe5f4f1c44e3d4fb83c742dd 127.0.0.1:6390
   replicates 80daef8a00d8d95d123d807f8ea21ccb9f188cb7
S: c1cef4cdee9cd641825793b39ede45d33faa3f22 127.0.0.1:6391
   replicates 3ec104631e7087b99b25311d40935deb6a9c163a
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 80daef8a00d8d95d123d807f8ea21ccb9f188cb7 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: de7f8a76c211788fbe5f4f1c44e3d4fb83c742dd 127.0.0.1:6390
   slots: (0 slots) slave
   replicates 80daef8a00d8d95d123d807f8ea21ccb9f188cb7
S: c1cef4cdee9cd641825793b39ede45d33faa3f22 127.0.0.1:6391
   slots: (0 slots) slave
   replicates 3ec104631e7087b99b25311d40935deb6a9c163a
M: 96a64c539a0b801746cb1f8f6426c085786e5485 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 3ec104631e7087b99b25311d40935deb6a9c163a 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a4f77673a5f853a393272af74f3408dccca626e2 127.0.0.1:6389
   slots: (0 slots) slave
   replicates 96a64c539a0b801746cb1f8f6426c085786e5485
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试主从效果
[root@zyq ~]#: redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set age 20
OK

[root@zyq ~]#: redis-cli -h 127.0.0.1 -p 6390
127.0.0.1:6390> keys *
1) "age"


[root@zyq ~]#: yum -y install ruby
[root@zyq ~]#: vim example.rb 
[root@zyq ~]#: cat example.rb 
require './cluster'
  
     if ARGV.length != 2
         startup_nodes = [
             {:host => "127.0.0.1", :port => 6379},
             {:host => "127.0.0.1", :port => 6380}
         ]
     else
         startup_nodes = [
            {:host => ARGV[0], :port => ARGV[1].to_i}
        ]
    end
  
    rc = RedisCluster.new(startup_nodes,32,:timeout => 0.1)
  
    last = false
  
    while not last
        begin
            last = rc.get("__last__")
            last = 0 if !last
        rescue => e
            puts "error #{e.to_s}"
            sleep 1
        end
    end
  
    ((last.to_i+1)..1000000000).each{|x|
        begin
            rc.set("foo#{x}",x)
            puts rc.get("foo#{x}")
            rc.set("__last__",x)
        rescue => e
            puts "error #{e.to_s}"
        end
        sleep 0.1
    }


测试集群模式效果(要加上'-c')可修改不同节点的事务且不会重复
[root@zyq ~]#: redis-cli -h 127.0.0.1 -p 6379 -c
127.0.0.1:6379> keys *
1) "age"

[root@zyq ~]#: redis-cli -h 127.0.0.1 -p 6380 -c
127.0.0.1:6380> get age
-> Redirected to slot [741] located at 127.0.0.1:6379
"20"
相关推荐
Elastic 中国社区官方博客2 小时前
在 Elasticsearch 中使用 Mistral Chat completions 进行上下文工程
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
编程爱好者熊浪4 小时前
两次连接池泄露的BUG
java·数据库
cr7xin4 小时前
缓存三大问题及解决方案
redis·后端·缓存
TDengine (老段)5 小时前
TDengine 字符串函数 CHAR 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
qq7422349845 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
姚远Oracle ACE6 小时前
Oracle 如何计算 AWR 报告中的 Sessions 数量
数据库·oracle
Dxy12393102166 小时前
MySQL的SUBSTRING函数详解与应用
数据库·mysql
码力引擎6 小时前
【零基础学MySQL】第十二章:DCL详解
数据库·mysql·1024程序员节
杨云龙UP7 小时前
【MySQL迁移】MySQL数据库迁移实战(利用mysqldump从Windows 5.7迁至Linux 8.0)
linux·运维·数据库·mysql·mssql
l1t7 小时前
利用DeepSeek辅助修改luadbi-duckdb读取DuckDB decimal数据类型
c语言·数据库·单元测试·lua·duckdb