Distributed task Distribution Framework Gearman tutorials and examples of PHP implementations

Source: Internet
Author: User
Tags php cli

1, Gearman Introduction and use Scene Gearman is a distribution task of the program framework, can be used in various situations, compared with Hadoop, Gearman more inclined to the task distribution function. Its task distribution is very simple, simple can only be done with a script. Gearman originally used for livejournal image resize function, because the picture resize need to consume a lot of computing resources, so it needs to dispatch to the back-end of multiple servers to execute, after the completion of the task to return to the front-end rendering to the interface.
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. 2. Gearman Job distribution Server Installation installation Gearman servers 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
sudo make
sudo make install

You may encounter some problems in the middle:
In the./configure, there may be a lack of libraries, generally the lack of libevent and UUID of the two development packages, installation of ...

sudo apt-get install Libevent-dev
sudo apt-get install Uuid-dev

After the installation is complete, reconfigure the installation and execute after the installation is complete
sudo ldconfig3, Gearman client and worker side PHP Implementation example installation 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
sudo make
sudo make install

Problems that may be encountered in the middle:
phpize command not found, phpize in PHP development package, so install Php5-dev first

sudo apt-get install Php5-dev

After the installation, you can execute phpize in the source directory to generate the relevant installation configuration information, and then perform the following./configure, etc.

After make install, it tells you a directory where the generated gearman.so is.

Test to the appropriate PHP extension directory (because I am using the default installation of PHP directly, it is automatically generated in the extension)


Next, modify the php.ini so that PHP loads the module:
PHP--ini See where php.ini is, sudo vim modified, in which to add
Extension = "gearman.so"

Then, start writing client and worker side

client.php

<? PHP       $client New gearmanclient ();       $client->addserver ("127.0.0.1", 4730);       Print $client,do ("title", "Linvo");       Print "/n"    ;

worker.php

<?PHP$worker=NewGearmanworker (); $worker->addserver ("127.0.0.1", 4730); $worker->addfunction ("title", "Title_function");  while(true){     $worker-Work (); if($this->worker->returncode ()! =gearman_success) {//Gearman status error requires log or exception handling     }  }         functionTitle_function ($job)      {        $str=$job-workload (); return strlen($str); }    ?>

The preparations are complete, the test begins.
1. Start Job
Gearmand-d

2. Start worker
Php-c/etc/php5/apache2/php.ini worker.php

3. Start client (open in new terminal)
Php-c/etc/php5/apache2/php.ini client.php

The screen displays the length of the string "5"

Here, there are a few things to explain:
1, here directly with the PHP CLI operation, add-c parameter is to load the php.ini configuration file to load the Gearman extension
2, the worker should be made into a daemon (CLI mode), you can open multiple, so that the client-initiated task will be distributed to each worker to perform (automatic load Balancing)
This example is too simple, even if the opening of multiple workers can not see the effect, but by terminating one, you can see the system automatically switch to other workers continue to perform normal
3, the same, the client can also open multiple (model please refer to the previous side of the log)

4, at the same time, the job can also open multiple to avoid a single point of failure

Distributed task Distribution Framework Gearman tutorials and examples of PHP implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.