gearmand 编译 could not find gperf
安装步骤:
#wget https://launchpad.net/gearmand/1.2/1.1.8/+download/gearmand-1.1.8.tar.gz
#tar zxvf gearmand-1.1.8.tar.gz
#cd gearmand-1.1.8
#./configure
在configure过程中出现了以下错误:
checking for Boost headers version >= 1.39.0… no
configure: error: cannot find Boost headers version >= 1.39.0
解决办法:
# yum search boost
# yum install boost.x86_64
# yum install boost-devel.x86_64
继续执行./configure出现以下错误
checking for gperf... no
configure: error: could not find gperf
解决办法:
#yum search gperf
#yum install gperf.x86_64
再次执行成功
#make
#make install
检测是否安装成功
#gearman
出现以下错误
gearman: error while loading shared libraries: libgearman.so.8: cannot open shared object file: No such file or directory
这表示系统不知道libgearman.so.8 放在哪个目录下。 要在/etc/ld.so.conf中加入libgearman.so.8所在的目录。 检查了下,文件所在目录为/usr/local/lib,且该目录在/etc/ld.so.conf文件中。 因此出现这个问题的原因是libgearman.so.8刚生成,没有加入到ld.so.cache中,所以这时需要重新运行一下 /sbin/ldconfig(ldconfig命令的作用)
再次运行gearman命令
#gearman
gearman Error in usage(No Functions were provided).
Client mode: gearman [options] [<data>]
Worker mode: gearman -w [options] [<command> [<args> ...]]
Common options to both client and worker modes.
-f <function> - Function name to use for jobs (can give many)
-h <host> - Job server host
-H - Print this help menu
-v - Print diagnostic information to stdout(false)
-p <port> - Job server port
-t <timeout> - Timeout in milliseconds
-i <pidfile> - Create a pidfile for the process
Client options:
-b - Run jobs in the background(false)
-I - Run jobs as high priority
-L - Run jobs as low priority
-n - Run one job per line(false)
-N - Same as -n, but strip off the newline(false)
-P - Prefix all output lines with functions names
-s - Send job without reading from standard input
-u <unique> - Unique key to use for job
Worker options:
-c <count> - Number of jobs for worker to run before exiting
-n - Send data packet for each line(false)
-N - Same as -n, but strip off the newline(false)
-w - Run in worker mode(false)
Gearman安装成功!
Gearman PHP扩展安装
#wget http://pecl.php.net/get/gearman-1.1.1.tgz
#tar zxvf gearman-1.1.1.tgz
#phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
修改php.ini文件,将extension = gearman.so添加到配置文件,用php -m | grep gearman检查扩展是否安装成功
Gearman使用方法(原文地址)
编写client和worker端 client.php
<?php
$client= new GearmanClient();
$client->addServer(“127.0.0.1″, 4730);
print $client->do(“title”, “Linvo”);
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);
}
?>
准备工作已经完毕,试验开始
1、启动job
gearmand -d
2、启动worker
php -c /etc/php5/apache2/php.ini worker.php
3、启动client(新开终端中打开)
php -c /etc/php5/apache2/php.ini client.php
屏幕显示字符串的长度 “5”
这里,有几点需要说明一下:
1、这里直接用php cli方式运行,添加-c参数是为了加载php.ini配置文件,以加载gearman扩展
2、worker应该做成守护进程(CLI模式),可以开启多个,这样client发起的任务就会分发到各个worker分别来执行(自动负载均衡)
这个例子由于太过简单,即使开启多个worker也无法看出效果,不过可以通过终止其中一个,可以看出系统自动切换到其他worker继续正常执行
3、同理,client也是可以开启多个的(模型请参考之前的那边日志)
4、同时,job也可以开启多个,以避免单点故障
简易实作
接下来,我们可以试着用 PHP API 来连接 Job Server 。前面安装好 PECL 的 Gearman Extension 后,我们就可以在 PHP 程式裡建立操作 Gearman API 的物件了。
以下我用简单的方式来模拟 Client 和 Worker 的运作,所以这裡 Client 和 Worker 会在同一部主机上,但实际运作时是不需要的,请大家注意。
Client 端程式
<?php
$client= new GearmanClient();
$client->addServer("127.0.0.1", 4730);
$who = array('who_send'=>'web','get_email'=>'');
$img['image'] = '/var/www/pub/image/test.png';
print $client->do("sendEmail", serialize($who));
print "\n";
print $client->do("resizeImage", serialize($img));
print "\n";
?>
Worker 端程式
<?php
$worker = new GearmanWorker();
$worker->addServer(); // 预设为 localhost
$worker->addFunction('sendEmail', 'doSendEmail');
$worker->addFunction('resizeImage', 'doResizeImage');
while($worker->work()) {
sleep(1); // 无限回圈,并让 CPU 休息一下
}
function doSendEmail($job)
{
$data = unserialize($job->workload());
print_r($data);
sleep(3); // 模拟处理时间
echo "Email sending is done really.\n\n";
}
function doResizeImage($job)
{
$data = unserialize($job->workload());
print_r($data);
sleep(3); // 模拟处理时间
echo "Image resizing is really done.\n\n";
}
首先, PHP Gearman Extension 提供了一个名为 GearmanClient 的类别,它可以让程式安排工作给 Job Server 。
而 addServer 方法表示要通知的是哪些 Job Server ,也就是说如果有多台 Job Server 的话,就可以透过 addServer 新增。
然后我们将要呼叫哪个 Worker 以及该 Worker 所需要的资料,利用 GearmanClient 的 doBackground 方法传送过去。 doBackground 方法顾名思义就是在背景执行, Client 在丢出需求后就可以继续处理其他的程式,也就是我们常说的「射后不理」。
doBackground 方法的第一个参数是告诉 Job Server 要执行哪个功能,而这个功能则是由 Worker 提供的;要注意是,这个参数只是识别用的,并不是真正的函式名称。而第二个参数是要传给 Worker 的资料,它必须是个字串;因此如果要传送的是阵列的话,我们就要用 PHP 的 serialize 函式来对这些资料做序列化。
PHP 的 Gearman Extension 也提供了一个 GearmanWorker 类别,让我们可以实作 Worker 。而 GearmanWorker 类别也提供了addServer 方法,让所生成的 Worker 物件可以注册到 Job Server 中。
另外 GearmanWorker 类别也提供了 addFuncton 方法,告诉 Job Server 自己可以处理哪些工作。 addFunction 的第一个参数就是对应到 GearmanClient::doBackground 方法的第一个参数,也就是功能名称;这使得 Client 和 Worker 能透过这个名称来互相沟通。而第二个参数则是一个callback函式,它会指向真正应该要处理该工作的函式或类别方法等。
最后因为 Worker 因为要随时准备服务,是不能被中断的,因此我们透过一个无限迴圈来让它常驻在 Job Server 中。
测试
准备好 Client 和 Worker 的程式后,就可以测试看看了。首先我们必须得先执行 worker.php ,让它开始服务。
php worker.php
这时我们会看到 worker.php 停驻在等待服务。
接着我们开启另一个 console 视窗来执行 client.php :
切换到执行 worker.php 的 console 时,就会看到以下执行结果:
Array
(
[who_send] => web
[get_email] =>
)
Email sending is really done.
Array
(
[image] => /var/www/pub/image/test.png
)
Image resizing is really done.
这表示 Worker 正常地处理 Client 的需求了。
现在试着把 worker.php 停掉 (Ctrl+C) ,然后再执行 client.php ,大家应该会发现 client.php 还是正常地完成它的工作;这是因为 Job Server 帮我们把需求先放在 Queue 裡,等待 Worker 启动后再处理。
这时可以查看 MySQL 的 gearman 资料库,在 gearman_queue 资料表中应该就会看到以下结果:
这表示 Job Server 成功地将 Queue 保留在 MySQL 资料表中。
接着再执行 worker.php ,这时 Job Server 会得知 Worker 复活,赶紧将 Queue 裡面属于该 Worker 应该执行的工作再发送出去以完成作业;而 Worker 完成作业后, Job Server 就会把 Queue 清空了。
Message Queue 这个架构的应用可以说相当广泛,尤其在大流量的网站上,我们能透过它来来有效运用分散式的系统架构,以处理更多使用者的需求。
而目前 Gearman 可说是在 PHP 上一个很棒的 Message Queue 支援套件,而且 API 也相当完善;因此如果能善用 Gearman 的话,那么我们在 PHP 网站的架构上就可以有更大的延展性,也能有更多的可能性。
gearmand 编译 could not find gperf的更多相关文章
- gearmand 编译 Unable to find libevent
如果出现configure: error: Unable to find libevent,则输入命令:yum -y install libevent libevent-devel然后重新config ...
- vs2010 编译Qt5.2 rc1
首先要准备一些依赖: 下载Qt 5.2.0 rc版的源码 qt-everywhere-opensource-src-5.2.0-rc1.7z 并解压出来, 我的路径为D:\qt5\qt-src-5.2 ...
- android编译遇到问题修改
(注意要确定安装了jdk) 第一步: cd lichee; ./build.sh -p sun5i_elite -k 3.0 (apt-get install uboot-mkimage需要安装 ...
- PHP 安装使用 gearman
1.安装服务器端: 官方下载,请到https://launchpad.net/gearmand. yum install boost-devel* gperf* libevent-devel* lib ...
- PHP-Gealman
一.简介 Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manu ...
- gearman的安装与使用
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manual/zh ...
- centos6.5编译安装gearmand Job Server(C)
1)下载安装包: wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz 2)安装编译器: yu ...
- Gearmand 任务分发系统
简介: Gearmand 是一个用来把工作委派给其它机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来调用其它语言的函数的系统. 简单来讲,就是客户端程序把请求提 ...
- 编译CM14.1(sudmod71.1)过程记录
编译CM14.1内存要求很高,至少8G以上,我的6G也可以搞定,交换空间分配大一点. 1.安装平台 建议安装Deepin 15.3桌面版系统,系统UI好看. 2.配置环境 (1)安装编译依赖库 sud ...
随机推荐
- CDN working principle diagram
转自 https://cloud.tencent.com/developer/article/1358553
- python 变量,输入,输出
目录 2.0 注释 2.1 变量 2.2 变量名命名规范 2.3 常量 2.4 输入 input 2.5 输出 print 2.6 关于开发工具 2.0 注释 python的注释方法 "&q ...
- 唯快不破:Web 应用的 13 个优化步骤
时过境迁,Web 应用比以往任何时候都更具交互性.搞定性能可以帮助你极大地改善终端用户的体验.阅读以下的技巧并学以致用,看看哪些可以用来改善延迟,渲染时间以及整体性能吧! 更快的 Web 应用 优化 ...
- NoSuchMethodError idea解决jar包冲突
报NoSuchMethodError(使用spring boot框架idea)一般是jar包冲突 Exception in thread"main" java.lang.NoSuc ...
- 我的省选 Day -10
Day -10 今天的分数也许会比昨天更低.. 感觉2017年比远古时代的2007年的第一试难诶. 估个分数好了,我猜88分(为什么猜了一个这么吉利的数字??到时候出来没几分就啪啪啪打脸了) 和昨天一 ...
- [AHOI2009]飞行棋 BZOJ1800
题目描述 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. 输入输出格式 输入格式: 第一行为 ...
- 黑匣子_NOI导刊2010提高(06) Splay Tree
题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...
- 未找到与约束 Micorosoft.CodeAnalysis.Editor.TypeScript.ToolsOptions.IUserSettingsProvider
问题: 未找到与约束 ContractName Micorosoft.CodeAnalysis.Editor.TypeScript.ToolsOptions.IUserSettingsProvide ...
- Linux--5 mariadb和redis的安装
一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...
- Appium + Python自动化3 - 输入中文
在做app自动化过程中会踩很多坑,咱们都是用中文的app,所以首先要解决中文输入的问题!本篇通过屏蔽软键盘,绕过手机的软键盘方法,解决中文输入问题. 一.定位搜索 1.打开淘宝点击搜索按钮,进入搜索页 ...