Gearman的介绍php
主页在http://gearman.org/index.php, 它的主要优势有:服务器
Gearman的安装配置负载均衡
1.安装Gearman server and library:异步
wget http://launchpad.net/gearmand/tr ... gearmand-0.8.tar.gz tar zxf gearmand-0.8.tar.gz cd gearmand-0.8 ./configure make make install分布式
2.安装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性能
3.编辑php.ini配置文件加载相应模块并使之生效:.net
extension = "gearman.so"调试
4.启动Job:server
gearmand -d
若是当前用户是root的话,则须要这样操做:
gearmand -d -u root
缺省会使用4730端口,下面会用到。
以调试的方式启动:
gearmand -vv
5.编写Worker:
worker.php文件内容以下:
<?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()); } ?>
设置后台运行work:
php worker.php &
6.编写Client:
client.php文件内容以下:
<?php $client= new GearmanClient(); $client->addServer('127.0.0.1', 4730); echo $client->do('reverse', 'Hello World!'), "/n"; ?>
运行client:
php client.php
输出:!dlroW olleH