Often, integration between multi-lingual and multi-system is a big problem, in general, people tend to use webservice to deal with such integration problems, but regardless of the style of webservice, such as RPC style, or restful style, it has a certain complexity. Gearman, by contrast, can achieve similar effects and are easier to use.
The processing of a Gearman request involves three roles: Client-to-Job Worker.
Client: The initiator of the request, which can be a c,php,perl,mysql UDF, and so on.
Job: The dispatcher of the request is responsible for coordinating the forwarding of requests from the client to the appropriate work.
Worker: The handler for the request, which can be c,php,perl, and so on.
Because Client,worker does not restrict the use of the same language, it facilitates integration between multiple languages and multiple systems.
Even by adding more workers, we can easily implement the distributed load-balancing architecture of the application.
Below is a look at how to install the run an example, the condition is limited, we put client,job,worker three roles on a server:
Install Gearman server and library:
wget http://launchpad.net/gearmand/trunk/0.8/+download/gearmand-0.8.tar.gz
Tar zxf gearmand-0.8.tar.gz
CD gearmand-0.8
./configure
Make
Make install
Install gearman PHP Extension:
wget http://pecl.php.net/get/gearman-0.4.0.tgz
Tar zxf gearman-0.4.0.tgz
CD gearman-0.4.0
Phpize
./configure
Make
Make install
Edit the php.ini configuration file to load the appropriate module and make it effective:
Extension = "gearman.so"
Start Job:
Gearmand-d
If the current user is root, you need to do this:
gearmand-d-U Root
The provincial capital uses port 4730, which is used below.
Note: If you cannot find the path to the Gearmand command, do not forget to confirm with Whereis Gearmand.
Write worker:
The contents of the worker.php file are as follows:
<?php
$worker = new Gearmanworker ();
$worker->addserver (' 127.0.0.1′, 4730);
$worker->addfunction (' reverse ', ' my_reverse_function ');
while ($worker->work ());
function My_reverse_function ($job)
{
Return Strrev ($job->workload ());
}
?>
To set up a background run work:
PHP worker.php &
Write client:
The contents of the client.php file are as follows:
<?php
$client = new Gearmanclient ();
$client->addserver (' 127.0.0.1′, 4730);
echo $client->do (' reverse ', ' Hello world! '), ' \ n ';
?>
Run Client:
PHP client.php
Output:!dlrow Olleh
For convenience, Worker,client uses PHP, but this does not affect the presentation, and in practice, you can fully integrate the worker,client implemented in different languages through Gearman. Perhaps you also want to understand the load balancing feature mentioned earlier: simply by adding multiple workers, you can write a few similar files as worker.php, and set different return values to identify the presentation effect. Then start the worker files in turn, and use client.php to request them several times, and you will notice that the job forwards the client request to the different worker.
Command-line tools
If you think it's too much of a hassle to install something like PHP, you can only experience gearman with command-line tools:
Start Worker:gearman-w-F Wc-wc-l &
Running Client:gearman-f WC </etc/passwd
Author: Night Light Rain
Original link: PHP implementation of asynchronous Task distribution processing tool-gearman
Night Light rain All rights reserved, please retain this information when reprint this website article.
PHP implements asynchronous Task distribution processing tool-gearman