


Laravel project uses twemproxy to deploy the complete steps of redis cluster php instance
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 that 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!
Summary
Articles you may be interested in:
How to open PHP7 Opcode creates powerful performance with detailed explanation of php skills
##PHP solves the problem of rabbits giving birth to rabbits based on recursive algorithm php skills
About ThinkPHP Detailed explanation of exception handling in_php example
The above is the detailed content of Laravel project uses twemproxy to deploy the complete steps of redis cluster php instance. 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

AI Hentai Generator
Generate AI Hentai for free.

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

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

Comparing Laravel's Blade and CodeIgniter's Twig template engine, choose based on project needs and personal preferences: Blade is based on MVC syntax, which encourages good code organization and template inheritance. Twig is a third-party library that provides flexible syntax, powerful filters, extended support, and security sandboxing.

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.

For small projects, Laravel is suitable for larger projects that require strong functionality and security. CodeIgniter is suitable for very small projects that require lightweight and ease of use.

Laravel - Facades - Facades provide a static interface to classes that are available in the application's service container. Laravel facades serve as static proxies to underlying classes in the service container, providing the benefit of a terse, exp

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.
