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

相关文章

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf

Redis的安全机制详细介绍及配置方法

《Redis的安全机制详细介绍及配置方法》本文介绍Redis安全机制的配置方法,包括绑定IP地址、设置密码、保护模式、禁用危险命令、防火墙限制、TLS加密、客户端连接限制、最大内存使用和日志审计等,通... 目录1. 绑定 IP 地址2. 设置密码3. 保护模式4. 禁用危险命令5. 通过防火墙限制访问6.

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has