Gearman简单介绍

2024-05-01 17:49
文章标签 简单 介绍 gearman

本文主要是介绍Gearman简单介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自: http://www.cppblog.com/guojingjia2006/archive/2012/12/27/196738.html

Gearman是一个分布式的任务调度框架,它包括 a client,a worker ,a job server这三部分组成。

Gearman的执行过程:客户端通过客户端API(PHP,C,Perl等)创建一个任务发送到job server上,Job Server 通过客户端的function name 查找合适的worker,并分到该worker上,worker接收到任务后根据worker的规则执行,并返回数据到job Server,而Job Server则把数据返回给客户端,这样Gearman的执行过程就结束了。

用户可以根据不同的需求制定不同的worker来处理不同的任务,将这些worker存放到不同的服务器上,Job Server会根据不同的客户端发送来的任务的function name寻找worker来执行,从而达到为业务服务器减轻压力;

Gearman的安装:

下载http://launchpad.net/gearmand/trunk/0.12/+download/gearmand-0.12.tar.gz



[falcon@www-001  ~/src/]$ wget  http://launchpad.net/gearmand/tr ... earmand-0.12.tar.gz
[falcon@www-001  ~/src/]$ cd gearmand-0.12

[falcon@www-001  ~/src/gearmand-0.12]$ ./configure --prefix=/home/falcon/gearmand

[falcon@www-001  ~/src/gearmand-0.12]$ make && make instal

运行gearman 的job Server


[falcon@www-001  ~/src/gearmand-0.12]$ cd ~/gearmand

[falcon@www-001  ~/gearmand]$ ls
bin  include  lib  sbin  share
[falcon@www-001  ~/gearmand]$ sbin/gearmand --help
gearmand 0.12 -  https://launchpad.net/gearmand
usage: sbin/gearmand 
[OPTIONS]Main Options:
-b, --backlog=BACKLOG       Number of backlog connections for listen. 
-d, --daemon                Daemon, detach and run in the background. 
-f, --file-descriptors=FDS  Number of file descriptors to allow for the process                             
(total connections will be slightly less). Default     is max allowed for user. 
-h, --help                  Print this help menu. 
-j, --job-retries=RETRIES   Number of attempts to run the job before the job  server removes it. Thisis helpful to ensure a bad  job does not crash all available workers. Default  is no limit. 
-l, --log-file=FILE         Log file to write errors and information to. Turning this option on also forces the first  verbose level to be enabled. 
-L, --listen=ADDRESS        Address the server should listen on. Default is  INADDR_ANY. 
-p, --port=PORT             Port the server should listen on. 
-P, --pid-file=FILE         File to write process ID out to. 
-r, --protocol=PROTOCOL     Load protocol module. 
-R, --round-robin           Assign work in round-robin order per  workerconnection. The default is to assign work in  the order of functions added by the worker. 
-q, --queue-type=QUEUE      Persistent queue type to use. 
-t, --threads=THREADS       Number of I/O threads to use. Default=0. 
-u, --user=USER             Switch to given user after startup. 
-v, --verbose               Increase verbosity level by one. 
-V, --version               Display the version of gearmand and exit. 
-w, --worker-wakeup=WORKERS Number of workers to wakeup for each job received.   The default is to wakeup all available workers.



运行Job Server服务

[falcon@www-001  ~/gearmand]$ sbin/gearmand -d



判断gearmand是否运行

[falcon@www-001  ~/gearmand]$ ps -ef|grep gearmand

falcon    9083     1  0 02:46 ?        00:00:00 sbin/gearmand -d -vv

falcon    9112 28298  0 02:47 pts/1    00:00:00 grep gearmand

[falcon@www-001  ~/gearmand]$ netstat -an -t|grep 4730

tcp        0      0 0.0.0.0:4730                0.0.0.0:*                   LISTEN  



到此Job Server运行正常,下面我们可以简单的在本地上测试Worker和Client是否能够正常接收任务

我们这里用gearman命令来测试

[falcon@www-001  ~/gearmand]$ bin/gearman --help

bin/gearman: invalid option -- -

Client mode: bin/gearman [options] [<data>]

Worker mode: bin/gearman -w [options] [<command> [<args> ...]]

公共参数区

Common options to both client and worker modes.  

        -f <function> - Function name to use for jobs (can give many)处理任务的函数名

        -h <host>     - Job server host  (Job Server主机,默认是localhost)

        -H            - Print this help menu

        -p <port>     - Job server port (Job Server端口,默认是4730)

        -t <timeout>  - Timeout in milliseconds  (执行多长时间超时,微秒)

        -i <pidfile>  - Create a pidfile for the process (创建进程的pid文件)

Client部分参数

Client options:

        -b            - Run jobs in the background (后台运行任务)

        -I            - Run jobs as high priority (高优先级运行任务)

        -L            - Run jobs as low priority (低优先级运行任务)

        -n            - Run one job per line (逐行执行任务)

        -N            - Same as -n, but strip off the newline  

        -P            - Prefix all output lines with functions names (在输入结果前面加处理的函数名)

        -s            - Send job without reading from standard input 执行任务不返回结果

        -u <unique>   - Unique key to use for job 任务的唯一标识

Worker部分参数

Worker options:

        -c <count>    - Number of jobs for worker to run before exiting (统计worker进程处理多少个任务后中止)

        -n            - Send data packet for each line

        -N            - Same as -n, but strip off the newline

        -w            - Run in worker mode   以worker模式运行




示例一、以命令行方式模拟worker 和 client来处理任务

开启一个worker,以function name 为 tongji 来处理输入的数据,统计行数并返回结果

[falcon@www-001  ~/gearmand]$ bin/gearman -w -f tongji -- wc -l  

模拟客户端连接到Job Server,以tongji函数名来提交一个文件,并接收结果

[falcon@www-001  ~/gearmand]$ bin/gearman -f tongji  < /etc/profile

54




示例二、利用Gearman的php扩展来测试Gearman



安装PHP的Gearman扩展模块

[falcon@www-001  ~/src]$ wget  http://pecl.php.net/get/gearman-0.7.0.tgz

[falcon@www-001  ~/src]$ cd gearman-0.7.0

[falcon@www-001  ~/src/gearman-0.7.0]$ /home/falcon/php/bin/phpize

......

[falcon@www-001  ~/src/gearman-0.7.0]$ ./configure \

--with-php-config=/home/falcon/php/bin/php-config --with-gearman=/home/falcon/gearmand

[falcon@www-001  ~/src/gearman-0.7.0]$ make && make install

将gearman.so加入到php.ini配置文件使其生效

测试php是否加载gearman模块



[falcon@www-001  ~/php/bin]$ php -m|grep gearman




官方示例:

将提交的字符串翻转后返回



Worker :worker_reverse.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());

}

?>


运行worker

$php work_reverse.php &




Client:client_reverse.php



<?php

$client= new GearmanClient();

$client->addServer();

print $client->do("reverse", "Hello World!");

?>


执行client_reverse.php



$ php client_reverse.php

!dlroW olleH

参考资料:


http://gearman.org/index.php?id=getting_started


http://pecl.php.net/package/gearman


http://www.ibm.com/developerworks/cn/opensource/os-php-gearman/

这篇关于Gearman简单介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952300

相关文章

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

Python实现html转png的完美方案介绍

《Python实现html转png的完美方案介绍》这篇文章主要为大家详细介绍了如何使用Python实现html转png功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 1.增强稳定性与错误处理建议使用三层异常捕获结构:try: with sync_playwright(

Java使用多线程处理未知任务数的方案介绍

《Java使用多线程处理未知任务数的方案介绍》这篇文章主要为大家详细介绍了Java如何使用多线程实现处理未知任务数,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 知道任务个数,你可以定义好线程数规则,生成线程数去跑代码说明:1.虚拟线程池:使用 Executors.newVir

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis