Redis服务器集群搭建

为什么要有集群?

  • 一主可以多从,如果同时的访问量过大(1000w),主服务肯定就会挂掉,数据服务就挂掉了或者发生自然灾难
  • 大公司都会有很多的服务器(华东地区、华南地区、华中地区、华北地区、西北地区、西南地区、东北地区、台港澳地区机房)

集群的概念?

       集群是一组相互独立的、通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理。一个客户与集群相互作用时,集群像是一个独立的服务器。集群配置是用于提高可用性和可缩放性。

redis集群

  • 分类
    • 软件层面
    • 硬件层面
  • 软件层面:只有一台电脑,在这一台电脑上启动了多个redis服务。缺点:一旦硬件主机坏掉整个服务器都不能工作。

  • 硬件层面:存在多台实体的电脑,每台电脑上都启动了一个redis或者多个redis服务。便于扩展!


配置机器1

1.在Django项目根目录下创建conf文件夹,并在该文件夹下创建7000.conf、7001.conf、7002.conf 配置文件

# 7000.conf
port 7000
bind 192.168.1.102
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yes
# 7001.conf
port 7001
bind 192.168.1.102
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
# 7002.conf
port 7002
bind 192.168.1.102
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes

2.进入项目名/conf文件夹,启动服务器集群 

cd conf
redis-server 7000.conf
redis-server 7001.conf
redis-server 7002.conf

配置机器2

1.查看机器2 IP地址

ifconfig

2.在桌面创建conf文件夹,并创建 7003.conf、7004.conf、7005.conf配置文件

# 7003.conf
port 7003
bind 192.168.1.104
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
# 7004.conf
port 7004
bind 192.168.1.104
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
port 7005
bind 192.168.1.104
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes

3.启动redis服务器

cd conf
redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf

创建集群

  • redis的安装包中包含了redis-trib.rb,⽤于创建集群
  • 接下来的操作在linux 192.168.1.104 机器上进⾏
# 安装ruby环境,因为redis-trib.rb是⽤ruby开发的
sudo apt-get install ruby

# 先查看⾃⼰的 gem 源是什么地址
gem source -l -- 如果是https://rubygems.org/ 就需要更换

# 更换指令为
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/

# 通过 gem 安装 redis 的相关依赖
sudo gem install redis
  • 将命令复制,这样可以在任何⽬录下调⽤此命令

sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/

cd /usr/local/bin/
  • 运⾏如下命令创建集群
redis-trib.rb create --replicas 1 192.168.1.102:7000 192.168.1.102:7001 192.168.1.102:7002 192.168.1.104:7003 192.168.1.104:7004 192.168.1.104:7005

  • 检查链接状态 
redis-trib.rb check 192.168.1.102:7000


测试

  • 在机器2运行,并设置键值
redis-cli -h 192.168.1.104 -c -p 7005

  • 在机器1运行,并查询键值
redis-cli -h 192.168.1.102 -c -p 7002


pyhont交互

  • 在Pycharm安装 redis-py-cluster
  • 创建⽂件redis_cluster.py,示例码如下
from rediscluster import StrictRedisCluster

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值