Preface
I vaguely remember the second interview that I attended during the summer vacation in my sophomore year. Here, Weipin will join the school in a two-week study program for your company, I met the leader (X.C) of the PHP class (which was divided into three directions at the time: PHP, IOS, and Android). During the interview, I performed normally. I remember asking me about Memcache, the degree of understanding of Redis Cache, before that, I only heard about what they mean, but I have never learned how to practice it. After that, I will go back and write down the questions I have not answered one by one, later, I tried to win the opportunity to enter this project. I would like to express my gratitude to leader, dogstar, Laura, and your company! (Of course, thanks to your friends ~) You gave me the opportunity and hope your company will get better and better! Well, it seems to be a little far away. What I want to talk about is that the cache is widely used in companies or a little larger applications, especially Web applications, therefore, it is important to master and use it. In addition, I am in charge of the main maintenance of the two clusters in my internship project team, one of which is the SSDB distributed cluster, so today I will simply learn how to build an SSDB cluster and use it in PHP.
Set up SSDB
Download SSDB from the SSDB official website, decompress, compile, and install SSDB.
# Wget https://github.com/ideawu/ssdb/archive/master.zip
# Unzip master.zip
# Cd ssdb-master
# Make
# Sudo make install [PREFIX =/home/users/denglitong/local/ssdb]. If you do not know the PREFIX parameter, it is installed in the/usr/local directory by default, here I installed it in the/home/users/denglitong/local/ssdb Directory
I have not encountered any installation errors here, so I directly start the ssdb service and go to the ssdb directory cd/home/users/denglitong/local/ssdb:
Open ssdb. conf to make necessary changes, such as the listening port and log mode:
Start ssdb: Run #./ssdb-server-d-c ssdb. conf in the ssdb installation directory,
View ssdb processes: ps-ef | grep ssdb-server,
I have built two ssdb processes on my local machine, so there will be two processes. When I build the second ssdb, I will directly copy the first one and modify ssdb. conf to start it.
The ssdb-cli in the ssdb installation directory is used to connect to the ssdb service and run. /ssdb-cli [-h 127.0.0.1]-p 8600 can connect to the ssdb node for the set and get test. If it is a local machine, the server address specified by-h 127.0.0.1 can be defaulted.
Now, we have built SSDB for multiple nodes. The next step is to build the Twenproxy proxy server.
- Build Twemproxy
Twemproxy was originally used with Redis, but the performance of SSDB is better than that of Redis. SSDB will be used to replace Redis later, while newer versions of SSDB already support the Redis protocol, so Twemproxy can proxy Redis/SSDB mashups. Here we use Twemproxy to proxy multiple SSDB nodes.
Download twemproxy: # git clone https://github.com/twitter/twemproxy (git first installed without git)
# Cd twemproxy
# Autoreconf-fvi, here my centos6.5 prompts the autoconf-2.63 version needs to be upgraded to the autoconf-2.64 version:
First check the current version: # rpm-qf/usr/bin/autoconf
Uninstall Current version: # rpm-e -- nodeps autoconf-2.63
Download New version: # wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.64.tar.gz
Uncompress installation: # tar zxvf autoconf-2.64.tar.gz
Cd autoconf-2.64
./Configure -- prefix =/usr
(This can also be installed in other directories, but remember to create a soft connection to the execution file under the bin directory after installation to/usr/bin)
Make & make install
Check whether the installation is successful: #/usr/bin/autoconf-V
# Compile and install: make & make install [PREFIX =/home/users/denglitong/local/twemproxy], if the PREFIX parameter is not added, it is installed in the/usr/local/share/sbin directory by default, and the configuration file is in the/etc/nutcracker Directory. Here I install it in the specified directory, therefore, you also need to create the conf directory under the installation directory and put the conf/nutcracker in the source package. copy yml,
Here, modify conf/nutcracker. yml and change the servers list to an SSDB node created locally:
Start twemproxy: #./sbin/nutcracker-d-c./conf/nutcracker. yml
Here, "-d" indicates the running mode of the later processes, and "-c" indicates the configuration file. You can also use the "-t" parameter followed by the configuration file to test whether the syntax is correct.
Now the establishment of The nutcracker is complete, and we will test whether it can work. Note that ssdb-cli uses the ssdb protocol when connecting to the ssdb node, the redis protocol is used when twemproxy connects to ssdb. Because SSDB is compatible with the redis protocol, twemproxy is not compatible with the ssdb protocol, therefore, using ssdb-cli to connect to the port of the twemproxy listener cannot be connected. Here we also need to install Redis, and then use redis-cli to connect to twemproxy, so as to facilitate verification of the build result, of course, you can only install the phpredis module to connect to twemproxy for interactive verification. However, we recommend that you install a local Redis instance.
- Install Redis
Download the Redis package, decompress the package, run make, generate the redis-server startup file in the root directory, and generate the redis-cli connection client file in the src directory, what we need is the redis-cli, which does not need to start a redis storage node. Then we create a soft connection to/usr/bin for convenient connection: sudo ln-s/home/users/denglitong/local/redis-3.2.3/src/redis-cli/usr/bin/redis-cli, then you can connect to twemproxy for verification:
The phpredis extension is used to connect redis with php for the set/get test instead of the manual test. The phpredis extension will be discussed later,
We can see that the twemproxy proxy can unify multiple ssdb nodes to provide an ip/port for external services. The number of tests here is relatively small, when I manually set dozens of values, it will fall into two ssdb nodes on average.
- Build phpredis
Redis-cli is the client connecting to redis in linux. twemproxy itself supports the redis protocol, so it can be used to connect to twemproxy, but we also need to connect to redis in php, therefore, you need to install the redis client for php, which is the phpredis extension.
Install phpredis extension:
$ Wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz
$ Cd phpredis-2.2.4 # go to the phpredis Directory
$/Usr/local/php/bin/phpize # php Installation Path
$./Configure -- with-php-config =/home/users/denglitong/local/The php-5.6.24/bin/php-config
$ Make & make install
The with-php-config parameter specified here specifies the php directory to which the redis extension is installed, after compilation and installation, an extended directory will be generated under the extension directory of the php installation directory, which contains redis. so extension, and then modify php. ini file:
Restart the php-fpm process and check the phpinfo page to see if the redis extension has been compiled:
This is OK, and then you can use the redis class in php to connect to redis (twemproxy ):
- Well, here, a pseudo cluster of twemproxy and ssdb nodes has been built up. Why is it a pseudo cluster? Because it can only use consistent hash to set the set value to each node, some advanced features of the cluster, such as server load balancer and how to migrate data and when to migrate data, are not implemented, and are not distributed locally. Therefore, the cluster is a pseudo cluster. In fact, in this way of proxy + service nodes, the twemproxy proxy is easy to become a performance bottleneck, but SSDB standalone can also handle terabytes of data, of course, a proxy of the official SSDB distributed cluster is under development. At that time, we may not be far from the real SSDB cluster.
- Follow-up
This article was originally intended to be written last night. It was half written, but at, the company had something to deal with. It was about, so it was not written last night, it's not too late today, so I wrote these codes here! Then I have made breakfast. I have breakfast!