热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Gearman+PHP实现分布式对象调用

GearmanPHP实现分布式对象调用http:netkiller.github.iojournalgearman.php.htmlMr.NeoChen(netkiller),陈景



Gearman + PHP 实现分布式对象调用




http://netkiller.github.io/journal/gearman.php.html





MrNeo Chen (netkiller)陈景峰(BG7NYT)





中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890
+86 755 29812080







$Id$




版权 © 2011, 2012, 2013 http://netkiller.github.io




$Date$





摘要


在群里看到有网友问,IDC的服务器是否需要开启防火墙,我意识到应该写一篇关于IDC安全的文章。







我的系列文档




Netkiller Architect 手札Netkiller Developer 手札Netkiller PHP 手札Netkiller Python 手札Netkiller Testing 手札Netkiller Cryptography 手札
Netkiller Linux 手札Netkiller CentOS 手札Netkiller FreeBSD 手札Netkiller Security 手札Netkiller Version 手札Netkiller Web 手札
Netkiller Monitoring 手札Netkiller Storage 手札Netkiller Mail 手札Netkiller Shell 手札Netkiller Network 手札Netkiller Database 手札
Netkiller PostgreSQL 手札Netkiller MySQL 手札Netkiller NoSQL 手札Netkiller LDAP 手札Netkiller Cisco IOS 手札Netkiller H3C 手札
Netkiller Multimedia 手札Netkiller Docbook 手札Netkiller 开源软件 手札 

 



目录


1. Gearman Job Server
2. Gearman PHP扩展
3. 参数传递与返回值



1. Gearman Job Server



文本格式回复


yum install gearmand




2. Gearman PHP扩展



PHP扩展安装



#!/bin/bash
yum install libgearman-devel -y
pecl install gearman
cat >> /srv/php/etc/conf.d/gearman.ini <extension&#61;gearman.so
EOF


确认模块是否安装&#xff0c;同时检查gearman扩展的版本。


# php -m | grep gearman
gearman
# php -r &#39;printf("%s\r\n",gearman_version());&#39;
1.1.8

测试脚本 server.php



$worker&#61; new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", "my_reverse_function");
while ($worker->work());
function my_reverse_function($job)
{
return strrev($job->workload());
}
?>


测试脚本 client.php



$client&#61; new GearmanClient();
$client->addServer();
print $client->doNormal("reverse", "Hello World!");
?>


我用&#39;o&#39; 表示与上次备份中有差异的部分。



 




3. 参数传递与返回值



Gearman 向functon 传递参数只能通过$job->workload(), 而 $job->workload() 只能传递字符串。


如果托传递多个参数&#xff0c;需要将参数序列化后传递


返回值也一样&#xff0c;一个字符串可以直接返回&#xff0c;如果返回数字类型是不允许的&#xff0c;需要序列化处理




例 1. 多参数传递与返回值实例



Server



require &#39;Doctrine/Common/ClassLoader.php&#39;;
use Doctrine\Common\ClassLoader;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;
$classLoader &#61; new ClassLoader(&#39;Doctrine&#39;, &#39;/www/DoctrineDBAL-2.3.4/&#39;);
$classLoader->register();
$config &#61; new Configuration();
$connectionParams &#61; array(
&#39;dbname&#39; &#61;> &#39;example&#39;,
&#39;user&#39; &#61;> &#39;www&#39;,
&#39;password&#39; &#61;> &#39;password&#39;,
&#39;host&#39; &#61;> &#39;192.168.2.1&#39;,
&#39;driver&#39; &#61;> &#39;pdo_mysql&#39;,
);
$conn &#61; DriverManager::getConnection($connectionParams, $config);
$host &#61; &#39;127.0.0.1&#39;;
$port &#61; 4730;
echo "Starting\n";
# Create our worker object.
$gmworker&#61; new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer($host, $port);
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("members", "members");
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() !&#61; GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function members($job)
{
global $conn;
$param &#61; unserialize($job->workload());
print_r($param);
$sql &#61; "SELECT username FROM members limit ".$param[&#39;limit&#39;].",".$param[&#39;offset&#39;];
$stmt &#61; $conn->query($sql);
while ($row &#61; $stmt->fetch()) {
//printf("%s\r\n", );
$result[] &#61; $row[&#39;username&#39;];
}
return serialize($result);
}


Client



# create our client object
$gmclient&#61; new GearmanClient();
# add the default server (localhost)
$gmclient->addServer();
# run reverse client in the background
$job_handle &#61; $gmclient->doNormal("members",serialize(array(&#39;limit&#39;&#61;>5,&#39;offset&#39;&#61;>10)));
if ($gmclient->returnCode() &#61;&#61; GEARMAN_SUCCESS)
{
print_r(unserialize($job_handle));
}


运行结果


$ php client.php
Array
(
[0] &#61;> 257000005
[1] &#61;> 257000006
[2] &#61;> 257000009
[3] &#61;> 257000010
[4] &#61;> 257000011
[5] &#61;> 257000012
[6] &#61;> 257000013
[7] &#61;> 257000014
[8] &#61;> 257000015
[9] &#61;> 257000016
)






推荐阅读
author-avatar
国国国国涛
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有