gearman的安装使用

Posted clubs

tags:

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

yum 安装(推荐)

1.安装gearmand

[root@iZbp12y6fwj9mup08bgko6Z ~]# mkdir /usr/local/gearmand
[root@iZbp12y6fwj9mup08bgko6Z ~]# cd /usr/local/gearmand/
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# sudo yum install gearmand

2.安装php扩展

[root@iZbp12y6fwj9mup08bgko6Z gearmand]# sudo yum install php-pecl-gearman

3.查看扩展是否安装成功

[root@iZbp12y6fwj9mup08bgko6Z gearmand]# php -m | grep gearman
gearman

源码安装(配置php没有成功)

1.安装gearmand

[root@iZbp12y6fwj9mup08bgko6Z ~]# mdkir /usr/local/gearmand
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# tar xvzf gearmand-1.1.12.tar.gz
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# cd gearmand-1.1.12
[root@iZbp12y6fwj9mup08bgko6Z gearmand-1.1.12]# ./configure (此步可能会报错,参考下面解决方法)
[root@iZbp12y6fwj9mup08bgko6Z gearmand-1.1.12]# make
[root@iZbp12y6fwj9mup08bgko6Z gearmand-1.1.12]# make install

执行./configure可能会有报错could not find boost,别慌,按下面方法安装相关依赖后重新执行./configure编译

checking for boostlib >= 1.39... configure: We could not detect the boost libraries (version 1.39 or higher). If you have a staged boost library (still not installed) please specify $BOOST_ROOT in your environment and do not give a PATH to --with-boost option.  If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.
configure: error: could not find boost

解决方法
yum install -y boost boost-devel
yum install gcc-c++.x86_64 gperf
yum -y install libevent libevent-devel

2.安装扩展

[root@iZbp12y6fwj9mup08bgko6Z gearmand]# wget http://pecl.php.net/get/gearman-1.1.2.tgz
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# tar xvzf gearman-1.1.2.tgz
[root@iZbp12y6fwj9mup08bgko6Z gearmand]# cd gearman-1.1.2
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# whereis phpize
phpize: /usr/local/php/bin/phpize
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# /usr/local/php/bin/phpize (此步可能会报错,参考下面解决方法,解决后重新执行phpize)
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# ./configure
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# make (此步可能会报错,参考下面解决方法,解决后重新make)
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]#

执行 phpize 报错

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# /usr/local/php/bin/phpize
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决方法,安装 autoconf 依赖,然后重新执行phpize
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# yum install autoconf

执行make报错

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# make

......
make: *** [php_gearman.lo] Error 1

解决方法
将"php_gearman.loT" 拷贝一份命名为 "php_gearman.lo",然后再make
[root@iZbp12y6fwj9mup08bgko6Z ~]# find / -name php_gearman.loT
/usr/local/gearmand/gearman-1.1.2/php_gearman.loT
[root@iZbp12y6fwj9mup08bgko6Z ~]# cp /usr/local/gearmand/gearman-1.1.2/php_gearman.loT /usr/local/gearmand/gearman-1.1.2/php_gearman.lo
[root@iZbp12y6fwj9mup08bgko6Z ~]# find / -name php_gearman.lo
/usr/local/gearmand/gearman-1.1.2/php_gearman.lo

3.手动添加配置(修改php.ini 添加extension = gearman.so)

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# find / -name php.ini
/usr/local/php/etc/php.ini
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# vim /usr/local/php/etc/php.ini

...
extension = gearman.so

或者

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# sudo echo "extension=gearman.so" >> /usr/local/php/etc/php.ini

或者

修改php.ini,以下加入语句加入:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/gearman.so

4.重启php

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# service php-fpm stop
[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# service php-fpm start

 

使用
相关代码:
worker.php

<?php
    $worker= new GearmanWorker();
    $worker->addServer("127.0.0.1", 4730);  
    $worker->addFunction("title", "title_function");
    while ($worker->work());

    function title_function($job){
        return "你请求的数据:" . $job->workload() . " 请求时间:" . date(‘Y-m-d H:i:s‘);
        //return ucwords(strtolower($job->workload()));
    }
?>

client.php

<?php
    $client= new GearmanClient();
    $client->addServer("127.0.0.1", 4730);
    print $client->do("title", json_encode(
        [
            ‘username‘=>‘jack‘,
            ‘email‘=>‘jack@foxmail.com‘,
        ]
    ));
    print "
";
?>

 

测试:
1.启动服务

sudo gearmand -d

源码安装启动服务可能会报缺失日志文件

[root@iZbp12y6fwj9mup08bgko6Z gearman-1.1.2]# gearmand -d (报错)
gearmand: Could not open log file "/usr/local/var/log/gearmand.log", from "/usr/local/gearmand/gearman-1.1.2", switching to stderr. (No such file or directory)

在提示的目录下创建好/log/gearmand.log文件,再次执行上述命令,就正常启动了job server。

 

2.启动worker

php worker.php

3.启动client

[root@iZbp12y6fwj9mup08bgko6Z gearmand]# php client.php 
你请求的数据:{"username":"jack","email":"jack@foxmail.com"} 请求时间:2020-07-15 13:55:43

 

参考:https://www.jianshu.com/p/c7a55dee5796

报错参考解决:https://blog.csdn.net/huangjinvv/article/details/8018664

以上是关于gearman的安装使用的主要内容,如果未能解决你的问题,请参考以下文章

安装 gearman

mac下快速安装gearman和php扩展

分布式调用gearman安装

linuxcentos6.9安装gearman

[Linux]使用PHP编写Gearman的Worker守护进程

Gearman 异步任务软件安装与实例