Implementation of redis cluster and calling using php
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
