当前位置:网站首页>分布式的任务分发框架-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
边栏推荐
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
- Basic grammar of interview (Part 2)
- Leetcode 108 converts an ordered array into a binary search tree -- recursive method
- STM32 realizes 74HC595 control
- vs2013已阻止安装程序,需安装IE10
- Servlet specification Part II
- link preload prefetch
- QT adjust win screen brightness and sound size
- QT learning diary 9 - dialog box
- [vscode] convert tabs to spaces
猜你喜欢
Kubernetes resource object introduction and common commands (III)
互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
问题随记 —— 在 edge 上看视频会绿屏
鸿蒙第四次培训
TensorBoard快速入门(Pytorch使用TensorBoard)
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
Select 3 fcpx plug-ins. Come and see if you like them
[UE4] brush Arctic pack high quality Arctic terrain pack
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
【JokerのZYNQ7020】DDS_ Compiler。
随机推荐
PHP returns 500 errors but no error log - PHP return 500 error but no error log
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
link preload prefetch
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
c# . Net tool ecosystem
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
Wechat applet for the first time
[mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
The difference between get and post
List的stream中Long对象与long判等问题记录
Leetcode540: a single element in an ordered array
鸿蒙第三次培训
A day's work list of an ordinary programmer
Getting started with deops
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
Kotlin的协程:上下文
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]