欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Redis集群|redis cluster|redis cluster in 3.0

程序员文章站 2022-03-28 08:09:15
...

redis现在网站开发中经常用到 key-value存储系统 数据类型更加丰富 能做主从,集群 在redis3.0以前要做集群需要采用proxy的方法 在3.0(不过现在还在rc版本)自身提供了cluster的方法 这里没有用常用的一致性hash的方法,而是引入了新的概念叫hashslot(哈希

redis现在网站开发中经常用到
key-value存储系统
数据类型更加丰富
能做主从,集群
在redis3.0以前要做集群需要采用proxy的方法
在3.0(不过现在还在rc版本)自身提供了cluster的方法
这里没有用常用的一致性hash的方法,而是引入了新的概念叫hash slot(哈希槽),总共16384个 Redis PHP
第一步:
安装redis3.0,安装这里就不说了
第二步:
要做redis集群,至少需要3个Master的服务器,
我先测试的是具有子节点的,那个就需要6个服务器,3个主服务器,3个从服务器
我们来按照端口的不同来进行划分,6379、6380、6381、6382、6383、6384
创建这样6个目录,每个目录里有这样两个文件:redis-server、redis.conf
redis-server是从redis的安装目录中的src里copy过来就好
redis.conf内容如下;
#======================================#
port 6379 #不同目录,这里端口对应不同
cluster-enabled yes #开启集群功能
cluster-config-file nodes.conf #节点配置文件,这个文件是服务启动时自己配置创建的
cluster-node-timeout 5000 
appendonly yes
#======================================#
那么6个服务器都配置好后则启动他们,方法如下:./redis-server  ./redis.conf
启动好以后查看一下是否都已经成功运行了 ps aux|grep redis
第三步:
创建集群
./redis-trib.rb create --replicas 1 192.168.1.25:6379 192.168.1.25:6380 192.168.1.25:6381 192.168.1.25:6382 192.168.1.25:6383 192.168.1.25:6384
这里的--replicas 1 表示每个主节点下有一个从节点

操作结果如下
>>> Creating cluster
Connecting to node 192.168.1.25:6379: OK
Connecting to node 192.168.1.25:6380: OK
Connecting to node 192.168.1.25:6381: OK
Connecting to node 192.168.1.25:6382: OK
Connecting to node 192.168.1.25:6383: OK
Connecting to node 192.168.1.25:6384: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.25:6379
192.168.1.25:6380
192.168.1.25:6381
Adding replica 192.168.1.25:6382 to 192.168.1.25:6379
Adding replica 192.168.1.25:6383 to 192.168.1.25:6380
Adding replica 192.168.1.25:6384 to 192.168.1.25:6381
说明了将前3个服作了主节点,后3个服务作为了从节点,并且是相对应的

./redis-trib.rb是ruby程序,需要到rubygem去安装redis的包
yum install rubygems
gem install redis #如果这样不能安装就下载到本地安装吧

下面进行测试,使用redis-cli
首先登入6380这个端口的redis服务
./redis-cli -c -h 192.168.1.25 -p 6380
192.168.1.25:6380> set name andyhua.chen
退出后重新登入6379端口的redis服务
./redis-cli -c -h 192.168.1.25 -p 6379
192.168.1.25:6379> get name #如果没有集群的话,这里肯定是没有的,现在则出现如下情况
-> Redirected to slot [5798] located at 192.168.1.25:6380
"andyhua.chen"
192.168.1.25:6380> #可以看到get之后终端自动跳转到了6380端口下并取得了之前set过的name的值
Redis集群|redis cluster|redis cluster in 3.0
./redis-trib.rb create --replicas 1 192.168.1.25:6379 192.168.1.25:6380 192.168.1.25:6381 192.168.1.25:6382 192.168.1.25:6383 192.168.1.25:6384
#这里的--replicas 1 表示每个主节点下有一个从节点

#分片情况如下:
NodeA:  slots:0-5460 (5461 slots) master
NodeB:  slots:5461-10922 (5462 slots) master
NodeC:  slots:10923-16383 (5461 slots) master
Redis集群|redis cluster|redis cluster in 3.0
#如果不要从节点的方法如下
./redis-trib.rb create --replicas 0 192.168.1.25:6379 192.168.1.25:6380 192.168.1.25:6381

#分片情况如下:
NodeA:  slots:0-5460 (5461 slots) master
NodeB:  slots:5461-10922 (5462 slots) master
NodeC:  slots:10923-16383 (5461 slots) master
Redis集群|redis cluster|redis cluster in 3.0
./redis-trib.rb check 192.168.1.25:6379
#在创建集群里redis会自动执行这样的检测,当然你也可以自己运行这个check,端口(节点)也是任一其中一个
---------------------------------------------------------------------------------
Connecting to node 192.168.1.25:6379: OK
Connecting to node 192.168.1.25:6381: OK
Connecting to node 192.168.1.25:6380: OK
>>> Performing Cluster Check (using node 192.168.1.25:6379)
M: 5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6379
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
M: 47b1c1df820ff5e9057a526f69e9fe4fdd9800e9 192.168.1.25:6381
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: c97f77ebcba3f48b9da07329b0e4e2943ded88c9 192.168.1.25:6380
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#查看集群节点的相关信息
redis-cli -p 6379 -h 192.168.1.25 cluster nodes
-------------------------------------------------------------------------------------
47b1c1df820ff5e9057a526f69e9fe4fdd9800e9 192.168.1.25:6381 master - 0 1416900312414 3 connected 10923-16383
c97f77ebcba3f48b9da07329b0e4e2943ded88c9 192.168.1.25:6380 master - 0 1416900313439 2 connected 5461-10922
5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6379 myself,master - 0 0 1 connected 0-5460
--------------------------------------------------------------------------------------
#官方文档里列表参数的说明:
* Node ID
* ip:port
* flags: master, slave, myself, fail, ...
* if it is a slave, the Node ID of the master
* Time of the last pending PING still waiting for a reply.
* Time of the last PONG received.
* Configuration epoch for this node (see the Cluster specification).
* Status of the link to this node.
* Slots served...
#添加节点的测试,需求:把6382这个节点作为slave添加到6379这个master节点上
#命令如下:(两个命令都可行)
./redis-trib.rb add-node --slave 192.168.1.25:6382 192.168.1.25:6379
./redis-trib.rb add-node --slave master-id 5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6382 192.168.1.25:6379
---------------------------------------------------------------------------------------
#检测集群节点相关信息
redis-cli -p 6379 -h 192.168.1.25 cluster nodes
---------------------------------------------------------------------------------------
c31fb6f027db72876cb36e606b93817736e0ce81 192.168.1.25:6382 slave 5dfa7d9f2dbb4ab64b71f2713538125354c88462 0 1416901290565 1 connected
47b1c1df820ff5e9057a526f69e9fe4fdd9800e9 192.168.1.25:6381 master - 0 1416901293638 3 connected 10923-16383
c97f77ebcba3f48b9da07329b0e4e2943ded88c9 192.168.1.25:6380 master - 0 1416901291590 2 connected 5461-10922
5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6379 myself,master - 0 0 1 connected 0-5460
#根据官方文档列表参数说明的第4行说明
#5dfa7d9f2dbb4ab64b71f2713538125354c88462就是master的NodeID
#那么我们再看6379节点的NodeID正好也是5dfa7d9f2dbb4ab64b71f2713538125354c88462
#如果需要改变主从关系,比如将6383这个服务节点从master(6380)转变成master(6379),那么如下
redis-cli -p 6383 -h 192.168.1.25
192.168.1.25:6383> cluster replicate 5dfa7d9f2dbb4ab64b71f2713538125354c88462
#5dfa7d9f2dbb4ab64b71f2713538125354c88462是master(6379)的NodeID