#gearman服务
Gearman 任务分发程序;
Gearman client: 请求的发起者
Gearman job server: gearman worker的调度者,不处理业务逻辑
Gearman worker: 提供worker API给应用程序调用,复制客户端请求,处理结果返回客户端
适用场景:Mogilefs分布式文件系统;应用方面的负载均衡
导入数据,发送电子邮件,编码视频文件,日志系统;不影响网站体验和响应,可以并行处理数据
Gearman持久化:Gearman工作队列保存在内存和关系型数据库内存有备份,gearman故障,重启后重新创建这个工作队列;memcached集群增加gearman持久性,依赖memcache分布式
###编译安装gearman
gearman托管地址:https://launchpad.net/gearmand
wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz
tar zxvf gearmand-1.1.12.tar.gz
cd gearmand-1.1.12
./configure --prefix=/opt/local/gearman
make && make install
###php gearman扩展
wget http://pecl.php.net/get/gearman-1.1.2.tgz
tar zxvf gearman-1.1.2.tgz
cd gearman-1.1.2/
/opt/local/php/bin/phpize
./configure --with-php-config=/opt/local/php/bin/php-config --with-gearman=/opt/local/gearman
make && make install
加入gearman.so文件
[gearman]
extension=/opt/local/php/lib/php/extensions/no-debug-non-zts-20131226/gearman.so
###php的测试文件
client.php客户端程序
<?php
$client = new GearmanClient();
$client->addServer("127.0.0.1",4730);
print $client->do("title","daxia");
print "\n";
worker.php服务程序
<?php
$worker = new GearmanWorker();
$worker->addServer("127.0.0.1",4730);
$worker->addFunction("title","title_function");
while($worker->work());
function title_function($job) {
$str = $job->workload();
return strlen($str);
}
###gearman+mysql实现持久化队列
持久化队列是在0.6版本中新添的一项功能,允许将队列存放在drizzle或mysql中。0.7版本允许将队列存放在memcached。0.9版本可以将队列存放在sqlite3或postgresq
create database gearman;
create table `gearman_queue` (
`unique_key` varchar(64) NOT NULL,
`function_name` varchar(255) NOT NULL,
`priority` int(11) NOT NULL,
`data` LONGBLOB NOT NULL,
`when_to_run` INT,
primary key (`unique_key`)
);
###gearman命令
-b 储备的监听连接数量(默认值32)
-d 后台运行
-f 文件描述符的数量
-j 在job server移除不可用job之前运行的次数,防止不断运行导致其他可用worker崩溃。默认没有限制
-l 日志文件存放位置(默认记录最简单日志/opt/local/gearman/var/log/gearmand.log)
-L 监听的IP,默认全部接受
-r 加载协议模块
-q 指定持久化队列
--config-file =/opt/local/gearman/etc/gearmand.conf
-t 使用的I/O线程数量。默认为4
-u 启动后,切换到指定用户
--http-port http端口
-p 4730
--ssl Enable ssl connections.
--ssl-ca-file arg CA file.
--ssl-certificate arg SSL certificate.
--ssl-key arg SSL key for certificate.
###gearman监控
GearmanManager Gearman-Monitor
###gearman faq
1、configure: error: could not find boost
yum install -y boost-devel
2、configure: error: could not find gperf
yum search gperf
yum install gperf.x86_64
3、configure: error: Unable to find libevent
yum install libevent-devel
4、configure: error: Unable to find libuuid
yum install libuuid-devel