做了german集群,所以worker.php要复制两份,当然german都要安装两份。
german是自动根据机器任务程度自动分发的,不需要做任何关于它的配置。
Linux上安装:
202 yum install -y boost-devel gperf libevent-devel libuuid-devel 203 wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz 204 LS 205 ls 206 tar xf gearmand-1.1.12.tar.gz 207 cd gearmand-1.1.12 208 ls 209 ./configure 210 make && make install 211 /sbin/ldconfig 212 cd /opt 213 wget http://pecl.php.net/get/gearman-1.1.2.tgz 214 tar xf gearman-1.1.2.tgz 215 ls 216 cd gearman-1.1.2 217 ls 218 /usr/local/php/bin/phpize 219 ./configure --with-php-config=/usr/local/php/bin/php-config --with-gearman 220 make && make install 221 make test 222 vi /home/wwwroot/bulutu.a.com/test.php 223 vi /usr/local/php/etc/php.ini 224 /etc/init.d/php-fpm restart 225 /usr/local/sbin/gearman -p 4730 -u root -d 226 /usr/local/sbin/gearmand -p 4730 -u root -d 227 mkdir -p /usr/local/var/log 228 /usr/local/sbin/gearmand -p 4730 -u root -d 229 ls /home/wwwroot/bulutu.a.com/ 230 vi /home/wwwroot/bulutu.a.com/worker.php 231 cd /home/wwwroot/bulutu.a.com/ 232 php worker.php &
worker.php 源码:
<?php $worker = new GearmanWorker(); $worker->addServer("192.168.1.11",4730); $worker->addServer("192.168.1.12",4730); $worker->addFunction("title","title_function"); while($worker->work());
//不同机器返回不同以作区别 function title_function($job) { return ucwords(strtolower($job->workload()));//192.168.1.11
retrun '12321321';//192.168.1.12
} ?>
client.php 源码:
<?php $client = new GearmanClient(); $makr = 1; if(!$client->addServer("192.168.1.11",4730)){echo 'hh';exit;} if(!$client->addServer("192.168.1.12",4730)){echo 'dd';exit;} print $client->do("title","AIL THE World's a sTagE"); print "\n"; ?>
结果:
参考资料:http://learnbank.blog.163.com/blog/static/12869923720126122212562/