您的位置:首页 > 编程语言 > PHP开发

gearman 在mac上为php添加Gearman扩展

2016-04-18 18:34 645 查看
首先区分一下概念-GearmanGearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统。一个Gearman请求的处理过程涉及三个角色:Client -> Job -> Worker。Client:请求的发起者,可以是 C,PHP,Perl,MySQL UDF 等等。Job:请求的调度者,用来负责协调把 Client 发出的请求转发给合适的 Worker。Worker:请求的处理者,可以是 C,PHP,Perl 等等。因为 Client,Worker 并不限制用一样的语言,所以有利于多语言多系统之间的集成。甚至我们通过增加更多的 Worker,可以很方便的实现应用程序的分布式负载均衡架构。-gearman php extension是对libgearman接口的php封装,是我们在php编码时直接使用的。1.Install gearman environment在http://gearman.org/getting-started/上看到gearman对于不同系统的安装方法,而对应Mac的包安装当然应该使用brew。brew install gearmand 安装gearman服务端,其中包括libgearman。通过brew安装的程序会在/usr/local/Cellar/中,所以gearman的位置是/usr/local/Cellar/gearman。gearmand: gearman 守护进程,类似 mysqld 命令(/usr/local/Cellar/gearman/1.1.12/sbin)gearadmin: 如文件名,用于管理 gearman,类似 mysqladmin 命令(/usr/local/Cellar/gearman/1.1.12/bin)gearman: gearman 命令行客户端,类似 mysql 命令(/usr/local/Cellar/gearman/1.1.12/bin)至此,gearman环境安装完毕。由于gearmand没有设置环境变量,现在只能在相应文件路径下执行gearmand操作。可以在terminal中测试一下:

-Worker

The 
-W
 flag tells the gearman tool to run in worker mode. After all options, a command canbe specified to run for each job. For example:
$ gearman -w -f wc -- wc -l
This will start a worker, connect to the job server on 
localhost
 (the default), and registerthe function 
wc
 (the argument to the 
-f
option
). For each job that comes in, the gearman tool will fork a process to run the 
wc -l
 command. The gearman toolwill write the workload of the job to the processes standard input, and then read the response from standard output. The gearman tool will wait and accept jobs indefinitely, so press CTRL-C or kill the process to stop.

-Client

Without the 
-w
 worker flag, the gearman tool will run as a client. By default it submitsa foreground job and waits for the response. You can also start background jobs by specifying the 
-b
 flag. The workloadfor the job can be given after all options, or piped into the process like other shell utilities. For example:
$ gearman -f wc < /etc/passwd
26
2.Install gearman php extension(1)添加php扩展即PECL,需要相应的工具pecl,pecl与pear关系紧密,下面就是安装pear和pecl请参照官方文档操作 http://pear.php.net/manual/en/installation.getting.php download go-pear.phar到/usr/local,这个位置是放置非系统程序的地方。cd /usr/local/
$ curl -O http://pear.php.net/go-pear.phar $ php -d detect_unicode=0 go-pear.phar
[/code]type 1 and return
/usr/local/pear
type 4 and return
/usr/local/bin
return
pear version
现在,pear,pecl都安装好了。但是还可能存在一些问题,基本可以参考下面这个链接:http://pear.php.net/manual/en/installation.checking.php#installation.checking.cli.modifyingphpini(2)add gearman extension control (/usr/lib/php/extensions这可能就是php扩展放置的位置,I am not sure)$ sudo pecl install gearmanterminal中输出了下面这些话,看起来不太妙!>>>>>>>>>>>>>>>downloading gearman-1.1.2.tgz ...Starting to download gearman-1.1.2.tgz (30,961 bytes).........done: 30,961 bytes3 source files, buildingrunning: phpizeConfiguring for:PHP Api Version:         20121113Zend Module Api No:      20121212Zend Extension Api No:   220121212Cannot find autoconf. Please check your autoconf installation and the$PHP_AUTOCONF environment variable. Then, rerun this script.<<<<<<<<<<<<<<<<所以,我们又遇到了另一个问题,解决方法是安装autoconf。brew install autoconf   可以安装,但是无法link到外部Error: The `brew link` step did not complete successfullyThe formula built, but is not symlinked into /usr/localCould not symlink share/info/autoconf.info/usr/local/share/info is not writable.所以,pecl install 还是无法使用,无法通过pecl安装gearman extension control。抛弃上面这种autoconf的安装方法,将/usr/local/Cellar中的autoconf文件删掉。(最后发现并不是这种安装方式不对,而且OS X对特定路径进行了写限制,即使是root也不行。解决方法在后面,可以使用后面的方法直接在这里把问题解决,这个括号是后来补充的。)(3)山重水复疑无路,柳暗花明又一村,只要上Google,总是能找到解决方法。https://coolestguidesontheplanet.com/install-mcrypt-php-mac-osx-10-10-yosemite-development-server/这篇文章讲解了安装php extension的整个流程,包括一些必备条件,虽然不是gearman。
cd /usr/local/
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz[/code] 
tar xvfz autoconf-latest.tar.gz
cd autoconf-2.69/
./configure
make
sudo make install
安装完成后删掉没用的文件。再次执行$ sudo pecl install gearman,基本成功,但是又出现一个问题。ERROR: failed to write /usr/lib/php/extensions/no-debug-non-zts-20121212/gearman.so (copy(/usr/lib/php/extensions/no-debug-non-zts-20121212/gearman.so): failed to open stream:Operation not permitted)疑问:为什么使用了sudo还是没有权限? 因为OS X对诸如/usr/lib一类的路径进行了特别保护。破解方法:restart mac, press COMMAND+R,enter recovery-mode. Select Utilities->Terminal, and type "csrutil disable", finally restart.现在我们执行$ sudo pecl install gearman,便没有错误了,最后You should add "extension=gearman.so" to php.ini。如果你发现你的mac自带的php,可能找不到php.ini这个文件,没关系复制一份就好了,可能需要重启服务器。$ sudo cp /etc/php.ini.default  /etc/php.ini  $ sudo apachectl restart-----------------另外,这些php extensions也可以不经过pecl自己手动安装,下载你想使用的extension的tarball,解压后可以看到一个文件config.m4,这是phpize执行时需要的文件。phpize是干什么的?phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块。在解压后的根目录执行phpize,然后执行以下步骤:
./configure
make
sudo make install
最后同样是add "extension=gearman.so" to php.ini。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php gearman pear pecl