Session sharing: How PHP and Redis clusters implement session sharing

Source: Internet
Author: User
Tags install php redis cluster redis server
This article brings to you the content is about session sharing: How PHP and Redis cluster to achieve session sharing, there is a certain reference value, the need for friends can refer to, I hope to help you.

One, Redis db cluster installation

    1. Software version redis-3.2.11, php-redis-2.2.4

Role

IP Address

Host Name

Redis Database Server

192.168.1.41

Redis41

Slave 1 Redis Database server

192.168.1.42

Redis42

Slave 2 Redis Database server

192.168.1.43

Redis43

VIP Address

192.168.1.40

Three databases are also configured

Adduser-s/sbin/nologin-d/var/lib/redis redisyum-y install gcc gcc-c++tar-xf redis-3.2.11.tar.gzcd redis-3.2.11/make MALLOC=LIBC   //with malloc this environment variable to compile Redis, and libc is not the default allocator, the default is jemallocmake Prefix=/usr/local/redis install    // Specify the installation path mkdir-p/USR/LOCAL/REDIS/CONFCP redis.conf/usr/local/redis/conf/    CP sentinel.conf/usr/local/redis/conf/

Common configuration Options/usr/local/redis/conf/redis.conf

–port 6379     //Port –bind 192.168.1.41     //IP address –tcp-backlog 511//tcp     Connections Total –timeout 0     //Connection time-out –tcp-keepali ve     //Long connection time –daemonize Yes/     /daemon mode run –databases     //Database number –logfile/var/log/redis_6379.log     // PID file –maxclients 10000     //number of concurrent connections –dir/var/lib/redis/6379    //Database directory

Configure Redis to enable SYSTEMCLT System management (not required)

    1. Vim/usr/lib/systemd/system/redis.server

[Unit] Description=redis in-memory Data storeafter=network.target[service]user=redisgroup=redistype=simpleexecstart=/usr /local/redis/bin/redis-server/usr/local/redis/conf/redis.confexecstop=/usr/local/redis/bin/redis-cli Shutdownrestart=alwaysrestartsec=42s[install]wantedby=multi-user.target

Second, install Php-redis extension

Configuring PHP to support Redis

Tar zxvf phpredis-2.2.4.tar.gz   cd phpredis-2.2.4    /usr/local/php/bin/phpize    //phpize configuration file is generated with the. Configure--enable-redis--with-php-config=/usr/local/php/bin/php-config make  && make install    php-m |grep Redis

Write a test File redis.php page test

<?php$redis = new Redis (); $redis->connect (' 192.168.1.41 ', 6379); $redis->set (' redistest ', ' 666666 '); echo $ Redis->get (' redistest ');? >

Third, Redis server cluster is highly available

the Redis cluster implementation is probably in the following ways :

Redis-cluster, the  official provision of the cluster construction scheme (too heavy, more suitable for later data when the use of a large amount of time) redis+keepalive  virtual IP, multiple configurations are very complex, not suitable for maintenance, need slaveof no one Script Redis+zookeeper  need to introduce zookeeper, to the existing code changes Redis+sentinel   Redis self-monitoring middleware, Sentinel mode configuration three Redis server configuration master-slave relationship, in the slave Add configuration slaveof 192.168.1.41 6379 view cluster master-slave relationship redis-cli-h 192.168.1.41-p 6379 Info replication

Modify Redis Sentinel Profile sentinel.conf

Bind 192.168.1.41protected-mode nodaemonize yesport 26379dir/tmpsentinel monitor MyMaster 192.168.1.41 6379 2sentinel do Wn-after-milliseconds mymaster 3000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 5000sentinel Client-reconfig-script mymaster/usr/local/redis/conf/reconfig.sh in the main, starting from the Sentinelredis-sentinel/usr/local/redis /conf/sentinel.conf View Sentinel Status redis-cli-h 192.168.1.41-p 26379 info Sentinel

Iv. Client compatibility issues

Client programs (such as PHP programs) require IP and port to connect to Redis, but when Redis-server fails over, the primary database is changed, so the IP address is also variable, how does the client program perceive the current master Redis IP address and port?
Redis-sentinel provides an interface to request any Sentinel, send Sentinel Get-master-addr-by-name <master name> to get the current master Redis IP and port.

Solution:

Add configuration switch Script sentinel.conf, when the primary database service goes down, the VIP drift is automatically switched to master and slave.
Sentinel Client-reconfig-script mymaster/usr/local/redis/conf/reconfig.sh

#!/bin/bash#mymaster leader start 192.168.1.41 6379 192.168.1.42 6379vip= "192.168.1.40/24" local_ip=$ (IP  addr Show Dev eth0 |awk ' $1== "inet" {print $} ') if [["${local_ip%%/*}" = = "$4"]];then   /usr/sbin/ifconfig eth0:1 downelif [["${ local_ip%%/*} "= =" $6 "]];then   /usr/sbin/ifconfig eth0:1" ${vip} "fi
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.