Memcacheq is a simple distributed Message Queuing service.
First, the application background of Memcacheq
What is in Web applications
Would you need a message queue? The main reason is because in the high concurrency environment, because it is too late to synchronize, the request will often occur jams, for example, a large number of insert,update, such as requests to reach MySQL, directly lead to countless row lock table lock, or even the final request will accumulate too much, triggering too Manyconnections error. By using Message Queuing, we can handle requests asynchronously, thereby relieving system stress. In the age of Web2.0, high concurrency is becoming more common, making Message Queuing a must-have trend, and a number of implementations have sprung up, like Twitter used RABBITMQ to implement Message Queuing services, and now instead use Kestrel to implement Message Queuing services, and there are many other options , such as: Activemq,zeromq and so on.
Most of the above Message Queuing software has become extremely heavyweight in order to implement protocols such as AMQP,STOMP,XMPP, but in many Web applications the reality is that we just want to find a solution that alleviates high concurrent requests without the need for assorted functionality, A lightweight Message Queuing implementation is what we really need.
Second, the characteristics of Memcacheq
1 simple and easy to use
2 Fast processing speed
3-odd queues
4 Concurrency performance is good
5 is compatible with the Memcache protocol. This means that as long as the memcache extension can be installed, no additional plug-ins are required.
Third, install
Memcacheq relies on libevent and berkleydb.
Berkleydb the data used to persist the storage queue. So that when the Memcacheq crashes or the server hangs out,
Do not cause the loss of data. This is important and important.
Its installation relies on BerkeleyDB and libevent, so install the BerkeleyDB and Libevent first:
Where libevent if you have installed the memcached has been installed, if not sure, check it
1. Check libevent First, Libevent-devel is installed: rpm-qa|grep libevent output must contain libevent, Libevent-deve, If missing, install with the following command:
yum install libevent yum
Install libevent-devel
considerations: Libevent, Libevent-devel priority to use Yum installation source, CD-ROM image of the RPM package installation, so that stability and compatibility can be guaranteed, the online use of the source code installed Libevent method will be problematic, because it is likely that the system has been installed Libevent, The use of source code installation, will inevitably lead to conflicts, causing unexpected problems, so be sure to use the above command to check whether the system has installed the appropriate library
2. Install Berkleydb
1.tar ZXVF bdb-5.3.3.tar.gz
2.CD db-5.3.3/
#需要进入特定操作系统编译环境, the compilation of more conventional software is somewhat different
3.CD build_unix/
4.. /dist/configure--prefix=/usr/local/berkeleydb
#如果没有指定特殊安装路径, the compilation is complete, and the path to the Berkeley DB Runtime needs to be added to the system configuration
echo "/usr/local/berkeleydb.5.3/lib/" >>/etc/ld.so.conf
#重载系统Ld运行库
Ldconfig
5. Make & make Install
Remember to change/etc/ld.so.conf file, add/usr/local/berkeleydb.5.3/lib Ah, or the back of the MCQ will install errors.
And BerkeleyDB is going to download it.
Click to download Berkeley DB 5.3.21.rar
Install Memcacheq below,
First download a memcacheq-0.2.0.rar
Unpack, enter directory
./configure–with-bdb=/usr/local/berkeleydb.5.1–with-libevent=/usr/local/lib–enable-threads
Make
Make install
The key is the red font that step, must be entered correctly, otherwise make does not pass, can not install
here's the boot.
memcacheq-d-r-u root-p21201-h/data/memcacheq-n-v-l 1024-b 1024 >/data/mq_error.log 2>&1
It is not recommended to use root, and some posts say no, I can test here, but it may not be safe.
1 The following is the starting point of the parameter
Use the memcacheq-h command to view command-line options
2 This is right to start memcacheq:memcacheq-d-uroot-r-p11212-h/home/wwwroot/mcq-n-r-v-L 1024-b 1024 >/HOME/WWWLOGS/MQ _error.log 2 > &1
3 doesn't know why./usr/local/memcacheq/bin/memcacheq-d-L 127.0.0.1-a 8192-h/data/memcacheq-b 65535-n-r-u Root
-P <num> TCP listening ports (default:22201)
-U <num> UDP listening ports (default:0, off)
-S <file> UNIX socket Path (network not supported)
-A <mask> UNIX socket access Mask (default 0700)
-L <ip_addr> Monitor NIC
-D Daemon
-R maximizes core file limits
-U <username> Run as user (only when run as root)
-C <num> Maximum number of concurrent connections (default is 1024)
-v Verbose output (print errors/warnings while in event loop)
-VV more verbose output (also print client commands/reponses)
-I print license information
-P <file> PID files
-T <num> number of threads (default 4)
--------------------BerkeleyDB Options-------------------------------
-M <num> berkeleydb memory cache size, default is 64MB
-A <num> bottom page size, default is 4096, (512B ~ 64KB, Power-of-two)
-H <dir> Database home directory, default is '/data1/memcacheq '
-l <num> Log buffer size, default is 32KB
-C <num> How many seconds checkpoint once, 0 for disable, the default is 5 minutes
-T <num> how many seconds Memp_trickle once, 0 for disable, default is seconds
-S <num> How many sec queue stats dump once, 0 for disable, default is seconds
-e <num> cache per cent required to refresh, default is 60%
-e <num> How many pages of a single DB file, default is 16*1024, 0 for disable
-B <num> Specify the length of the message body, Unit bytes, default is 1024
-D <num> How many milliseconds to do deadlock detection (deadlock detecting), 0 for disable, default is 100ms
-N open Db_txn_nosync for significant performance improvement, default is off
-r automatically deletes log files that are no longer needed, and the default is off
Test
Third, testing
1.telnet 10.218.31.121 22201
2.stats
2.stats queue
3.set Q4 0 0 5
4 Hello
5 Get Q4
6 Stats Queue
7 Delete Q4
If the set time to fill the successful not_stored, check your start command bar, parameters are not set, if you are novice, dry cui more than a few posts, try to start the command, change the parameters, on the line
Four, use
After using the above command to start MQ, (note that the-b parameter above indicates that the body length of messag cannot exceed 1024 bytes), only two commands are required to use MQ: Set and get:
Set <queue name> <flags> 0 <message_len>\r\n
<put your message body here>\r\n
stored\r\n
Get <queue name>\r\n
VALUE <queue name> <flags> <message_len>\r\n
<your message body would come here>\r\n
end\r\n
As you can see, the Memcache protocol is basically consistent, except that the key name is changed to queue name, and the Expire_time parameter is ignored in the set's command. After all, MQ data storage exists in BerkeleyDB, does the persistent storage, has no memory expiration time.
When the set command is used, a new message is written to the specified message queue, which is to insert a new data into the BerkeleyDB, and when the Get command is used, a new message is removed from the specified queue, that is, a data is delete from the berkeleydb. When you use stats to view a specified queue, you can see how many messages the queue received, and how many were taken out of it.
Example:
Copy Code code as follows:
fengbo@onlinegame-10-121:~$ telnet 127.0.0.1 22202
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set Q4 0 0 5
Hello
STORED
Set Q4 0 0 5
World
STORED
Stats queue
STAT Q4 2/0
End
Get Q4
VALUE Q4 0 5
Hello
End
Stats queue
STAT Q4 2/1
End
The above executed two sets of commands, using the Stats queue view, you can see Q4 queue 2, has been removed 0; When you use get to remove the first, then use the stats queue to view, Q4 message has 2, which has been removed 1.
PHP Test:
Copy Code code as follows:
Session_Start ();
$memcache _obj = new Memcache;
$memcache _obj->connect (' 127.0.0.1′, 11212) or Die ("error");
Memcache_set ($memcache _obj, ' K ', 10, 0, 0);
echo "Queue". Memcache_get ($memcache _obj, ' K ');
Memcache_close ($memcache _obj);
Note:
This is the kind of problem that comes up.
Memcacheq:error while loading shared libraries:libdb-5.0.so:cannot open Shared object file:no such file or directory
Solution: Build a libdb-5.0.so soft chain under the/usr/lib, OK.
Ln-s/usr/local/berkeleydb.5.0/lib/libdb-5.0.so/usr/lib/
Five, close Memcacheq.
Use the PS command to check the MEMCACHEQ process: Ps-ef|grep wuf, and then kill the process directly.