科技常识:MemcacheQ安装及使用方法

2021-04-06 04:31:14
导读今天小编跟大家讲解下有关科技常识:MemcacheQ安装及使用方法,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关科技常识:Memca

今天小编跟大家讲解下有关科技常识:MemcacheQ安装及使用方法,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关科技常识:MemcacheQ安装及使用方法的相关资料,希望小伙伴会喜欢也能够帮助大家。

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

$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 libeventlibevent-devel-2.0.10-2.fc15.x86_64libevent-2.0.10-2.fc15.x86_64libevent-2.0.10-2.fc15.i686

或者:

ls -al /usr/lib |grep libevent

如果还没有安装:

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

$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进目录

./configure --enable-threadsmakemake 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目录,否则不能启动

命令行使用memcacheQtelnet 127.0.0.1 22202Trying 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\nSTORED\r\n取出队列:get <queue name>\r\nVALUE <queue name> <flags> <message_len>\r\n<your message body will come here>\r\nEND\r\n与memcache协议基本一致,只是把key name换成queue name,而且在set的命令中,忽略了expire_time的参数。mq的数据存储是存在berkeleyDB中,做了持久化存储,没有内存的过期时间。示例:telnet 127.0.0.1 22202Trying 127.0.0.1…Connected to 127.0.0.1.Escape character is ‘^]'.set q4 0 0 5helloSTOREDset q4 0 0 5worldSTOREDstats queueSTAT q4 2/0ENDget q4VALUE q4 0 5helloENDstats queueSTAT q4 2/1END

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

1.编译出现错误:checking for library containing db_create... noconfigure: 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中,消费者异步去队列中去取数据,进而进一步的消费处理数据。

#!/usr/bin/env python#-*- coding:utf8 -*- import sysimport timeimport 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

模拟消费者。

来源:爱蒂网

免责声明:本文由用户上传,如有侵权请联系删除!

猜你喜欢

最新文章