How to use Gearman for distributed computing

  

Generally, the integration between multi-language and multi-system is a big problem. Generally speaking, people will use WebService to deal with such integration problems, no matter what style is used. WebService, such as RPC style, or REST style, has its own complexity. In contrast, Gearman can achieve a similar effect, and is easier to use. The processing of a Gearman request involves three roles: Client -> Job -> Worker. Client: The originator of the request, which can be C, PHP
, Perl, MySQL UDF, etc. Job: The dispatcher of the request, responsible for coordinating the forwarding of requests from the client to the appropriate Work. Worker: The handler of the request, which can be C, PHP, Perl, etc. Because Client, Worker does not restrict the use of the same language, it is conducive to the integration between multi-language and multi-system. Even by adding more workers, we can easily implement a distributed load balancing architecture for the application. Let's take a look at how to install and run an example. Under the limited conditions, we run the three roles Client, Job, and Worker on a server: Install Gearman server and library: wget http://launchpad.net/gearmand/trunk/0.8 /+download/gearmand-0.8.tar.gztar zxf gearmand-0.8.tar.gzcd gearmand-0.8./configuremakemake install Install Gearman PHP extension:wget http://pecl.php.net/get/gearman-0.4.0. Tgztar zxf gearman-0.4.0.tgzcd gearman-0.4.0phpize./configuremakemake install Edit the php.ini configuration file to load the corresponding module and make it effective: extension = "gearman.so"Start Job:gearmand -d if the current user is For root, you need to do this: geomand -d -u root will use port 4730 by default, which will be used below. Note: If you can't find the path to the gearand command, don't forget to confirm with whereis gearmand. Write the contents of the Worker:worker.php file 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());}?> Set the background to run work:php worker.php & Write the Client:client.php file as follows: <?php$client= new GearmanClient();$client->addServer('127.0.0.1', 4730);echo$client->do('reverse' , 'Hello World!'), "\ ";?>Run client:php client.php output:!dlroW olleH For convenience, Worker, Client uses PHP, but this does not affect the demo In practical applications, you can integrate Worker, Client in different languages ​​through Gearman. Perhaps you still want to understand the load balancing function mentioned earlier: very simple, just add more workers, you can write several similar files according to the way worker.php, and set different return values ​​for Identify the presentation. Then start these Worker files in turn, and use client.php multiple times to request, you will find that the Job will forward the Client request to a different Worker. Command Line Tools If you feel that installing something like PHP is too much trouble, you can also experience Gearman's functionality just by using the command line tool: Start Worker: gearman -w -f wc -- wc -l & Run Client: gearman -f wc < /etc/passwd Specific reference to the official documentation, there are some good PDF.

Copyright © Windows knowledge All Rights Reserved