【Redis】 集群配置

集群:一组通过网络连接的计算机,共同对外提供服务,像一个独立的服务器。

 

 redis配置文件

port 7001    # 端口号
bind 172.16.179.130  # ip
daemonize yes   # 后台运行
pidfile 7001.pid  # 进程文件
cluster-enabled yes  # 可以作为集群节点
cluster-config-file 7001_node.conf   # 作为集群节点的配置文件
cluster-node-timeout 15000    # 集群连接的超时时间
appendonly yes   # 数据文件追加

 

redis集群模拟

 

1 设置配置文件,启动节点

  1) 第一台服务器,启动三个节点

python@ubuntu:~/Desktop/redis_cluster/conf$ cat 7001.conf
port 7001
bind 192.168.138.134
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
python@ubuntu:~/Desktop/redis_cluster/conf$ ll
总用量 44
drwxrwxr-x 2 python python 4096 6月  20 15:52 ./
drwxrwxr-x 3 python python 4096 6月  20 15:45 ../
-rwxr-xr-x 1 python python  159 6月  20 15:48 7001.conf*
-rw-r--r-- 1 python python  112 6月  20 15:52 7001_node.conf
-rw-rw-r-- 1 python python    6 6月  20 15:52 7001.pid
-rwxr-xr-x 1 python python  159 6月  20 15:48 7002.conf*
-rw-r--r-- 1 python python  112 6月  20 15:52 7002_node.conf
-rw-rw-r-- 1 python python    6 6月  20 15:52 7002.pid
-rwxr-xr-x 1 python python  159 6月  20 15:49 7003.conf*
-rw-r--r-- 1 python python  112 6月  20 15:52 7003_node.conf
-rw-rw-r-- 1 python python    6 6月  20 15:52 7003.pid
-rw-r--r-- 1 python python    0 6月  20 15:52 appendonly.aof
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server  7001.conf 
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server  7002.conf 
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server  7003.conf 

2) 第二台服务器,启动三个节点

python@ubuntu:~/Desktop$ cat 7004.conf 
port 7004
bind 192.168.138.138
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes

python@ubuntu:~/Desktop$ redis-server 7004.conf 
python@ubuntu:~/Desktop$ redis-server 7005.conf 
python@ubuntu:~/Desktop$ redis-server 7006.conf 

2 创建集群  

    redis的安装包中包含了redis-trib.rb,用于创建集群

    将命令复制,这样可以在任何目录下调用此命令

sudo cp /usr/local/redis/src/redis-trib.rb /usr/local/bin/

    因为该服务是ruby开发,首先安装ruby环境

sudo apt-get install ruby

   创建集群

redis-trib.rb create --replicas 1 192.168.138.134:7001 192.168.138.134:7002 192.168.138.134:7003 192.168.138.138:7004 192.168.138.138:7005 192.168.138.138:7006

 根据log可以看到主从节点

 

 

 

通过测试发现配置成功

 

 

Python 与 集群的交互

from rediscluster import *
if __name__ == '__main__':
  try:
      # 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
      startup_nodes = [
          {'host': '192.168.26.128', 'port': '7000'},
          {'host': '192.168.26.130', 'port': '7003'},
          {'host': '192.168.26.128', 'port': '7001'},
      ]
      # 构建StrictRedisCluster对象
      src=StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
      # 设置键为name、值为itheima的数据
      result=src.set('name','itheima')
      print(result)
      # 获取键为name
      name = src.get('name')
      print(name)
  except Exception as e:
      print(e)

 

扩展:redis集群存储数据算法

在哪个服务器上写数据:CRC16

  • redis cluster在设计的时候,就考虑到了去中⼼化,去中间件,也就是说,集群中 的每个节点都是平等的关系,都是对等的,每个节点都保存各⾃的数据和整个集 群的状态。每个节点都和其他所有节点连接,⽽且这些连接保持活跃,这样就保 证了我们只需要连接集群中的任意⼀个节点,就可以获取到其他节点的数据
  • Redis集群没有并使⽤传统的⼀致性哈希来分配数据,⽽是采⽤另外⼀种叫做哈希 槽 (hash slot)的⽅式来分配的。redis cluster 默认分配了 16384 个slot,当我们 set⼀个key 时,会⽤CRC16算法来取模得到所属的slot,然后将这个key 分到哈 希槽区间的节点上,具体算法就是:CRC16(key) % 16384。所以我们在测试的 时候看到set 和 get 的时候,直接跳转到了7000端⼝的节点
  • Redis 集群会把数据存在⼀个 master 节点,然后在这个 master 和其对应的salve 之间进⾏数据同步。当读取数据时,也根据⼀致性哈希算法到对应的 master 节 点获取数据。只有当⼀个master 挂掉之后,才会启动⼀个对应的 salve 节点,充 当 master
  • 需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败,并且当存 活的主节点数⼩于总节点数的⼀半时,整个集群就⽆法提供服务了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章