当前位置:网站首页>分布式的任务分发框架-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
边栏推荐
- List的stream中Long对象与long判等问题记录
- Loop through JSON object list
- 1164 Good in C
- 问题随记 —— 在 edge 上看视频会绿屏
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- Talk about the design and implementation logic of payment process
- Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
- Inheritance of ES6 class
- 聊聊支付流程的設計與實現邏輯
- 微服务组件Sentinel控制台调用
猜你喜欢
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack

Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition

Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028

Embedded-c language-7

Draw some simple graphics with MFC

Leetcode 669 pruning binary search tree -- recursive method and iterative method

【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use

How to install PHP on Ubuntu 20.04

Hongmeng fourth training
随机推荐
List的stream中Long对象与long判等问题记录
Comparison of kotlin collaboration + retro build network request schemes
Getting started with deops
TCP拥塞控制详解 | 3. 设计空间
QT学习日记9——对话框
PHP returns 500 errors but no error log - PHP return 500 error but no error log
PHP processing - watermark images (text, etc.)
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
Applet setting multi account debugging
TensorBoard快速入门(Pytorch使用TensorBoard)
[RT thread] NXP rt10xx device driver framework -- pin construction and use
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
List of financial products in 2022
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
聊聊支付流程的设计与实现逻辑
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
The third day of writing C language by Yabo people