如何安装和使用Beanstalkd工作队列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何安装和使用Beanstalkd工作队列相关的知识,希望对你有一定的参考价值。

使用aptitude安装:

下载并安装Beanstalkd运行以下命令:

aptitude install -y beanstalkd
编辑默认配置文件让随着系统启动

vim /etc/default/beanstalkd
打开文件后,向下滚动并找到底部线#开始= yes。将其更改为:

START=yes
下面介绍源码安装

我们需要从源代码安装过程的一个关键工具- Git。

运行以下获取Git在你系统上:

aptitude install -y git
下载必要的开发工具软件包:

aptitude install -y build-essential
使用Git克隆(下载)官方库:

git clone https://github.com/kr/beanstalkd
进入到下载目录:

cd beanstalkd
从源代码构建应用程序:

make
安装:

make install
再介绍一下centos下源码安装:

下载地址:

wget http://cloud.github.com/downloads/kr/beanstalkd/beanstalkd-1.4.6.tar.gz

解压:

tar xzf beanstalkd-1.4.6.tar.gz

cd beanstalkd-1.4.6

/configure

make

make install

默认安装路径 :/usr/local/bin/

查看版本:

/usr/local/bin/beanstalkd -v

1.4.6
再附加一个启动脚本,从Fedora下挖来的 startup 脚本:

#!/bin/sh
#
# beanstalkd - a simple, fast workqueue service
#
# chkconfig: - 57 47
# description: a simple, fast workqueue service
# processname: beanstalkd
# config: /etc/sysconfig/beanstalkd
#

### BEGIN INIT INFO
# Provides: beanstalkd
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Stop: 0 1 2 6
# Short-Description: start and stop beanstalkd
# Description: a simple, fast work-queue service
### END INIT INFO

# Source function library.
/etc/rc.d/init.d/functions

# Source networking configuration.
/etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit
exec="/usr/local/bin/beanstalkd"
prog=$(basename $exec)

# default options, overruled by items in sysconfig
BEANSTALKD_ADDR=127.0.0.1
BEANSTALKD_PORT=11300
BEANSTALKD_USER=beanstalkd

[ -e /etc/sysconfig/beanstalkd ] && . /etc/sysconfig/beanstalkd

lockfile=/var/lock/subsys/beanstalkd

start()
[ -x $exec ] || exit 5
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
options="-l $BEANSTALKD_ADDR -p $BEANSTALKD_PORT -u $BEANSTALKD_USER"
if [ "$BEANSTALKD_MAX_JOB_SIZE" != "" ]; then
options="$options -z $BEANSTALKD_MAX_JOB_SIZE"
fi

if [ "$BEANSTALKD_BINLOG_DIR" != "" ]; then
if [ ! -d "$BEANSTALKD_BINLOG_DIR" ]; then
echo "Creating binlog directory ($BEANSTALKD_BINLOG_DIR)"
mkdir -p $BEANSTALKD_BINLOG_DIR && chown $BEANSTALKD_USER:$BEANSTALKD_USER $BEANSTALKD_BINLOG_DIR
fi
options="$options -b $BEANSTALKD_BINLOG_DIR"
if [ "$BEANSTALKD_BINLOG_FSYNC_PERIOD" != "" ]; then
options="$options -f $BEANSTALKD_BINLOG_FSYNC_PERIOD"
else
options="$options -F"
fi
if [ "$BEANSTALKD_BINLOG_SIZE" != "" ]; then
options="$options -s $BEANSTALKD_BINLOG_SIZE"
fi
fi

daemon $exec -d $options
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval


stop()
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog -INT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval


restart()
stop
start


reload()
restart


force_reload()
restart


rh_status()
# run checks to determine if the service is running or use generic status
status $prog


rh_status_q()
rh_status >/dev/null 2>&1


case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 start|stop|status|restart|condrestart|try-restart|reload|force-reload"
exit 2
esac
exit $?
使用Beanstalkd

在安装之后,您就可以开始使用Beanstalkd服务器。以下是运行守护进程的选项:

-b DIR wal directory
-f MS fsync at most once every MS milliseconds (use -f0 for "always fsync")
-F never fsync (default)
-l ADDR listen on address (default is 0.0.0.0)
-p PORT listen on port (default is 11300)
-u USER become user and group
-z BYTES set the maximum job size in bytes (default is 65535)
-s BYTES set the size of each wal file (default is 10485760)
(will be rounded up to a multiple of 512 bytes)
-c compact the binlog (default)
-n do not compact the binlog
-v show version information
-V increase verbosity
-h show this help
使用例子:

# Usage: beanstalkd -l [ip address] -p [port #]
# For local only access:
beanstalkd -l 127.0.0.1 -p 11301 &
管理服务:

如果安装包管理器(i.e. aptitude),你将能够管理Beanstalkd作为服务守护进程。

# To start the service:
service beanstalkd start

# To stop the service:
service beanstalkd stop

# To restart the service:
service beanstalkd restart

# To check the status:
service beanstalkd status
获得Beanstalkd客户端库

Beanstalkd配有一长串的支持客户端库来处理许多不同的应用程序部署。这个列表的支持语言和框架,包括:

●Python

●Django

●Go

●Java

●Node.js

●Perl

php

●Ruby

●and more.

查看完整列表支持,寻找你最喜欢的语言和安装说明,查看客户端库页面Beanstalkd Github上。

使用Beanstalkd

在本节之前,完成这篇文章,让我们快速Beanstalkd的基本用法。在我们的示例中,我们将使用Python语言和Beanstald Python bindings ——beanstalkc。

安装beanstalkc,运行以下命令:

pip install pyyaml
pip install beanstalkc
基本操作

在所有Python文件你想处理Beanstalkd时,需要导入beanstalkc并连接:

import beanstalkc

# Connection
beanstalk = beanstalkc.Connection(host=\'localhost\', port=11301)
To enqueue a job:

beanstalk.put(\'job_one\')
To receive a job:

job = beanstalk.reserve()
# job.body == \'job_one\'
To delete a job after processing it:

job.delete()
To use a specific tube (i.e. queue / list):

beanstalk.use(\'tube_a\')
To list all available tubes:

beanstalk.tubes()
# [\'default\', \'tube_a\']
Final example (nano btc_ex.py):

import beanstalkc

# Connect
beanstalk = beanstalkc.Connection(host=\'localhost\', port=11301)

# See all tubes:
beanstalk.tubes()

# Switch to the default (tube):
beanstalk.use(\'default\')

# To enqueue a job:
beanstalk.put(\'job_one\')

# To receive a job:
job = beanstalk.reserve()

# Work with the job:
print job.body

# Delete the job:
job.delete()
当您运行上面的脚本时,您应该会看到工作的主体被打印:

python btc_ex.py
# job_one
参考技术A 下载并安装Beanstalkd运行以下命令:

aptitude install -y beanstalkd
编辑默认配置文件让随着系统启动

vim /etc/default/beanstalkd
打开文件后,向下滚动并找到底部线#开始= yes。将其更改为:

START=yes
下面介绍源码安装

我们需要从源代码安装过程的一个关键工具- Git。

运行以下获取Git在你系统上:

aptitude install -y git
下载必要的开发工具软件包:

aptitude install -y build-essential
使用Git克隆(下载)官方库:

git clone https://github.com/kr/beanstalkd
进入到下载目录:

cd beanstalkd
从源代码构建应用程序:

make
安装:

make install
再介绍一下centos下源码安装:

下载地址:

wget http://cloud.github.com/downloads/kr/beanstalkd/beanstalkd-1.4.6.tar.gz

解压:

tar xzf beanstalkd-1.4.6.tar.gz

cd beanstalkd-1.4.6

/configure

make

make install

默认安装路径 :/usr/local/bin/

查看版本:

/usr/local/bin/beanstalkd -v

1.4.6
再附加一个启动脚本,从Fedora下挖来的 startup 脚本:

#!/bin/sh
#
# beanstalkd - a simple, fast workqueue service
#
# chkconfig: - 57 47
# description: a simple, fast workqueue service
# processname: beanstalkd
# config: /etc/sysconfig/beanstalkd
#

### BEGIN INIT INFO
# Provides: beanstalkd
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Stop: 0 1 2 6
# Short-Description: start and stop beanstalkd
# Description: a simple, fast work-queue service
### END INIT INFO

# Source function library.
/etc/rc.d/init.d/functions

# Source networking configuration.
/etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit
exec="/usr/local/bin/beanstalkd"
prog=$(basename $exec)

# default options, overruled by items in sysconfig
BEANSTALKD_ADDR=127.0.0.1
BEANSTALKD_PORT=11300
BEANSTALKD_USER=beanstalkd

[ -e /etc/sysconfig/beanstalkd ] && . /etc/sysconfig/beanstalkd

lockfile=/var/lock/subsys/beanstalkd

start()
[ -x $exec ] || exit 5
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
options="-l $BEANSTALKD_ADDR -p $BEANSTALKD_PORT -u $BEANSTALKD_USER"
if [ "$BEANSTALKD_MAX_JOB_SIZE" != "" ]; then
options="$options -z $BEANSTALKD_MAX_JOB_SIZE"
fi

if [ "$BEANSTALKD_BINLOG_DIR" != "" ]; then
if [ ! -d "$BEANSTALKD_BINLOG_DIR" ]; then
echo "Creating binlog directory ($BEANSTALKD_BINLOG_DIR)"
mkdir -p $BEANSTALKD_BINLOG_DIR && chown $BEANSTALKD_USER:$BEANSTALKD_USER $BEANSTALKD_BINLOG_DIR
fi
options="$options -b $BEANSTALKD_BINLOG_DIR"
if [ "$BEANSTALKD_BINLOG_FSYNC_PERIOD" != "" ]; then
options="$options -f $BEANSTALKD_BINLOG_FSYNC_PERIOD"
else
options="$options -F"
fi
if [ "$BEANSTALKD_BINLOG_SIZE" != "" ]; then
options="$options -s $BEANSTALKD_BINLOG_SIZE"
fi
fi

daemon $exec -d $options
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval


stop()
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog -INT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval


restart()
stop
start


reload()
restart


force_reload()
restart


rh_status()
# run checks to determine if the service is running or use generic status
status $prog


rh_status_q()
rh_status >/dev/null 2>&1


case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 start|stop|status|restart|condrestart|try-restart|reload|force-reload"
exit 2
esac
exit $?
使用Beanstalkd

在安装之后,您就可以开始使用Beanstalkd服务器。以下是运行守护进程的选项:

-b DIR wal directory
-f MS fsync at most once every MS milliseconds (use -f0 for "always fsync")
-F never fsync (default)
-l ADDR listen on address (default is 0.0.0.0)
-p PORT listen on port (default is 11300)
-u USER become user and group
-z BYTES set the maximum job size in bytes (default is 65535)
-s BYTES set the size of each wal file (default is 10485760)
(will be rounded up to a multiple of 512 bytes)
-c compact the binlog (default)
-n do not compact the binlog
-v show version information
-V increase verbosity
-h show this help
使用例子:

# Usage: beanstalkd -l [ip address] -p [port #]
# For local only access:
beanstalkd -l 127.0.0.1 -p 11301 &
管理服务:

如果安装包管理器(i.e. aptitude),你将能够管理Beanstalkd作为服务守护进程。

# To start the service:
service beanstalkd start

# To stop the service:
service beanstalkd stop

# To restart the service:
service beanstalkd restart

# To check the status:
service beanstalkd status
获得Beanstalkd客户端库

Beanstalkd配有一长串的支持客户端库来处理许多不同的应用程序部署。这个列表的支持语言和框架,包括:

●Python

●Django

●Go

●Java

●Node.js

●Perl

●PHP

●Ruby

●and more.

查看完整列表支持,寻找你最喜欢的语言和安装说明,查看客户端库页面Beanstalkd Github上。

使用Beanstalkd

在本节之前,完成这篇文章,让我们快速Beanstalkd的基本用法。在我们的示例中,我们将使用Python语言和Beanstald Python bindings ——beanstalkc。

安装beanstalkc,运行以下命令:

pip install pyyaml
pip install beanstalkc
基本操作

在所有Python文件你想处理Beanstalkd时,需要导入beanstalkc并连接:

import beanstalkc

# Connection
beanstalk = beanstalkc.Connection(host='localhost', port=11301)
To enqueue a job:

beanstalk.put('job_one')
To receive a job:

job = beanstalk.reserve()
# job.body == 'job_one'
To delete a job after processing it:

job.delete()
To use a specific tube (i.e. queue / list):

beanstalk.use('tube_a')
To list all available tubes:

beanstalk.tubes()
# ['default', 'tube_a']
Final example (nano btc_ex.py):

import beanstalkc

# Connect
beanstalk = beanstalkc.Connection(host='localhost', port=11301)

# See all tubes:
beanstalk.tubes()

# Switch to the default (tube):
beanstalk.use('default')

# To enqueue a job:
beanstalk.put('job_one')

# To receive a job:
job = beanstalk.reserve()

# Work with the job:
print job.body

# Delete the job:
job.delete()
当您运行上面的脚本时,您应该会看到工作的主体被打印:

python btc_ex.py
# job_one

以上是关于如何安装和使用Beanstalkd工作队列的主要内容,如果未能解决你的问题,请参考以下文章

beanstalkd任务队列 linux平台安装测试

如何使用 PHP 设置 Beanstalkd

Laravel 队列与 beanstalkd 和 redis 重复

Beanstalkd 消息队列

Beanstalkd 在伪造队列工作人员上同时运行队列中的所有作业

ini Beanstalkd队列工作者