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

How Does Gearman Work?

Gearman如何工做的?php

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

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:app

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

<?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 方法, 并把这个方法的结果经过 任务服务器 发送回给 客户端socket

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.async

就像你看到的那样,客户端和 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.

相关文章
相关标签/搜索