1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//gearman是一个任务分发的程序框架,可以用来当队列用,可以替代多语言集成时常用的webservice方式,可以实现分布式负载均衡 //gearman请求处理流程:client (c|php|xxx) -> job (gearman服务器) -> worker (c|php|xxx) //gearman是通过分发给不同服务器的多个work来实现负载均衡的 wget https://launchpadlibrarian.net/165674261/gearmand-1.1.12.tar.gz tar -zxvf gearmand-1.1.12.tar.gz mv gearmand-1.1.12 /usr/local/gearmand cd /usr/local/gearmand ./configure //如果报错 configure: error: could not find gperf yum install gperf* ./configure //如果报错 configure: error: Unable to find libuuid yum install libuuid* ./configure make make install //启动gearmand mkdir -p /usr/local/var/log/ cd /usr/local/var/log/ touch gearmand.log //如果是root用户 -u root 默认端口4730 gearmand -d -u root |
1 2 3 4 5 6 7 8 |
wget http://pecl.php.net/get/gearman-1.1.2.tgz tar -zxvf gearman-1.1.2.tgz cd gearman-1.1.2 phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install //设置php.ini extension=gearman.so 并重启nginx php-fpm |
1 2 3 4 5 6 7 8 9 |
<?php $worker= new \GearmanWorker(); $worker->addServer('192.168.22.190', 4730); $worker->addServer('192.168.22.191', 4730); $worker->addFunction('test', 'doWorkTest'); while ($worker->work()); function doWorkTest($job) { return $job->workload(); } |
1 2 3 4 5 |
<?php $client= new \GearmanClient(); $client->addServer('192.168.22.190', 4730); $client->addServer('192.168.22.191', 4730); echo $client->do('test', 'Hello World!'), "\n"; |