当前位置:网站首页>分布式的任务分发框架-Gearman
分布式的任务分发框架-Gearman
2022-07-03 17:46:00 【星哥玩云】
官方文档:http://gearman.org/getting-started/ 安装方法和示例都有,可以详细看一下。
Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相比,Gearman更偏向于任务分发功能。它的任务分布非常简单,简单得可以只需要用脚本即可完成。Gearman最初用于LiveJournal的图片resize功能,由于图片resize需要消耗大量计算资源,因此需要调度到后端多台服务器执行,完成任务之后返回前端再呈现到界面。
Gearman可以做什么
- 异步处理:图片处理,订单处理,批量邮件/通知之类的
- 要求高CPU或内存的处理:大容量的数据处理,MapReduce运算,日志聚集,视频编码
- 分布式和并行的处理
- 定时处理:增量更新,数据复制
- 限制速率的FIFO处理
- 分布式的系统监控任务
1,安装方法
#安装gearman
yum install gearmand
#安装libgearman
yum install libgearman-devel
#安装gearman php扩展
pecl install gearman
#添加gearman.so到 php.ini
echo "extension=gearman.so" >> /etc/php.ini没有pecl的话装一下
yum install php-pear如果pecl没法安装的话,可以直接下载源代码,使用phpize编译安装
cd ~/software
wget http://pecl.php.net/get/gearman-1.1.2.tgz
cd gearman-1.1.2
phpize
./configure
make && make install 2,简单使用示例:(我们使用Gearman来异步处理百度云推送服务)
先看client.php注册事件,client.php
<?php
// 创建Gearman对象
$client = new GearmanClient();
// addServer默认是localhost,端口默认是4730,如果不是默认的话可以调整
$client -> addServer();
// $client->addServer("192.168.0.0",4730);
echo "Sending job\n";
$username = "test";
$message = "message";
$data = array(
"username" => $username,
"message" => $message ,
);
// 注册事件 以及 传递参数 , 多个参数使用json_encode转换
// 任务可以阻塞式运行,还可以指定优先级 ;当然,也可以非阻塞(运行不等待结果)运行
// 可以参考php Gearman api文档 : doNornal, doHigh, doLow,doBackground
$result = $client -> doBackground("testFunction", json_encode($data));
if ($result) {
echo "Success: $result\n";
}
?> 再看worker的处理,其实就是注册事件处理函数 , worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
// 注册事件及事件处理函数
$worker->addFunction("testFunction","handler");
// 运行worker
while ($worker->work());
function handler(GearmanJob $job) {
$workload = json_decode($job->workload());
echo "received: " . print_r($workload,1);
}
?>可以命令行运行 php client.php , php worker.php ,这里启动顺序最好是先启动worker,这样事件可以得到及时处理;也可以先启动client,事件会在Gearman中排队,等待worker处理;
还可以启动多个worker,Gearman会自动进行负载均衡,分配到不同的worker进行处理。
大家可以发掘更多应用场景...
例如:通过Gearman实现MySQL到Redis的数据同步(异步复制) http://www.linuxidc.com/Linux/2015-01/111380.htm
Gearman的监控:
可以使用supervisor,也可以使用gearman manager
使用supervisor监控Gearman任务的例子见:http://www.linuxidc.com/Linux/2015-01/111384.htm
边栏推荐
- PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
- 自动渗透测试工具核心功能简述
- The difference between get and post
- Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
- [RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
- Deops入门
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- [UE4] brush Arctic pack high quality Arctic terrain pack
- How to deploy applications on kubernetes cluster
- Play with fancy special effects. This AE super kit is for you
猜你喜欢

Leetcode 108 converts an ordered array into a binary search tree -- recursive method

Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028

List的stream中Long对象与long判等问题记录

The third day of writing C language by Yabo people

TensorBoard快速入门(Pytorch使用TensorBoard)

Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028

微服务组件Sentinel控制台调用

Draw some simple graphics with MFC

TCP congestion control details | 3 design space

How to deploy applications on kubernetes cluster
随机推荐
自动渗透测试工具核心功能简述
Servlet specification Part II
Where is the monitoring page of RDS database?
[combinatorics] generating function (linear property | product property)
Golang unit test, mock test and benchmark test
i++与++i的区别:通俗易懂的讲述他们的区别
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
Embedded-c language-7
When absolutely positioned, the element is horizontally and vertically centered
Draw some simple graphics with MFC
聊聊支付流程的設計與實現邏輯
VM11289 WAService. js:2 Do not have __ e handler in component:
Hongmeng fourth training
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
QT学习日记9——对话框
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
AcWing 4489. 最长子序列
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
1146_ SiCp learning notes_ exponentiation
The third day of writing C language by Yabo people