当前位置:网站首页>分布式的任务分发框架-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
边栏推荐
- 【JokerのZYNQ7020】DDS_ Compiler。
- Stm32h7 Hal library SPI DMA transmission has been in busy solution
- The difference between i++ and ++i: tell their differences easily
- Kotlin's collaboration: Context
- [vscode] convert tabs to spaces
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- QT learning diary 9 - dialog box
- What is the difference between cloud server and cloud virtual machine
- Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
猜你喜欢

STM32实现74HC595控制

Kubernetes resource object introduction and common commands (III)
![[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

Applet setting multi account debugging

Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028

Automata and automatic line of non-standard design

Deops入门

MySQL has been stopped in the configuration interface during installation

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

UE4 official charging resources, with a total price of several thousand
随机推荐
问题随记 —— 在 edge 上看视频会绿屏
VM11289 WAService. js:2 Do not have __ e handler in component:
Five problems of database operation in commodity supermarket system
ArrayList分析3 : 删除元素
Golang单元测试、Mock测试以及基准测试
Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
TensorBoard快速入门(Pytorch使用TensorBoard)
[vscode] convert tabs to spaces
小程序 多tab 多swiper + 每个tab分页
Golang unit test, mock test and benchmark test
i++与++i的区别:通俗易懂的讲述他们的区别
[RT thread] NXP rt10xx device driver framework -- pin construction and use
Where is the database account used when running SQL tasks in data warehouse tasks configured
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
QT adjust win screen brightness and sound size
Enterprise custom form engine solution (XI) -- form rule engine 1
How to deploy applications on kubernetes cluster
Detailed explanation of common network attacks
Kotlin的协程:上下文
1146_ SiCp learning notes_ exponentiation