1. Compile and install libevent:
Wget http://monkey.org /~ Provos/libevent-1.4.9-stable.tar.gz
Tar zxvf libevent-1.4.9-stable.tar.gz
CD libevent-1.4.9-stable/
./Configure -- prefix =/usr
Make & make install
CD ../
2. Compile and install memcached:
Wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
Tar zxvf memcached-1.2.6.tar.gz
CD memcached-1.2.6/
./Configure -- With-libevent =/usr
Make & make install
CD ../
3. Compile and install magent:
Mkdir magent
CD magent/
Wget http://memagent.googlecode.com/files/magent-0.5.tar.gz
Tar zxvf magent-0.5.tar.gz
/Sbin/ldconfig
Sed-I "s # libs =-Levent-LM # G" makefile
Make
CP magent/usr/bin/magent
CD ../
The previous three steps are skipped, and then the powerful functions of magent + memcached are tested.
Environment Description: centeros 5.4 (Virtual Machine) IP Address: 192.168.128.20.
Test client: XP
Open five memcached ports on port 133 first
Run
Memcached-M 1-u root-D-l 192.168.128.20.- P 10001
Memcached-M 1-u root-D-l 192.168.128.20.- p 10002
Memcached-M 1-u root-D-l 192.168.128.20.- P 10003
Memcached-M 1-u root-D-l 192.168.128.20.- P 10004
Memcached-M 1-u root-D-l 192.168.128.20.- P 10005
Check whether the listening port is successfully enabled.
PS-Ef | grep memcached
The result is 6 rows. The last line indicates that the command is successfully executed.
Start magent and set the startup parameter-u, of course, the startup account-N is the maximum number of connections, and-l is the IP address listened by magent-P is the port listened by magent
The following format is-s IP: Port represents the master server and then the specified IP: PORT format
-B IP: Port indicates the backup server,
Here I set 10003, as the master memcached Server
10004,10005 is the memcached Backup Server
Magent-u root-N 51200-l 192.168.128.20.- P 12000-s 192.168.128.20.: 10001-s 192.168.128.20.: 10002-s 192.168.128.20.: 10003-B 192.168.128.20.: 10004-B 192.168.128.20.: 10005
After executing the command, you can check whether the startup is successful.
PS-Ef | grep magent
Two OK lines are displayed. Continue
Test on the XP Client
Connect to magent port 12000 directly
Telnet 192.168.128.20.12000
Execute stats
It indicates that three memcached ports have been loaded in the magent.
Then proceed to set
Set key1 0 0 1
1
Stored
Set key2 0 0 1
2
Stored
Set key3 0 0 1
3
Stored
Three keys are set in sequence. The <command name> <key> <flags> <exptime> <bytes> \ r \N
For detailed explanations, please see: http://blog.zol.com.cn/737/article_736087.html
Since three keys are set, let's read them.
Run the get key1 get key2 get key3 command.
Returned in turn
At this point, the process of writing cache data to the database has been completed, but we need to understand the process. Haha
Then we directly log on to the memcached port to get data and see how it is saved.
Telnet 192.168.128.20.10001 first
Execute stats
Stat PID 7496
Statuptime 894
Stat time 1319202978
Stat version 1.2.6
Stat pointer_size 32
Stat rusage_user 0.000000
Stat rusage_system 0.001999
Stat curr_items 1
Stat total_items 1
Stat bytes 54
Stat curr_connections 3
Stat total_connections 4
Stat connection_structures 4
Stat cmd_get 1
Stat performance_set 1
Stat get_hits 1
Stat get_misses 0
Stat evictions 0
Stat bytes_read 36
Stat bytes_written 32
Stat limit_maxbytes 1048576
Stat threads 1 \ stat threads 1 \
Note: 10001 only one key is cached
Which one is stored?
Get key1
Value key1 0 1
1
End
Key1 stored in OK
Telnet 192.168.128.20.10002 again
Direct
Get key1
End
Get key2
Value key2 0 1
2
End
Get key3
End
Get key4
End
Get key5
End
Indicates the key2 cached on MySQL 10002,
It is not hard to see that 10003 should be the cached key3.
Try
Telnet 192.168.128.20.10003
Then
Get key1
End
Get key2
End
Get key3
Value key3 0 1
3
End
Get key4
End
Get key5
End
Now, we know that the magent distributes the content to be cached to the backend memcached in sequence based on its own algorithms, but we do not know the specific algorithms, I personally think it should be distributed in sequence. Of course, when memcached is alive,
I know that the master server distributes loads, but I don't know if the slave server is like this,
Telnet 192.168.128.20.10004
Sequential
Get key1
End
Get key2
Value key2 0 1
2
End
Get key3
End
Get key4
End
Get key5
End
The slave service 10004 does not cache all three keys, that is, the previous key1 key2 key3
Telnet 192.168.128.20.10005 again
Get key1
Value key1 0 1
1
End
Get key2
End
Get key3
Value key3 0 1
3
End
Get key4
End
Get key5
End
It seems that the backup server also implements load. Haha, it is estimated that the algorithm is the same as that of the master server,
Then I kill all the processes on the master server's 10001 10002 10003
Root 7496 1 0 21:01? 00:00:00 memcached-M 1-u root-D-l 192.168.128.20.- P 10001
Root 7498 1 0 21:01? 00:00:00 memcached-M 1-u root-D-l 192.168.128.20.- p 10002
Root 7500 1 0 21:01? 00:00:00 memcached-M 1-u root-D-l 192.168.128.20.- P 10003
Kill 7496 7498 7500
Then get key1 on the magent Port
Value key1 0 1
1
End
Get key2
Value key2 0 1
2
End
Get key3
Value key3 0 1
3
End
The value is not affected.
However, if the slave server is also dead, it will become powerless,
The test ends now.