search
HomeBackend DevelopmentPHP TutorialLaravel 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function