安裝Gearman

安裝Gearman

========================================================

向一個機器添加Gearman需要兩步:

1.構建並啓動這個守護進程(gearmand)

2.構建與php版本相匹配的PHP擴展。

========================================================


安裝gearmand

========================================================

我安裝版本是gearmand 1.1.12:

1. 首先安裝依賴包:

sudo yum -y install autoconf bison flex libtool libboost-all-dev libcurl4-openssl-dev curl libevent-dev uuid-dev libsqlite3-dev libmysqlclient-dev mysql-devel

2.下載Gearmand版本

在https://launchpad.net/gearmand下載最新版本

3.解壓、編譯、安裝源碼包

tar xvzf gearmand-1.1.12.tar.gz
cd gearmand-1.1.12
./configure
make
make install

注:這個過程中可能無法編譯成功,而且機率很大,我安裝的時候出現各種問題,但主要都是缺少共享庫,這時候要根據個人實際情況,看它報的是什麼錯,然後根據報錯,更新或者安裝共享庫。

sudo yum install ***等。

4.爲大多數最新的共享庫創建必須的鏈接和緩存。其中可能報:

error: gearman: error while loading shared libraries: libgearman.so.8: cannot open shared object file: No such file or directory

解決辦法:sudo ldconfig,,我安裝的時候就出現了這種情況,一開始不知道怎麼回事,百度了很久才找到這個方法。


安裝php 的gearman 擴展

========================================================

下載最新版本:

$ wget 
$ tar zxvf gearman-1.1.1.tgz
$ cd gearman-1.1.1/
$ phpize

$ ./configure --with-php-config=/usr/local/php/bin/php-config

$ make
$ make install

6. 在php.ini文件末尾添加"extension=gearman.so"

7. 檢測擴展是否安裝成功

$ php --info | grep "gearman support"
gearman support => enabled

顯示出:gearman support => enabled,就表示安裝成功啦。


測試

========================================================

1)sudo ldconfig

2)啓動gearmand: gearmand -d &

這一步可能會遇到:

啓動這個 agent,即 Gearman 守護程序:
/usr/local/sbin/gearmand --daemon
報錯: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
再次嘗試啓動:
/usr/local/sbin/gearmand --daemon
成功運行.查看進程:ps -ef | grep gearmand
root     19390     1  0 17:50 ?        00:00:00 gearmand --daemon
root     19403     1  0 17:54 ?        00:00:00 /usr/local/sbin/gearmand --daemon
root     19406  1556  0 17:54 pts/3    00:00:00 grep gearmand

3)查看gearmand是否在運行:ps aux | grep [g]earmand

4)檢查germand的任務檢測端口4730:sudo lsof -i tcp:4730


例子:從PHP使用Gearman

=====================================================

從 PHP 使用 Gearman 類似於之前的示例,惟一的區別在於這裏是在 PHP 內創建 producer 和 consumer。
每個 consumer 的工作均封裝在一個或多個 PHP 函數內。
先用 PHP 編寫的一個 Gearman worker。將這些代碼保存在一個名爲 worker.php 的文件中。
<?php
 $worker= new GearmanWorker();
 $worker->addServer();
 $worker->addFunction("title", "title_function");
 while ($worker->work());

 function title_function($job)
 {
   return ucwords(strtolower($job->workload()));
 }
?>


再用 PHP 編寫的一個 producer,或 client。將此代碼保存在一個名爲 client.php 的文件內。
<?php
 $client= new GearmanClient();
 $client->addServer();
 print $client->do("title", "All The World's a stage!");
 print "\n";
?>

現在,可以用如下的命令行連接客戶機與 worker 了:
php worker.php &
php client.php
結果:
All The World's a stage!

注意:安裝後# service php-fpm restart


發佈了4 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章