CentOS编译和yum安装Beanstalkd及service和systemctl管理

Beanstalkd基本概念请移步Beanstalkd消息/任务队列

1.yum安装Beanstalkd

设置yum安装源

#CentOS6.x
rpm -Uvh https://download.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
#CentOS7.x
rpm -Uvh https://download.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

查看Beanstalkd版本

[root@jmsiteos7 ~]# yum list beanstalkd
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * epel: mirrors.yun-idc.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.huaweicloud.com
 * webtatic: us-east.repo.webtatic.com
已安装的软件包
beanstalkd.x86_64                                                                             1.10-2.el7                                                                              @epe

安装beanstalkd

[root@jmsiteos7 ~]# yum install beanstalkd
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * epel: mirrors.yun-idc.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.huaweicloud.com
 * webtatic: us-east.repo.webtatic.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 beanstalkd.x86_64.0.1.10-2.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================================================================================================
 Package                                        架构                                       版本                                             源                                        大小
===========================================================================================================================================================================================
正在安装:
 beanstalkd                                     x86_64                                     1.10-2.el7                                       epel                                      48 k

事务概要
===========================================================================================================================================================================================
安装  1 软件包

总下载量:48 k
安装大小:94 k
Is this ok [y/d/N]: y
Downloading packages:
beanstalkd-1.10-2.el7.x86_64.rpm                                                                                                                                    |  48 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : beanstalkd-1.10-2.el7.x86_64                                                                                                                                           1/1 
  验证中      : beanstalkd-1.10-2.el7.x86_64                                                                                                                                           1/1 

已安装:
  beanstalkd.x86_64 0:1.10-2.el7                                                                                                                                                           

完毕!
[root@jmsiteos7 ~]# 

CentOS6.x启动并查看状态

[root@localhost ~]# service beanstalkd start
正在启动 beanstalkd:                                      [确定]
[root@localhost ~]# service beanstalkd status
beanstalkd (pid 19189) 正在运行...

CentOS7.x启动并查看状态

[root@jmsiteos7 ~]# systemctl start beanstalkd
[root@jmsiteos7 ~]# systemctl status beanstalkd
● beanstalkd.service - Beanstalkd Fast Workqueue Service
   Loaded: loaded (/usr/lib/systemd/system/beanstalkd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-20 19:24:38 CST; 5s ago
 Main PID: 8259 (beanstalkd)
   CGroup: /system.slice/beanstalkd.service
           └─8259 /usr/bin/beanstalkd -l 0.0.0.0 -p 11300 -u beanstalkd

1月 20 19:24:38 jmsiteos7 systemd[1]: Started Beanstalkd Fast Workqueue Service.
[root@jmsiteos7 ~]#

2.编译安装Beanstalkd

安装依赖

yum -y  install gcc gcc-c++ wget

创建源码存放目录

mkdir -p /usr/local/src
cd /usr/local/src

下载,解压

wget -c https://github.com/kr/beanstalkd/archive/v1.10.tar.gz
tar -zxvf v1.10.tar.gz
cd beanstalkd-1.10/

编译,安装

make
make install PREFIX=/usr/local/beanstalkd

查看版本

[root@localhost beanstalkd-1.10]# /usr/local/beanstalkd/bin/beanstalkd -v
beanstalkd 1.10

命令行参数

参数 含义
-b DIR 指定binlog目录开启binlog,重启后会自动恢复任务。
-f MS 每MS毫秒fsync
-F 从不fsync(默认)
-l ADDR 监听IP地址(默认为0.0.0.0)
-p PORT 监听端口号(默认为11300)
-u USER 以USER用户和组运行
-z BYTES 设置每个job最大字节数(默认值为65535)
-s BYTES 设置每个binlog文件最大字节数(默认为10485760) (将四舍五入为512字节的倍数)
-c 压缩binlog(默认)
-n 不要压缩binlog
-v 显示版本信息
-V 增加冗余长度
-h 显示帮助信息

3.创建beanstalkd用户和binlog目录

groupadd beanstalkd
useradd -s /sbin/nologin -g beanstalkd beanstalkd
mkdir -p /var/lib/beanstalkd/binlog
chown -R beanstalkd:beanstalkd /var/lib/beanstalkd/binlog

4.CentOS6.x加入服务设置开机启动

安装守护进程

cd /usr/local/src
#下载
wget -c https://github.com/bmc/daemonize/archive/release-1.7.8.tar.gz
#解压
tar -zxvf release-1.7.8.tar.gz
cd daemonize-release-1.7.8/
#编译,安装
sh configure
make
make install

创建配置文件

vim /etc/sysconfig/beanstalkd

输入如下配置

# System configuration for the beanstalkd daemon

# Available options correspond to the options to the
# beanstalkd commandline.

BEANSTALKD_ADDR=0.0.0.0
BEANSTALKD_PORT=11300
BEANSTALKD_USER=beanstalkd

# Job size is left to the default. Uncomment and set it
# to a value to have it take affect.
#BEANSTALKD_MAX_JOB_SIZE=65535

# Using the binlog is off by default.
#
# The direcory to house the binlog.
BEANSTALKD_BINLOG_DIR=/var/lib/beanstalkd/binlog
#
# fsync the binlog at most once every N milliseconds.
# setting this to 0 means 'always fsync'. If this is unset,
# and the binlog is used, then no explicit fsync is ever
# performed.  That is, the -F option is used.
#BEANSTALKD_BINLOG_FSYNC_PERIOD=
#
# The size of each binlog file.  This is rounded
# up to the nearest 512 byte boundary.
#BEANSTALKD_BINLOG_SIZE=10485760

创建服务脚本

vim /etc/init.d/beanstalkd

输入如下代码

#!/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/beanstalkd/bin/beanstalkd"
prog=$(basename $exec)

# default options, overruled by items in sysconfig
BEANSTALKD_ADDR=0.0.0.0
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}"
    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 /usr/local/sbin/daemonize -u ${BEANSTALKD_USER} $exec $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 $?

加入服务,设置开机启动

#设置权限
chmod -R 755 /etc/init.d/beanstalkd
#加入服务
chkconfig --add beanstalkd
#设置开机启动
chkconfig beanstalkd on
#启动
service beanstalkd start
#查看运行状态
service beanstalkd status
#更多命令
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}

5.CentOS7.x加入服务设置开机启动

创建配置文件

vim /etc/sysconfig/beanstalkd

输入如下配置

# System configuration for the beanstalkd daemon

#
# beanstalkd commandline options
#

ADDR=-l 0.0.0.0
PORT=-p 11300
USER=-u beanstalkd

# Job size is left to the default. Uncomment and set it
# to a value to have it take affect.
#MAX_JOB_SIZE=-z 65535

# Using the binlog is off by default.
#
# The direcory to house the binlog.
BINLOG_DIR=-b /var/lib/beanstalkd/binlog
#
# fsync the binlog at most once every N milliseconds.
# setting this to 0 means 'always fsync'. If this is unset,
# and the binlog is used, then no explicit fsync is ever
# performed.  That is, the -F option is used.
#BINLOG_FSYNC_PERIOD=-f some-value
#
# The size of each binlog file.  This is rounded
# up to the nearest 512 byte boundary.
#BINLOG_SIZE=-s 10485760

创建服务脚本

vim /usr/lib/systemd/system/beanstalkd.service

输入如下代码

[Unit]
Description=Beanstalkd Fast Workqueue Service
After=network.target

[Service]
User=beanstalkd
Type=simple
EnvironmentFile=-/etc/sysconfig/beanstalkd
ExecStart=/usr/local/beanstalkd/bin/beanstalkd $ADDR $PORT $USER $MAX_JOB_SIZE $BINLOG_DIR $BINLOG_SIZE $BINLOG_FSYNC_PERIOD
Restart=always

[Install]
WantedBy=multi-user.target

重新载入 systemd

systemctl daemon-reload

设置开机启动

systemctl enable beanstalkd.service

启动beanstalkd

[root@jmsiteos7 beanstalkd-1.10]# systemctl start beanstalkd
[root@jmsiteos7 beanstalkd-1.10]# systemctl status beanstalkd
● beanstalkd.service - Beanstalkd Fast Workqueue Service
   Loaded: loaded (/usr/lib/systemd/system/beanstalkd.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-20 21:46:37 CST; 7s ago
 Main PID: 8793 (beanstalkd)
   CGroup: /system.slice/beanstalkd.service
           └─8793 /usr/local/beanstalkd/bin/beanstalkd -l 0.0.0.0 -p 11300 -u beanstalkd -b /var/lib/beanstalkd/binlog

1月 20 21:46:37 jmsiteos7 systemd[1]: Started Beanstalkd Fast Workqueue Service.
[root@jmsiteos7 beanstalkd-1.10]# 

原文地址:https://www.jmsite.cn/blog-543.html

上一篇:ECS主动运维2.0,体验升级,事半功倍


下一篇:PHP使用Beanstalkd实例