在 CentOS 6.3 系统下安装 Gearman,并给出 PHP 实现小例子

学习笔记 马富天 2017-11-20 22:34:21 83 0

【摘要】由于我想尝试一下 Gearman 分布式处理一些程序,所以在 CentOS 6.3 系统下面搭建 gearman ,并使用 php 编程给出一个小例子,由于在刚刚开始的过程中遇到许多的问题,花了很长的时间才搭建起来,所以本文给出详细的记录。

系统:CentOS 6.3

Gearman 版本:gearmand-1.1.12

php 扩展 gearman 版本:gearman-1.1.2

php 是否需要安装:是

apache 是否需要安装:是

Gearman 的下载地址:

https://launchpad.net/gearmand/

PHP 的 Gearman 扩展下载地址:

http://pecl.php.net/package/gearman

下载方法:如果使用 wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz 这种方式下载的话,可能下载的不是源文件,可能不是直接下载该文件,需要在 windows 系统中下载后使用 winscp 工具将这两个文件放置到 centOS 系统中。另外 gearmand-1.1.12 对应的 php 扩展 gearman 版本是 1.1.2 ,如果低于此版本,可能会报错。

1. 先安装依赖库

  1. # yum install -y boost-devel gperf libevent-devel gcc-c++ libuuid-devel

2.安装 gearman

  1. tar xf gearmand-1.1.12.tar.gz
  2. cd gearmand-1.1.12
  3. ./configure
  4. make 
  5. make install

随意进入其他目录执行 gearman,检查安装是否成功:

  1. # gearman
  1. gearman Error in usage(No Functions were provided).
  2. Client mode: gearman [options] [<data>]
  3. Worker mode: gearman -w [options] [<command> [<args> ...]]
  4. Common options to both client and worker modes.
  5.         -f <function> - Function name to use for jobs (can give many)
  6.         -h <host>     - Job server host
  7.         -H            - Print this help menu
  8.         -v            - Print diagnostic information to stdout(false)
  9.         -p <port>     - Job server port
  10.         -t <timeout>  - Timeout in milliseconds
  11.         -i <pidfile>  - Create a pidfile for the process
  12.         -S            - Enable SSL connections
  13. Client options:
  14.         -b            - Run jobs in the background(false)
  15.         -I            - Run jobs as high priority
  16.         -L            - Run jobs as low priority
  17.         -n            - Run one job per line(false)
  18.         -N            - Same as -n, but strip off the newline(false)
  19.         -P            - Prefix all output lines with functions names
  20.         -s            - Send job without reading from standard input
  21.         -u <unique>   - Unique key to use for job
  22. Worker options:
  23.         -c <count>    - Number of jobs for worker to run before exiting
  24.         -n            - Send data packet for each line(false)
  25.         -N            - Same as -n, but strip off the newline(false)
  26.         -w            - Run in worker mode(false)
  27. # 

出现上面显示,说明安装 Gearman 成功了。

启动与停止 Gearman 服务:

1). 启动 Gearman

  1. # gearmand -d --log-file=/data/gearman/logs/gearmand.log 

log-file 参数指定日志文件。

2). 停止 Gearman

  1. # gearadmin --shutdown

3.安装 php 的 gearman 扩展

  1. [root@localhost html]# cd gearman-1.1.2
  2. [root@localhost gearman-1.1.2]# yum install php-devel
  3. [root@localhost gearman-1.1.2]# phpize
  4. Configuring for:
  5. PHP Api Version:         20090626
  6. Zend Module Api No:      20090626
  7. Zend Extension Api No:   220090626
  8. [root@localhost gearman-1.1.2]# ./configure
  9. [root@localhost gearman-1.1.2]# make
  10. [root@localhost gearman-1.1.2]# make install
  11. Installing shared extensions:     /usr/lib/php/modules/

4.打开 PHP 的配置文件 php.ini 在尾部添加如下一行,然后重启 apache 即可。(查找 php 配置文件 find / -name php.ini)

  1. extension = gearman.so

查看是否已加载 gearman 模块

  1. [root@localhost conf]# php -m

检测是否安装完毕:test.php:

  1. print gearman_version();        //      输出 gearman 的版本

5.启动 gearman

  1. #    这里要先创建 gearman.log 文件,否则会报错
  2. [root@newCentOS log]# gearmand -d --log-file=/var/www/log/gearman.log

6.执行 PHP 代码,一个小实例

worker.php 代码:

  1. $worker= new GearmanWorker();    
  2. $worker->addServer("127.0.0.1", 4730);     
  3. $worker->addFunction("title", "title_function");    
  4. while (true){
  5.         $worker->work();
  6.         if ($worker->returnCode() != GEARMAN_SUCCESS) {
  7.         //Gearman 状态错误 需要做日志或异常处理
  8.      }
  9.   }
  10. function title_function($job)    
  11. {    
  12.         $str = $job->workload();    
  13.         return strlen($str);    
  14. }

client.php 代码:

  1. $client= new GearmanClient();    
  2. $client->addServer("127.0.0.1", 4730);    
  3. print $client->do("title", "Linvo");    
  4. print "/n";

终端一:

  1. [root@newCentOS html]# php worker.php

终端二:

  1. [root@newCentOS html]# php client.php 
  2. 5/n 

这样就完成了一个小实例。

版权归 马富天个人博客 所有

本文标题:《在 CentOS 6.3 系统下安装 Gearman,并给出 PHP 实现小例子》

本文链接地址:http://www.mafutian.com/346.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

0

0

上一篇《 VMware 桥接模式设置实现虚拟机连接外网 》 下一篇《 如何在 IIS 服务器下执行 PHP 程序,同时执行 ASP 代码 》

暂无评论

评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多