优选主流主机商
任何主机均需规范使用

MemcacheQ详细安装教程与高效使用技巧分享

一. 安装
MemcacheQ 是一个简单的分布式队列服务,它的运行依赖于BerkeleyDB 和 libevent,所以需要先安装BerkeleyDB和libevent.
Berkeley DB 4.7 or later
Download from <http://www.oracle.com/database/berkeley-db/db/index.html>
How to install BerkeleyDB:

1 2 3 4 5 6 $ tar -xvzf db-5.3.21. tar .gz $ cd db-5.3.21/ $ cd build_unix/ $.. /dist/configure $ make $ make install

安装BerkeleyDB时,可以手动指定安装路径:

../dist/configure –prefix=/usr/local/berkeleydb

不指定的话,默认安装在:/usr/local/BerkeleyDB.5.3

libevent 1.4.x or later

先检查libevent 是否已经安装:

#rpm -qa|grep libevent
libevent-devel-2.0.10-2.fc15.x86_64
libevent-2.0.10-2.fc15.x86_64
libevent-2.0.10-2.fc15.i686

或者:

ls -al /usr/lib |grep libevent

如果还没有安装:

Download from <http://monkey.org/~provos/libevent/>
How to install libevent:

1 2 3 4 5 $ tar -xvzf libevent-1.4.x-stable. tar .gz $ cd libevent-1.4.x-stable $. /configure $ make $ make install

安装libevent时,可以手动指定安装路径:

./configure –prefix=/usr/local/libevent

不指定的话,默认安装在:/usr/lib64(64位系统)或者/usr/lib(32位系统)

memcacheQ

下载软件包:http://code.google.com/p/memcacheq/downloads/list
解压缩,cd进目录

1 2 3 . /configure -- enable -threads make make install

configure 时,如果libevent 不是安装在默认目录,需–with–libevent=/usr/local/libevent指定libevent的安装目录
若没有将

/usr/local/lib
/usr/local/BerkeleyDB.5.3/lib

添加进/etc/ld.so.conf 并运行 /sbin/ldconfig 则需–with-bdb=/usr/local/BerkeleyDB.5.3 指定berkeleyDb库的路径

二.使用

启动memcacheQ
使用memcacheq -h 的命令来查看命令行选项
启动memcacheq:memcacheq -d -u nobody -r -H /tmp/memcacheq -N -R -v -L 1024 -B 1024 > /tmp/mq_error.log 2>&1
启动时需-u 参数,指定运行memcacheQ的用户,且指定的用户必须有数据文件的读写权限,如这里的/tmp/memcacheQ目录,否则不能启动

命令行使用memcacheQ
telnet 127.0.0.1 22202
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.

只有两个命令可以在命令行下使用memcacheQ
写对列:
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 will come here>\r\n
END\r\n
与memcache协议基本一致,只是把key name换成queue name,而且在set的命令中,忽略了expire_time的参数。mq的数据存储是存在berkeleyDB中,做了持久化存储,没有内存的过期时间。
示例:
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

三.安装使用过程中可能出现的错误

1.编译出现错误:checking for library containing db_create… no
configure: error: cannot find libdb.so in /usr/local/BerkeleyDB.5.3/lib
需要修改 configure 中的BerkeleyDB中的预编译参数vim configure找到bdbdir=”/usr/local/berkeleydb”改为
bdbdir=”/usr/local/BerkeleyDB.5.3″再次编译

2.configure: error: cannot find libdb.so in /usr/local/BerkeleyDB.5.3/lib
出现此错误的原因在于没有安装BerkyleyDb,安装即可

3./usr/local/memcacheq/bin/memcachq -h
运行报:
memcacheq: error while loading shared libraries: libdb-5.3.so: cannot open shared object file: No such file or directory
解决方法:ln -s /usr/local/BerkeleyDB.5.3/lib/libdb-5.3.so /usr/lib/libdb-5.3.so
注:在64位操作系统中,需执行
ln -s /usr/local/BerkeleyDB.5.3/lib/libdb-5.3.so /usr/lib64/libdb-5.3.so

使用memcacheq做异步队列,做个简单的生产者-消费者模型。生产者将数据写入mq中,消费者异步去队列中去取数据,进而进一步的消费处理数据。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #!/usr/bin/env python #-*- coding:utf8 -*-   import sys import time import random   import memcache   mc = memcache.Client([ "%s:%s" % ( "127.0.0.1" , "22202" )]) queue_name = "q1" def putter():    count = 0    while True :      data = "hello%d" % (count)      mc. set (queue_name, data)      print "put " , data      count + = 1      time.sleep(random.randint( 1 , 10 ))   def process_data(data):    print "processing data :" , data   def getter():    while True :      data = mc.get(queue_name)      if data:        process_data(data)      else :        print "no message, sleep for a while ..."        time.sleep( 30 )   if __name__ = = "__main__" :    if len (sys.argv) ! = 2 :      print "Wrong arg numbers"    else :      cmd = sys.argv[ 1 ]      if cmd = = "put" : putter()      elif cmd = = "get" : getter()      else : print "wrong cmd"

在使用时,开两个终端模拟两个进程,在一个终端中运行

python mqdemo.py put

来模拟生产者;另一个终端中运行

python mqdemo.py get

模拟消费者。

未经允许不得转载:搬瓦工中文网 » MemcacheQ详细安装教程与高效使用技巧分享