一般,Gearman被用來分發任務,以便實現異步操做。下面捋捋如何管理Gearman。php
說明:請自行安裝好Gearman和PHP PECL Gearman。html
(我以前安裝的gearman php的c語言擴展,gearman-monitor和gearman-manager都須要git
Net_Gearman(也就是 github
A pure PHP API that can be found as Net_Gearman on PEAR.web
)數據庫
咱們先來建立一個Worker,實現一個簡單的顯示功能:json
<?php $worker= new GearmanWorker(); $worker->addServer('127.0.0.1', '4730'); $worker->addFunction('echo', 'my_echo_function'); while ($worker->work()); function my_echo_function($job) { return $job->workload(); } ?>
而後咱們運行它:數組
shell> php /path/to/worker/file
可能你已經注意到,代碼裏有一個死循環,是否是須要Sleep一下?讓咱們監測看看:bash
shell> strace -r php /path/to/worker/file 0.000099 poll([{fd=3, events=POLLIN}], 1, 10000) = 0 (Timeout) 10.006522 write(3, "\0REQ\0\0\0\t\0\0\0\0", 12) = 12
可見PHP PECL Gearman內部已經作了休息十秒的設置,咱們就不用杞人憂天了。
接下來咱們以Shell爲Client來調用一下:
shell> gearman -f echo "hello, world."
到這裏,準備工做基本就齊活兒了,相信你們已經對Gearman有了一個初步的認識。
出於效率的考慮,咱們每每會啓動不少個Worker,但具體應該啓動多少個呢?十個仍是一百個?少了不夠,多了浪費,到底應該如何度量呢?
其實Gearman自己已經提供了相應的命令供咱們查看狀態:
shell> (echo status; sleep 0.1) | nc 127.0.0.1 4730
命令的結果會分爲四列,它們的含義從左到右依次是:
從這些信息能夠推斷出:若是系統比較繁忙的話,Number of jobs running的數值會接近Number of capable workers;Number in queue可能會大於Number of capable workers。此時咱們應該增長Worker的數量,反之則應該考慮減小Worker的數量。
另外,推薦你們結合使用watch命令來監控Gearman的狀態,這樣更直觀一些:
shell> watch -n 1 "(echo status; sleep 0.1) | nc 127.0.0.1 4730"
實際應用中,還有不少特殊狀況須要考慮,好比說:程序代碼更新後,如何避免手動重啓Worker?還須要注意的是Worker長時間運行,一旦意外中斷或者內存泄漏怎麼辦?一般這類進程控制問題用Supervisor均可以輕鬆搞定,有興趣的讀者本身看看吧。此外網絡上還有一些不錯的工具能夠玩玩,好比:GearmanManager,Gearman-Monitor。
須要當心的一件事情是數據的共享。Gearman 不進行所交換數據的任何轉換或操做。對於這裏使用的簡單字符串和整數沒有問題,可是不能共享 PHP 中的數組值並指望能在 Java 語言中被理解。對於這種類型的交互,可使用不少結構化數據標準中的一種,好比 JavaScript Object Notation (JSON) 或 XML。另外,若是您在處理來自數據庫的信息,只要共享 ID 或者找到須要處理的數據時要用到的信息便可,或者使用 memcached 這樣的透明方法(儘管可能仍然須要 JSON 或等價物)。
轉自:http://huoding.com/2012/10/30/196
http://blog.chinaunix.net/uid-20357359-id-1963682.html
GearmanMonitor 是用來查看 Gearman 服務狀態的工具,包括 運行中/過的隊列 Queue,運行中的全部 workers,及服務器 servers。GearmanMonitor 須要有 Net_Gearman 支持。安裝 pear 後,直接執行:
咱們首先安裝NET_geraman(也就是php寫的gearman庫)。
https://github.com/brianlmoon/net_gearman
Installation 1. Install PEAR if it is not already installed on your system. 2. Use git to pull this repo to your servers. Be sure where you install it is in the autoload or include path before the rest of PEAR.
Linux上安裝php的pear
在搭建centreon的過程當中,須要pear模塊支持。
什麼是pear
pear是PHP擴展與應用庫(the PHP Extension and Application Repository)的縮寫。它是一個PHP擴展及應用的一個代碼倉庫,簡單地說,pear就是PHP的cpan。
在官網上有說明詳細的安裝信息,這裏做簡單說明。
http://pear.php.net/manual/en/about-pear.php
個人PHP目錄爲/usr/local/php5
在Linux下安裝PHP的PEAR:
1)下載
#curl -o go-pear.php http://pear.php.net/go-pear
若是提示:
PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) ‘json.so’ in Unknown on line 0
Sorry! Your PHP version is too new (5.2.9) for this go-pear.
Instead use http://pear.php.net/go-pear.phar for a more stable and current
version of go-pear, more suited to your PHP version.
那麼要從http://pear.php.net/go-pear.phar獲取。
#curl -o go-pear.php http://pear.php.net/go-pear.phar
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3594k 100 3594k 0 0 186k 0 0:00:19 0:00:19 –:–:– 196k
會在當前目錄下載go-pear.php 頁面。
2)運行go-pear.php
# /usr/local/php5/bin/php go-pear.php
3)這裏按回車繼續安裝,CTRL+C放棄安裝。
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type ‘all’ to change all of them or simply press Enter to
accept these locations.
1. Installation base ($prefix) : /usr/local/php5
2. Temporary directory for processing : /tmp/pear/install
3. Temporary directory for downloads : /tmp/pear/install
4. Binaries directory : /usr/local/php5/bin
5. PHP code directory ($php_dir) : /usr/local/php5/lib/php
6. Documentation directory : /usr/local/php5/docs
7. Data directory : /usr/local/php5/data
8. User-modifiable configuration files directory : /usr/local/php5/cfg
9. Public Web Files directory : /usr/local/php5/www
10. Tests directory : /usr/local/php5/tests
11. Name of configuration file : /usr/local/php5/etc/pear.conf
1-11, ‘all’ or Enter to continue:
Beginning install…
Configuration written to /usr/local/php5/etc/pear.conf…
Initialized registry…
Preparing to install…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.3.7.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Console_Getopt-1.3.0.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/PEAR-1.9.4.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/Structures_Graph-1.0.4.tar…
installing phar://go-pear.phar/PEAR/go-pear-tarballs/XML_Util-1.2.1.tar…
install ok: channel://pear.php.net/Archive_Tar-1.3.7
install ok: channel://pear.php.net/Console_Getopt-1.3.0
install ok: channel://pear.php.net/Structures_Graph-1.0.4
install ok: channel://pear.php.net/XML_Util-1.2.1
install ok: channel://pear.php.net/PEAR-1.9.4
PEAR: Optional feature webinstaller available (PEAR’s web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR’s PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR’s PHP-GTK2-based installer)
PEAR: To install optional features use 「pear install pear/PEAR#featurename」
** WARNING! Old version found at /usr/local/php5/bin, please remove it or be sure to use the new /usr/local/php5/bin/pear command
The ‘pear’ command is now at your service at /usr/local/php5/bin/pear
** The ‘pear’ command is not currently in your PATH, so you need to
** use ‘/usr/local/php5/bin/pear’ until you have added
** ‘/usr/local/php5/bin’ to your PATH environment variable.
Run it without parameters to see the available actions, try ‘pear list’
to see what packages are installed, or ‘pear help’ for help.
For more information about PEAR, see:
http://pear.php.net/faq.php
http://pear.php.net/manual/
安裝完畢
安裝 pear 後,直接執行:
1
2
3
4
5
|
[root@www bin]
# pear install Net_Gearman
# 若是提示
# No releases available for package "pear.php.net/Net_Gearman"
# 則執行
[root@www bin]
# pear install channel://pear.php.net/Net_Gearman-0.2.3
|
Net_gearman安裝完後就能夠用gearmanMonitor了
下載後放到web目錄下就能夠訪問了。
修改GearmanMonitor目錄下的_config.php配置文件
<?php /** * Gearman Monitor configuration file * * The following server fields are available: * - address: Gearman server address, hostname and port * - name: Gearman server name to display in Gearman Monitor interface * * Example: * $cfgServers[$i]['address'] = '192.168.0.10:4730'; * $cfgServers[$i]['name'] = 'Gearman server 1'; * ++ $i; * * $cfgServers[$i]['address'] = '192.168.1.1:7003'; * $cfgServers[$i]['name'] = 'Gearman server 2'; * ++ $i; */ $i = 0; $cfgServers = array(); $cfgServers[$i]['address'] = ''; $cfgServers[$i]['name'] = ''; ++ $i;
address改爲127.0.0.1:4730
GearmanManager 用來統一管理用 PHP 編寫的 Gearman workers。須要 PHP 啓用 pcntl。用 install/install.sh 安裝完成後,根據須要和設置,修改 /etc/init.d/gearman-manager
:
##PATH##
DAEMON=
/usr/local/bin/gearman-manager
PIDDIR=
/tmp
PIDFILE=${PIDDIR}
/manager
.pid
LOGFILE=
/tmp/gearman-manager
.log
CONFIGDIR=
/data/gearman-manager
GEARMANUSER=
"gearmand"
PARAMS=
"-c ${CONFIGDIR}/config.ini"
|
GearmanManager 安裝時,選擇的是 PECL library,啓動時可能會碰見以下的問題:
1
2
3
4
5
|
[root@www gearman-manager]
# /etc/init.d/gearman-manager start
Starting gearman-manager: [ OK ]
[root@www gearman-manager]
# php: libgearman/universal.cc:553: bool gearman_request_option(gearman_universal_st&, gearman_string_t&): Assertion `con->recv_state == GEARMAN_CON_RECV_UNIVERSAL_NONE' failed.
php: libgearman
/universal
.cc:553: bool gearman_request_option(gearman_universal_st&, gearman_string_t&): Assertion `con->recv_state == GEARMAN_CON_RECV_UNIVERSAL_NONE' failed.
.....
|
解決辦法如 Bug #60764 Enabling Non-Blocking Mode causes asseration 這裏所提, 修改 pecl-manager.php :
1
2
|
//
註釋下面這句
//
$thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
|