Gearman(分布式任务调度框架)工作原理

How Does Gearman Work?

Gearman如何工作的?

(写在前面的话: 工作当中遇到用Gearman来异步发送消息给用户,对相关原理不太了解,于是查询官网,遂翻译如下,由于水平有限,难免有错误不足之处,欢迎指出,敬请见谅.)

A Gearman powered application consists of three parts: a client, a worker, and a job server. The client is responsible for creating a job to be run and sending it to a job server. The job server will find a suitable worker that can run the job and forwards the job on. The worker performs the work requested by the client and sends a response to the client through the job server. Gearman provides client and worker APIs that your applications call to talk with the Gearman job server (also known as gearmand) so you don't need to deal with networking or mapping of jobs. Internally, the gearman client and worker APIs communicate with the job server using TCP sockets. To explain how Gearman works in more detail, lets look at a simple application that will reverse the order of characters in a string. The example is given in PHP, although other APIs will look quite similar.

We start off by writing a client application that is responsible for sending off the job and waiting for the result so it can print it out. It does this by using the Gearman client API to send some data associated with a function name, in this case the function reverse. The code for this is (with error handling omitted for brevity):

我们开始, 写一个 客户端应用,负责发送 job任务, 并 等待返回结果 这样就可以把它打印出来. 它是通过调用Gearman 客户端接口发送一些数据和一个方法名来实现的,在此处,这个方法叫reverse. 代码如下(为了简洁起见,省略了错误处理部分): 

<?php
// Reverse Client Code
$client = new GearmanClient();
$client->addServer();
print $client->do("reverse", "Hello World!");

This code initializes a client class, configures it to use a job server with addServer (no arguments means use 127.0.0.1 with the default port), and then tells the client API to run the reverse function with the workload “Hello world!”. The function name and arguments are completely arbitrary as far as Gearman is concerned, so you could send any data structure that is appropriate for your application (text or binary). At this point the Gearman client API will package up the job into a Gearman protocol packet and send it to the job server to find an appropriate worker that can run the reverse function. Let's now look at the worker code:

这段代码初始化了GearmanClient()类的实例,让这个实例调用addServer方法去使用一个 任务服务器,(此处这个addServer方法没有携带参数,意味着它使用127.0.0.1作为默认端口),然后告诉 client API 去运行 reverse 方法, 以及 "Hellow world!" 工作流.在Gearman方面,这个(do方法内的参数的) 方法名 和 参数 都是完全随意的,所以你可以发送 对你的项目来说合适的 任何数据结构(文本或者二进制). 此时, Gearman client API 将打包这个 任务 进一个 Gearman协议包,并将它发送到 任务服务, 进而找到一个可以运行这个 reverse 方法的 合适worker. 我们现在来看看 worker 的代码:

<?php
// Reverse Worker Code
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", function ($job) {
  return strrev($job->workload());
});
while ($worker->work());

This code defines a function addFunction that takes a string and returns the reverse of that string. It is used by a worker object to register a function named reverse after it is setup to connect to the same local job server as the client. When the job server receives the job to be run, it looks at the list of workers who have registered the function name reverse and forwards the job on to one of the free workers. The Gearman worker API then takes this request, runs the function addFunction, and sends the result of that function back through the job server to the client.

这段代码定义了一个 addFunction 方法, 这个方法 接收 一个字符串, 并返回 这个字符串的倒序. 这个方法被一个 任务对象($worker) 调用, 用来注册一个 reverse 方法, 当它被用来当做客户端来连接 同样的当地服务服务器 之后. 当 任务服务器 接收到需要运行的任务时, 它会查询那些注册了 reverse方法的 workers(工人) 的列表, 将这个 任务 提交给空闲workers(工人)中的一个. 然后 Gearman worker API接受这个请求,运行 addFunction 方法, 并把这个方法的结果通过 任务服务器 发送回给 客户端.

As you can see, the client and worker APIs (along with the job server) deal with the job management and network communication so you can focus on the application parts. There a few different ways you can run jobs in Gearman, including background for asynchronous processing and prioritized jobs. See the documentation available for the various APIs for details.

就像你看到的那样,客户端和 worker API(工人接口)(还有 任务服务器(job server)) 会处理 任务管理 和 网络通信,所以你可以专注于应用/项目部分.你也可以通过一些不同的方式在Gearman中运行任务, 包括 异步处理 和 优先任务 背景.你可以查看更多接口的 文档 来获取详情.

How Is Gearman Useful?

The reverse example above seems like a lot of work to run a function, but there are a number of ways this can be useful. The simplest answer is that you can use Gearman as an interface between a client and a worker written in different languages. If you want your PHP web application to call a function written in C, you could use the PHP client API with the C worker API, and stick a job server in the middle. Of course, there are more efficient ways of doing this (like writing a PHP extension in C), but you may want a PHP client and a Python worker, or perhaps a MySQL client and a Perl worker. You can mix and match any of the supported language interfaces easily, you just need all applications to be able to understand the workload being sent. Is your favorite language not supported yet? Get involved with the project, it's probably fairly easy for either you or one of the existing Gearman developers to put a language wrapper on top of the C library.

The next way that Gearman can be useful is to put the worker code on a separate machine (or cluster of machines) that are better suited to do the work. Say your PHP web application wants to do image conversion, but this is too much processing to run it on the web server machines. You could instead ship the image off to a separate set of worker machines to do the conversion, this way the load does not impact the performance of your web server and other PHP scripts. By doing this, you also get a natural form of load balancing since the job server only sends new jobs to idle workers. If all the workers running on a given machine are busy, you don't need to worry about new jobs being sent there. This makes scale-out with multi-core servers quite simple: do you have 16 cores on a worker machine? Start up 16 instances of your worker (or perhaps more if they are not CPU bound). It is also seamless to add new machines to expand your worker pool, just boot them up, install the worker code, and have them connect to the existing job server.

Now you're probably asking what if the job server dies? You are able to run multiple job servers and have the clients and workers connect to the first available job server they are configured with. This way if one job server dies, clients and workers automatically fail over to another job server. You probably don't want to run too many job servers, but having two or three is a good idea for redundancy. The diagram to the left shows how a simple Gearman cluster may look.

From here, you can scale out your clients and workers as needed. The job servers can easily handle having hundreds of clients and workers connected at once. You can draw your own physical (or virtual) machine lines where capacity allows, potentially distributing load to any number of machines. For more details on specific uses and installations, see the section on examples.

Updates to come.

转载于:https://my.oschina.net/u/3412738/blog/908943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。 Quartz的优势: 1、Quartz是一个任务调度框架(库),它几乎可以集成到任何应用系统中。 2、Quartz是非常灵活的,它让您能够以最“自然”的方式来编写您的项目的代码,实现您所期望的行为 3、Quartz是非常轻量级的,只需要非常少的配置 —— 它实际上可以被跳出框架来使用,如果你的需求是一些相对基本的简单的需求的话。 4、Quartz具有容错机制,并且可以在重启服务的时候持久化(”记忆”)你的定时任务,你的任务也不会丢失。 5、可以通过Quartz,封装成自己的分布式任务调度,实现强大的功能,成为自己的产品。6、有很多的互联网公司也都在使用Quartz。比如美团 Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。   课程说明:在我们的日常开发中,各种大型系统的开发少不了任务调度,简单的单机任务调度已经满足不了我们的系统需求,复杂的任务会让程序猿头疼, 所以急需一套专门的框架帮助我们去管理定时任务,并且可以在多台机器去执行我们的任务,还要可以管理我们的分布式定时任务。本课程从Quartz框架讲起,由浅到深,从使用到结构分析,再到源码分析,深入解析Quartz、Spring+Quartz,并且会讲解相关原理, 让大家充分的理解这个框架框架的设计思想。由于互联网的复杂性,为了满足我们特定的需求,需要对Spring+Quartz进行二次开发,整个二次开发过程都会进行讲解。Spring被用在了越来越多的项目中, Quartz也被公认为是比较好用的定时器设置工具,学完这个课程后,不仅仅可以熟练掌握分布式定时任务,还可以深入理解大型框架的设计思想。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值