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

Posted coder_up

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gearman 异步任务软件安装与实例相关的知识,希望对你有一定的参考价值。

安装依赖

Install Gearman (ubuntu)

sudo apt install gperf
sudo apt install libboost-program-options-dev
sudo apt install libevent-dev
sudo apt install uuid-dev

Install Gearman (centos)

sudo yum install gperf       // Hash 函数库
sudo yum install boost-devel
sudo yum install libevent libevent-devel
sudo yum install libuuid-devel

手动编译安装 boost (如果rpm包安装成功就不用这个了)

1.安装boost
如果不安装boost的话,是安装不上gearmand和gearman的.
wget -c http://download.slogra.com/gearman/boost_1_50_0.tar.gz
tar zxf boost_1_50_0.tar.gz && cd boost_1_50_0
sudo /bin/bash ./bootstrap.sh --prefix=/usr/local/boost
./b2 install

如没有问题,在/etc/profile里的最下方加入:
export CPPFLAGS=-I/usr/local/boost/include
export LDFLAGS=-L/usr/local/boost/lib
保存退出后运行
source /etc/profile
好了,boost就安装成功了.

编译GearmanD

wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz
tar zxvf gearmand…
cd gearmand …
//如果手动安装的boost那么就执行这句,否则执行下一句
sudo ./configure --prefix=/usr/local/gearmand --with-boost=/usr/local/boost
sudo ./configure --prefix=/usr/local/gearmand
sudo make && make install

Install php gearman ext

编译安装

wget http://pecl.php.net/get/gearman-1.1.2.tgz
tar zxvf gearman-1.
cd gearman-1 …
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

error1: configure: error: Please install libgearman

我们在安装gearman的时候,已经有libgearman了,但是在编译扩展的时候,却用额外安装的lib,在运行的时候,难免出现性能方面的问题

./configure --prefix=/opt --with-php-config=/usr/local/php/bin/php-config --with-gearman=/usr/local/gearmand

启动 Gearman Job Server

/usr/local/gearman/sbin/gearmand -d
nohup php myworker.php >/dev/null 2>&1 &

可能会出现如下错误:


Could not open log file "/usr/local/var/log/gearmand.log", from "/usr/sbin",
switching to stderr. (No such file or directory)

解决:

  mkdir -p /usr/local/var/log/
  cd /usr/local/var/log/
  touch gearmand.log

例子

client

<?php
  // producer
  $client= new GearmanClient();
  //$client->addServers('192.168.1.131:4730,192.168.2.249:4730,192.168.2.250:4730');
  $client->addServer('192.168.1.131',4730);
  print $client->do("title", "AlL THE World's a sTagE");
  print "\\n";

server

<?php
  $worker= new GearmanWorker();
  $worker->addServers('192.168.1.131:4730,192.168.2.249:4730,192.168.2.250:4730');
  $worker->addServer('192.168.1.131',4730);
  $worker->addFunction("title", "title_function");
  while ($worker->work());

   //consumer
  function title_function($job)
  
    return ucwords(strtolower($job->workload()));
  

以上是关于Gearman 异步任务软件安装与实例的主要内容,如果未能解决你的问题,请参考以下文章

Gearman 异步任务软件的介绍

php异步任务处理: gearman

Gearman使用示例

Gearman 分布式的异步任务分发框架

gearman(异步计算)学习

gearman中任务的优先级和返回状态