ifconfig
sudo vi redis.conf
bind 192.168.26.128
sudo service redis stop
sudo redis-server redis.conf
sudo cp redis.conf ./slave.conf
sudo vi slave.conf
bind 192.168.26.128
slaveof 192.168.26.128 6379
port 6378
sudo redis-server slave.conf
redis-cli -h 192.168.26.128 info Replication
redis-cli -h 192.168.26.128 -p 6379
redis-cli -h 192.168.26.128 -p 6378
set aa aa
get aa
集群是一组相互独立的、通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理。一个客户与集群相互作用时,集群像是一个独立的服务器。集群配置是用于提高可用性和可缩放性。
当请求到来首先由负载均衡服务器处理,把请求转发到另外的一台服务器上。
当前拥有两台主机172.16.179.130、172.16.179.131,这⾥的IP在使⽤时要改为实际值
port 7000
bind 172.16.179.130
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yes
port 7001
bind 172.16.179.130
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
port 7002
bind 172.16.179.130
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes
总结:三个⽂件的配置区别在port、pidfile、cluster-config-file三项
使⽤配置⽂件启动redis服务
redis-server 7000.conf
redis-server 7001.conf
redis-server 7002.conf
port 7003
bind 172.16.179.131
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
port 7004
bind 172.16.179.131
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
port 7005
bind 172.16.179.131
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes
总结:三个⽂件的配置区别在port、pidfile、cluster-config-file三项
使⽤配置⽂件启动redis服务
redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf
sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
sudo apt-get install ruby
redis-trib.rb create --replicas 1 172.16.179.130:7000 172.16.179.130:7001 172.16.179.130:7002 172.16.179.131:7003 172.16.179.131:7004 172.16.179.131:7005
执⾏上⾯这个指令在某些机器上可能会报错,主要原因是由于安装的 ruby 不是最 新版本!
天朝的防⽕墙导致⽆法下载最新版本,所以需要设置 gem 的源
解决办法如下
-- 先查看⾃⼰的 gem 源是什么地址
gem source -l -- 如果是https://rubygems.org/ 就需要更换
-- 更换指令为
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
-- 通过 gem 安装 redis 的相关依赖
sudo gem install redis
-- 然后重新执⾏指令
redis-trib.rb create --replicas 1 172.16.179.130:7000 172.16.179.130:7001 172.16.179.130:7002 172.16.179.131:7003 172.16.179.131:7004 172.16.179.131:7005
redis-cli -h 172.16.179.131 -c -p 7002
set name itheima
pip install redis-py-cluster
redis-py-cluster源码地址https://github.com/Grokzen/redis-py-cluster
创建⽂件redis_cluster.py,示例码如下
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)