Twemproxy is a proxy server that can reduce the number of connections opened by memcached or Redis servers. The following article is mainly about the Laravel project using Twemproxy to deploy Redis cluster information, the article through the sample code introduced in very detailed, the need for friends can refer to the following
Objective
Twemproxy is a redis agent developed by Twitter Proxy,twemproxy can use multiple Redis servers as one, and developers access these Redis servers through Twemproxy You don't have to worry about which Redis server is going to read k-v data or update k-v data to a dataset, and also solve the problem of Redis sharing in multiple servers. With the help of Redis's Master-slave replication, you can guarantee that an entire data set can exist without any redis working.
What is the use of twemproxy? It can:
-
Reduce the number of connections to the cache server by proxy
-
Automatically share data across multiple cache servers
-
Support for consistent hashing through different policies and hash functions
-
To disable failed nodes by configuration
-
Running on multiple instances, the client can connect to the first available proxy server
-
Supports streaming and batching of requests, thus reducing the cost to and fro
Here are the installation and configuration of Twemproxy:
1, install twemproxy before need to install M4, autoconf, Automake, Libtool, GCC, or will report all kinds of mistakes
Installing 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
Installing autoconf, Automake, Libtool, GCC
Yum-y install autoconf automake libtool gcc
2, Installation Twemproxy
git clone https://github.com/twitter/twemproxy.gitautoreconf-fvi #生成configure文件. Configuremake && Make Install
3. Edit the configuration file
Vim Conf/nutcracker.yml
alpha:listen:127.0.0.1:22121 #监听端口 hash:fnv1a_64 #key值hash算法, default fnv1a_64 distribution:ketama #分布算法 # Ketama consistency hash algorithm; Modula is very simple, is based on the key value of the hash value of modulo; random distribution auto_eject_hosts:true #摘除后端故障节点 redis:true # Is the Redis cache, the default is False timeout:400 #代理与后端超时时间, Ms server_retry_timeout:200000 #摘除故障节点后重新连接的时间, Ms Server_failure_ Limit:1 #故障多少次摘除 Servers:-172.16.175.145:6379:1 -172.16.175.122:6379:1
4. Change Redis configuration file
The IP and port of the Redis listener should correspond to the twemproxy (because I just put the twemproxy on one of the machines, if both are installed, you can not change it)
Change the BIND option in the two server redis.conf, changing the 127.0.0.1 to 172.16.175.145 and 172.16.175.122 in this example, respectively.
5. Open Twemproxy
Nutcracker-t-c/twemproxy/conf/nutcracker.yml #测试配置文件 If not add-C is detected by default 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 is using Laravel, just set the Redis_host and Redis_port in the. env above to 127.0.0.1 and 22121
Note that you will also need to comment out the ' database ' \ 0 in the Redis configuration in database.php under the Config folder, Predis the select operation on this configuration will cause a connection error.
Do a Redis read and write test, no accident, twemproxy can be normal use!