Home  >  Article  >  Backend Development  >  Implementation of redis cluster and calling using php

Implementation of redis cluster and calling using php

不言
不言Original
2018-04-26 14:43:223593browse

This article introduces the implementation of redis cluster and the use of php to call it. I would like to share it with you. Friends in need can refer to it

1. Building a redis cluster
1. Concept Explanation
Redis versions after 3.0 support redis-cluster clusters. Redis-Cluster adopts a centerless structure. Each node saves data and the entire cluster status, and each node is connected to all other nodes. The redis-cluster architecture diagram is as follows:
Implementation of redis cluster and calling using php
Its structural characteristics:

 1、所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
 2、节点的fail是通过集群中超过半数的节点检测失效时才生效。
 3、客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
 4、redis-cluster把所有的物理节点映射到[0-16383]slot上(不一定是平均分配),cluster 负责维护nodeslotvalue。

 5、Redis集群预分好16384个桶,当需要在 Redis 集群中放置一个 key-value 时,根据 CRC16(key) mod 16384的值,决定将一个key放到哪个桶中。
  现在我们是三个主节点分别是:A, B, C 三个节点,它们可以是一台机器上的三个端口,也可以是三台不同的服务器。那么,采用哈希槽 (hash slot)的方式来分配16384个slot 的话,它们三个节点分别承担的slot 区间是:


      节点A覆盖0-5460;
      节点B覆盖5461-10922;
      节点C覆盖10923-16383.

can’t understand,,,,

     获取数据:

      如果存入一个值,按照redis cluster哈希槽的算法: CRC16('key')384 = 6782。 那么就会把这个key 的存储分配到 B 上了。同样,当我连接(A,B,C)任何一个节点想获取'key'这个key时,也会这样的算法,然后内部跳转到B节点上获取数据

2. Redis Cluster master-slave mode

redis cluster 为了保证数据的高可用性,加入了主从模式,一个主节点对应一个或多个从节点,主节点提供数据存取,从节点则是从主节点拉取数据备份,当这个主节点挂掉后,就会有这个从节点选取一个来充当主节点,从而保证集群不会挂掉。并且如果之前的旧主节点恢复正常时。
  上面那个例子里, 集群有ABC三个主节点, 如果这3个节点都没有加入从节点,如果B挂掉了,我们就无法访问整个集群了。A和C的slot也无法访问。

     所以我们在集群建立的时候,一定要为每个主节点都添加了从节点, 比如像这样, 集群包含主节点A、B、C, 以及从节点A1、B1、C1, 那么即使B挂掉系统也可以继续正确工作。

     B1节点替代了B节点,所以Redis集群将会选择B1节点作为新的主节点,集群将会继续正确地提供服务。 当B重新开启后,它就会变成B1的从节点。

    不过需要注意,如果节点B和B1同时挂了,Redis集群就无法继续正确地提供服务了。

3. Construction of redis cluster

集群中至少应该有奇数个节点,所以至少有三个节点,每个节点至少有一个备份节点,所以下面使用6节点(主节点、备份节点由redis-cluster集群确定)。

Well, all require at least 6 redis services.
Then start these 6 redis services on this machine, but remember to modify the configuration before starting.

port  7000                                        //端口7000,7002,7003        bind 10.93.84.53                                     //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群daemonize    yes                               //redis后台运行pidfile  ./redis_7000.pid          //pidfile文件对应7000,7001,7002cluster-enabled  yes                           //开启集群  把注释#去掉cluster-config-file  nodes.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 

Then copy redis-trib.rb in the src directory in the redis source code to the current path.
Install ruby ​​environment

yum install ruby  
yum install rubygems  
gem install redis-3.2.2.gem

Check the startup status of 6 redis on this machine

ps -ef | grep redis

Finally we use redis-trib.rb to create a redis cluster. Use the create command –replicas 1 parameter to create a slave node for each master node, a pair of two, the first one is the master and the second is the slave.

./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

Use redis-cli for testing. You need to add the -c parameter to connect in cluster mode.

redis-cli -c

To enter, you must add the -c parameter, which means the connection is in cluster mode.
At this point, the redis cluster is successfully established.
But here I only need 6 ports for local testing. To be honest, at least two machines are needed. How do we do security?
1. Modify redis.conf and change the bind option to the current LAN IP
2. Configure the firewall to only allow access to another redis server. Of course, the IP of the application server must also be allowed to access. . . Otherwise, the program will not be able to connect to the redis service, which is a waste of time. . . .

//先关掉6379(redis服务使用的端口)iptables -I INPUT -p tcp --dport 6379  -j DROP 
//允许192.168.1.0这个机器进行访问本机的6379端口iptables -I INPUT -s 192.168.1.0 -p tcp --dport 6379 -j ACCEPT

2. Connect to the redis cluster in php
First use php –ri redis to view the redis extended version. Must be above version 3.0.
Reference
The second parameter only needs one node in the cluster, and you don’t need to fill it all in

$obj_cluster = new RedisCluster(NULL, ['127.0.0.1:6380']);echo $obj_cluster->get('name1');

Get it done

Related recommendations:

php method to query mysql and cache it in redis

php command sharing to operate redis


The above is the detailed content of Implementation of redis cluster and calling using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php regular expressionNext article:php regular expression