Gearman is a system used to delegate work to other machines, distributed calls to a machine that is more suitable for a job, concurrent workloads that do some work across multiple calls, or functions that are used to invoke other languages.
Official website: http://gearman.org/
The following operations are performed under Ubuntu12.0, and the other operating systems are identical.
1, Gearman Installation
Install dependent packages
sudo apt-get install Libboost-program-options-dev libevent-dev Uuid-dev
As of the author Post Gearman latest stable version 1.0. 6:https://launchpad.net/gearmand/1.0/1.0.6/+download/gearmand-1.0.6.tar.gz
wget HTTPS://LAUNCHPAD.NET/GEARMAND/1.0/1.0.6/+DOWNLOAD/GEARMAND-1.0.6.TAR.GZTAR-ZXVF GEARMAND-1.0.6.TAR.GZCD Gearmand-1.0.6./configure--prefix=/usr/local/gearmandsudo Makesudo make install
If the following error message appears:
Sorry, Unimplemented:graphite loop optimizations can only being used if the LIBCLOOG-PPL0 package is installed
Workaround: Modify makefile, find and remove floop-parallelize-all
Start Gearmand
/usr/local/gearmand/sbin/gearmand-d
If the prompt log file/usr/local/gearmand/var/log/does not exist, the new
sudo mkdir-p/usr/local/gearmand/var/log/sudo chmod 777/usr/local/gearmand/var
Download Gearman Extensions for PHP
wget HTTP://PECL.PHP.NET/GET/GEARMAN-1.0.2.TGZTAR-ZXVF GEARMAN-1.0.2.TGZCD gearman-1.0.2
Installing Libgearman
sudo apt-get install Libgearman-dev
The author's PHP is installed in the/usr/loal/php directory,
/usr/local/php/bin/phpize./configure--with-php-config=/usr/local/php/bin/php-configsudo Makesudo make install
The Gearman so file is generated under folder/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/, and the edit php.ini loads gearman.so
Vim/usr/local/php/etc/php.ini
Add to
Extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gearman.so
I use the server for the NGINX+PHP-FPM mode, restart PHP-FPM can
sudo kill-usr2 php-fpm-master-pid
View Phpinfo to see if Gearman is loading
Php-r "Phpinfo ();" | grep Gearman
Installation is complete.
2, the use of Gearman
For the official online, can be very good to explain the composition of the three parts of the division of Gearman.
By, we can easily find that Gearman is made up of Client+job Server+worker.
Client: It offers a variety of programming languages (C,php,perl ... ) API, which is the initiator of the request in Gearman.
Job Server: It will find a worker who can execute the request based on a request from the client, and accept the worker's response and respond to the client.
Worker: It offers a variety of programming languages (C,php,perl ... ) API, in Gearman as the true respondent of the request.
Here is an example of PHP using Gearman:
Client:client.php
<?php//Create Our client object$client = new Gearmanclient ();//Add a server$client->addserver (); By default host/port would be "localhost" & 4730echo "Sending job\n";//Send Reverse Job$result = $client->donorm Al ("Reverse", "hello!"); if ($result) { echo "Success: $result \ n";}
Worker:worker.php
<?php//Create our worker object$worker = new Gearmanworker ();//ADD a server (again, same defaults apply as a worker) $ Worker->addserver ();//Inform The server that this worker can process "reverse" function calls$worker->addfunction ( "Reverse", "REVERSE_FN"); while (1) { print ' waiting for job...\n '; $ret = $worker->work (); Work () would block execution until a job is delivered if ($worker->returncode ()! = gearman_success) { break;< c4/>}}//A Much simple reverse functionfunction reverse_fn (gearmanjob $job) { $workload = $job->workload (); echo "Received job:". $job->handle (). "\ n"; echo "Workload: $workload \ n"; $result = Strrev ($workload); echo "Result: $result \ n"; return $result;}
Run:
PHP worker.php &php client.php