Home > Article > Backend Development > Complete steps for laravel project to deploy redis cluster using twemproxy_php example
Twemproxy is a proxy server that can reduce the number of connections opened by the Memcached or Redis server. The following article mainly introduces you to the relevant information about the Laravel project using twemproxy to deploy the redis cluster. The article introduces it in detail through the sample code. Friends in need can refer to it
Preface
twemproxy is a redis proxy proxy developed by Twitter. Twemproxy can use multiple redis servers as one. When developers access these redis servers through twemproxy, they don’t have to worry about which one to go to. A redis server reads k-v data or updates k-v data into the data set, which also solves the problem of redis sharing among multiple servers. With the help of redis's master-slave replication, it can be guaranteed that even if any redis fails to work, an entire data set can still be guaranteed to exist.
What is Twemproxy used for? It can:
Reduce the number of cache server connections through proxy
Automatically share data among multiple cache servers
Support consistent hashing through different strategies and hashing functions
Disable failed nodes through configuration
Run on multiple instances, the client can connect to the first available proxy server
Supports streaming and batch processing of requests, thus reducing round-trip consumption
The following introduces the installation and configuration of twemproxy:
1. You need to install m4 before installing twemproxy , autoconf, automake, libtool, gcc, otherwise various errors will be reported
Install m4
wget http://mirrors.kernel.org/gnu/m4/m4-1.4.13.tar.gz && tar -xzvf m4-1.4.13.tar.gz && cd m4-1.4.13 && ./configure –prefix=/usr/local make && make install
Install autoconf, automake, libtool, gcc
yum -y install autoconf automake libtool gcc
2. Install twemproxy
git clone https://github.com/twitter/twemproxy.git autoreconf -fvi #生成configure文件 .configure make && make install
3. Edit the configuration file
vim conf/nutcracker.yml
alpha: listen: 127.0.0.1:22121 #监听端口 hash: fnv1a_64 #key值hash算法,默认fnv1a_64 distribution: ketama #分布算法 #ketama一致性hash算法;modula非常简单,就是根据key值的hash值取模;random随机分布 auto_eject_hosts: true #摘除后端故障节点 redis: true #是否是redis缓存,默认是false timeout: 400 #代理与后端超时时间,毫秒 server_retry_timeout: 200000 #摘除故障节点后重新连接的时间,毫秒 server_failure_limit: 1 #故障多少次摘除 servers: - 172.16.175.145:6379:1 - 172.16.175.122:6379:1
4. Change the redis configuration file
The IP and port monitored by redis must correspond to twemproxy (because I only installed twemproxy on one of the machines, if it is installed on both machines, there is no need to change it. )
Change the bind option in redis.conf of the two servers respectively, change 127.0.0.1 to 172.16.175.145 and 172.16.175.122 in this example
5. Turn on twemproxy
nutcracker -t -c /twemproxy/conf/nutcracker.yml #测试配置文件 如果不加-c 默认是检测conf/nutcracker.yml
nutcracker -d -c /twemproxy/conf/nutcracker.yml -p -o /data/logs/twemproxy/redisproxy.log #启动并将日志记录到/data/logs/twemproxy/redisproxy.log
6. Change the redis connection in the project to twemproxy ip and port
My project uses laravel, just set the REDIS_HOST and REDIS_PORT in the .env to 127.0.0.1 and 22121
Note, you also need to set the config Comment out 'database' => 0 in the redis configuration in database.php under the folder. Predis will perform a select operation on this configuration by default, which will cause a connection error.
Do a redis read and write test. If nothing unexpected happens, twemproxy can be used normally!
Related recommendations:
The above is the detailed content of Complete steps for laravel project to deploy redis cluster using twemproxy_php example. For more information, please follow other related articles on the PHP Chinese website!