Gearman简介
概况
Gearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统。
组成
Gearman是一个分发任务的程序架构,由三部分组成:
- Gearman client:提供gearman client API给应用程序调用。API可以使用C,PHP,PERL,MYSQL UDF等待呢个语言,它是请求的发起者。
- Gearman job server:将客户端的请求分发到各个gearman worker的调度者,相当于中央控制器,但它不处理具体业务逻辑。
- Gearman worker:提供gearman worker API给应用程序调用,具体负责客户端的请求,并将处理结果返回给客户端。
应用
Mogilefs的分布式文件系统的核心就是用gearman实现的。
这个软件的应用场景很多,比如视频网站的视频处理,分布式日志处理,电子邮件处理,文件同步处理,图片处理等等,只要是可以放开,不影响体验和响应的场 景,需要并行进行大量计算和处理的程序都是可以的。Yahoo在60或更多的服务器上使用gearman每天处理600万个作业。新闻聚合器digg构建 了一个相同规模的gearman网络,每天可处理400000个作业。
Gearman不但可以做为任务分发,还可以做为应用方面的负载均衡。可以让worker放在不同的一堆服务器上,也可以启动放在同一个cpu的多个核 上。比如,应用视频转换程序,不希望web服务器来处理视频格式转换,这时,可以在这一堆服务器上进行任务分发,在上面加载worker处理视频格式,对 外的web服务器就不会被视频转换过程影响。而且扩展方便,加一台服务器到任务调度中心,注册成worker即可,这时job server会在请求到来的时候,将请求发送给空闲的worker。还可以运行多个job server,组成ha架构,如果一个job server当掉了,client和worker会自动迁移到另一台job server上。
工作原理
运行过程
一个Gearman请求的处理过程涉及三个角色:Client -> Job -> Worker。
Client:请求的发起者,可以是 C,PHP,Perl,MySQL UDF 等等。
Job:请求的调度者,用来负责协调把 Client 发出的请求转发给合适的 Work。
Worker:请求的处理者,可以是 C,PHP,Perl 等等。
因为 Client,Worker 并不限制用一样的语言,所以有利于多语言多系统之间的集成。
甚至我们通过增加更多的 Worker,可以很方便的实现应用程序的分布式负载均衡架构。
Gearman安装
官网: http://gearman.org/
官网下载: https://launchpad.net/gearmand
PHP扩展:http://pecl.php.net/package/gearman
PHP使用:http://php.net/manual/en/book.gearman.php
一键下载:http://download.youkuaiyun.com/detail/webben/9781877
Linux中必备常用支持库的安装(CentOS-6.5)
http://blog.youkuaiyun.com/webben/article/details/62217156
#安装依赖库
yum install -y boost-devel gperf libevent-devel libuuid-devel
shell> cd /usr/local/src/
#下载
shell> wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz
#解压
shell> tar zxf gearmand-1.1.12.tar.gz
shell> cd gearmand-1.1.12
#配置
shell> ./configure
#编译安装
shell> make && make install
#测试
shell> gearman
出现上图就安装成功了。
安装过程中出现过的错误(添加依赖库就能解决):
#错误:configure: error: could not find boost
#解决
shell> yum install -y boost boost-devel
#错误:configure: error: could not find gperf
#解决
shell> yum install gperf
#错误:configure: error: Unable to find libevent
#解决
shell> yum install libevent-devel
#错误:configure: error: Unable to find libuuid
#解决
shell> yum install libuuid-devel
安装PHP拓展
shell> cd /usr/local/src
#解压
shell> tar xzf gearman-1.1.2.tgz
#配置
shell> cd gearman-1.1.2
shell> phpize
shell> ./configure
#编译安装
shell> make && make install
出现以下代表安装成功
Installing shared extensions: /usr/local/php/lib/php/extensions/
#编辑配置文件
shell> vim /usr/local/php/etc/php.ini
#增加gearman拓展
extension=gearman.so
#重启Web服务或者php-fpm。
shell> service php-fpm restart
#测试
shell> php -r 'phpinfo();' | grep gearman
#出现以下代表安装成功
gearman
gearman support => enabled
libgearman version => 1.1.12
Gearman启动停止
#创建日志
shell> mkdir -p /data/logs
shell> touch /data/logs/gearmand.log
#启动
shell> /usr/local/sbin/gearmand -d -u root -L 192.168.43.135 --log-file=/data/logs/gearmand.log
#报错
/usr/local/sbin/gearmand: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
#解决
#1.先确认/usr/local/mysql/lib/下是否有libmysqlclient.so.18这个文件.
shell> ll /usr/local/mysql/lib/|grep libmysqlclient.so.18
#2.修改ld.so.conf文件
shell> echo "/usr/local/mysql/lib" >>/etc/ld.so.conf
#3.然后执行ldconfig使其生效.
shell> /usr/local/mysql/lib/ldconfig
#再执行启动命令
#查看是否启动成功
shell> ps -ef | grep gearman
root 86570 1 0 03:17 ? 00:00:00 /usr/local/sbin/gearmand -d -u root -L 192.168.43.135 --log-file=/data/logs/gearmand.log
#查看监听端口
shell> netstat -anp | grep 86570
#停止,直接kill掉进程。
shell> hill -9 86570
参数解释
- -b,–backlog= 储备的监听连接数量
- -d, –daemon 后台运行
- -f, –file-descriptors= 文件描述符的数量
- -h, –help 帮助
- -j, –job-retries= 在ob server移除不可用job之前运行的次数,防止不断运行导致其他可用worker崩溃。默认没有限制
- -l, -log-file= 日志文件存放位置(默认记录最简单日志)
- -L, –listen= 监听的IP,默认全部接受
- -p, –port= 指定监听端口
- -P, –pid-file= 指定进程ID写入位置
- -r, –protocol= 加载协议模块
- -q, –queue-type= 指定持久化队列
- -t, –threads= 使用的I/9线程数量。默认为0
- -u, –user= 启动后,切换到指定用户
- -v, –verbose 增加一级详细程度
- -V, –version 显示版本信息
Gearman使用
创建Worker
创建worker.php。
# php code
$worker= new GearmanWorker();
$worker->addServer('192.168.43.135', '4730');
$worker->addFunction("repeat", "str_repeat_callback");
while ($worker->work());
function str_repeat_callback($job){
// 接收数据
$tmp = $job->workload();
//重复次数
$rs = str_repeat( '.' , $tmp );
echo $rs."\n";
return $rs;
}
启动Worker端
shell> php worker.php
创建Client(阻塞模式,同步返回结果)
创建一个client.php,do()方法是阻塞模式,必须等待worker端返回结果,程序才能停止。
$client= new GearmanClient();
$client->addServer('192.168.43.135', '4730');
for( $i = 0;$i < 10 ; $i++ )
{
//等到worker端返回结果,才会结束。
$ret = $client->do("repeat", $i);
echo $ret."\n";
}
结果如下:
创建Client(非阻塞,异步请求)
创建client1.php。doBackground()不用等待worker端返回结果,程序就结束了。
$client= new GearmanClient();
$client->addServer('192.168.43.135', '4730');
for( $i = 0 ;$i < 10;$i++)
{
//不等待返回结果,就会结束
$ret = $client->doBackground("repeeat", $i);
echo $ret."\r\n";
}
更多使用方法请参考:
http://php.net/manual/en/book.gearman.php
Gearman管理
输入以下命令,查看4730端口情况。
shell> (echo "status" ; sleep 2 ) | telnet 192.168.43.135 4730
- 字段说明:”已知注册的任务” “正在运行的任务” “队列中的任务” “可用的 Worker”.
- repeat 0 0 2,注册的任务名为 repeat ,0 个正常在运行,队列为空,有一个可用的 Worker.