Home | 簡體中文 | 繁體中文 | 雜文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品與服務 | Email

Gearman + PHP 實現分散式對象調用

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

Mr. Neo Chen (陳景峯), netkiller, BG7NYT


中國廣東省深圳市龍華新區民治街道溪山美地
518131
+86 13113668890


$Id: 9c398ce66f66eef0683cd940da22340dbed1c671

版權聲明

轉載請與作者聯繫,轉載時請務必標明文章原始出處和作者信息及本聲明。

文檔出處:
http://netkiller.github.io
http://netkiller.sourceforge.net

微信掃瞄二維碼進入 Netkiller 微信訂閲號

QQ群:128659835 請註明“讀者”

2016-03-29

摘要

在群裡看到有網友問,IDC的伺服器是否需要開啟防火牆,我意識到應該寫一篇關於IDC安全的文章。


目錄

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 <<EOF
extension=gearman.so
EOF
		
		

確認模組是否安裝,同時檢查gearman擴展的版本。

# php -m | grep gearman
gearman

# php -r 'printf("%s\r\n",gearman_version());'
1.1.8
		

測試腳本 server.php

		
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", "my_reverse_function");
while ($worker->work());

function my_reverse_function($job)
{
  return strrev($job->workload());
}
?>		
		
		

測試腳本 client.php

		
<?php
$client= new GearmanClient();
$client->addServer();
print $client->doNormal("reverse", "Hello World!");
?>
		
		

我用'o' 表示與上次備份中有差異的部分。

3. 參數傳遞與返回值

Gearman 向functon 傳遞參數只能通過$job->workload(), 而 $job->workload() 只能傳遞字元串。

如果托傳遞多個參數,需要將參數序列化後傳遞

返回值也一樣,一個字元串可以直接返回,如果返回數字類型是不允許的,需要序列化處理

例 1. 多參數傳遞與返回值實例

Server

			
<?php
require 'Doctrine/Common/ClassLoader.php';

use Doctrine\Common\ClassLoader;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;

$classLoader = new ClassLoader('Doctrine', '/www/DoctrineDBAL-2.3.4/');
$classLoader->register();

$config = new Configuration();

$connectionParams = array(
    'dbname' => 'example',
    'user' => 'www',
    'password' => 'password',
    'host' => '192.168.2.1',
    'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);

$host = '127.0.0.1';
$port = 4730;

echo "Starting\n";

# Create our worker object.
$gmworker= 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() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function members($job)
{
	global  $conn;
	$param = unserialize($job->workload());
	print_r($param);
	$sql = "SELECT username FROM members limit ".$param['limit'].",".$param['offset'];
	$stmt = $conn->query($sql);

	while ($row = $stmt->fetch()) {
		//printf("%s\r\n", );
		$result[] = $row['username'];
	}
	return serialize($result);
}
			
			

Client

			
<?php

# create our client object
$gmclient= new GearmanClient();

# add the default server (localhost)
$gmclient->addServer();

# run reverse client in the background
$job_handle = $gmclient->doNormal("members",serialize(array('limit'=>5,'offset'=>10)));

if ($gmclient->returnCode() == GEARMAN_SUCCESS)
{
	print_r(unserialize($job_handle));
}		
				
			

運行結果

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

comments powered by Disqus