From 02963ed19ffa8c3f4db8383597a7ac7aa31d5391 Mon Sep 17 00:00:00 2001 From: pengbd3 Date: Wed, 29 Nov 2017 15:42:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4gearman=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 165 ++++++++++++------- src/Phphc/Gearman/Commands/Workers.php | 32 +++- src/Phphc/Gearman/GearmanServiceProvider.php | 2 +- src/Phphc/Gearman/Service/SumServer.php | 8 +- 4 files changed, 140 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 399029b..646270e 100644 --- a/README.md +++ b/README.md @@ -1,91 +1,144 @@ -# Description +# 描述: -This package gives you the possibily to add gearman as native queue back-end service +该包是gearmn服务对laravel的一个适配方案,参照 [laravel-gearman](https://github.com/pafelin/laravel-gearman) 进行了扩展,安装之前确保php gearman扩展安装以及gearman服务已正确安装,具体的安装方案可以参考 [gearman服务安装](https://pengbd3.github.io/2017/11/08/gearman1/) -#Installation +# 安装: -first you need to add it to your composer.json +使用composer安装 `composer require many/gearman` +* 进入`laravel`项目,修改配置文件 `config/app.php` ,首先需要注释掉原有队列服务器提供者的代码: -second, in `config/app.php`, you need to comment out the native queue service provider + //'Illuminate\Queue\QueueServiceProvider::class', - //'Illuminate\Queue\QueueServiceProvider', + 然后添加自定义的服务器提供者: -and to put this instead: + 'Phphc\Gearman\GearmanServiceProvider::class', - 'Pafelin\Gearman\GearmanServiceProvider', +* 接着在 `config/queue.php` 文件中添加: -Then in your config/queue.php file you can add: - - 'default' => 'gearman', - 'connections' => array( + 'connections' => array( + //追加内容 start 'gearman' => array( 'driver' => 'gearman', - 'host' => 'localserver.6min.local', + 'host' => '127.0.0.1', 'queue' => 'default', 'port' => 4730, - 'timeout' => 1000 //milliseconds + 'timeout' => 300 ) - ) + ) -or, if you have multiple gearman servers: + 如果你有多个Gearman服务器: - 'default' => 'gearman', - 'connections' => array( + 'connections' => array( 'gearman' => array( 'driver' => 'gearman', 'hosts' => array( - array('host' => 'localserver.6min.local', 'port' => 4730), - array('host' => 'localserver2.6min.local', 'port' => 4730), + array('host' => '127.0.0.1', 'port' => 4730), + array('host' => '127.0.0.2', 'port' => 4730), ), 'queue' => 'default', - 'timeout' => 1000 //milliseconds + 'timeout' => 300 ) - ) - -Then in your code you can add code as (this is the native way to add jobs to the queue): - - Queue::push('SomeClass', array('message' => 'The data that should be available in the SomeClass@fire method')); - -Small hint, you can call Namespaced classes and everything that is written in the docs of laravel for calling custom methods is valid here, too. - - -# Example: - -I add a "service" folder to my app folder and inside I create a file "SendMail.php" -The code of the class is here: - - addServer($server['host'], $server['port']); + } + } else { + $client->addServer($config['host'], $config['port']); + } + + //可以通过setCompleteCallback函数给计算结果返回给客户端 + $client->setCompleteCallback(function (GearmanTask $task) use (&$sum) { + $sum = $task->data(); + $this->info("Server results:" . $sum . "\n"); + }); + + for ($i = 0; $i < 2; $i++) { + //后期版本会封装单独的类来匹配数据项 + $data = [ + "displayName" => "App\\Services\\SumServer", + "job" => "App\\Services\\SumServer", + "maxTries" => null, + "timeout" => null, + "data" => [1 + $i, 2 + $i] + ]; + //添加任务 + $client->addTask('default', json_encode($data, 1)); + } + //添加任务 + + $client->runTasks(); + } + ``` +- 当laravel队列监听任务启动时,默认将名为 `default` 的函数 注册到gearman服务中,在终端可以通过命令 `gearadmin --status` 查看,在 `$client->addTask` 添加任务的时候第一个参数为函数名,第二个参数为需要提交到job中的数据。`$data1` 数组的 `displayName` 和 `job` 项是需要处理任务的命名空间名,`data`项才是最终 `SumServer` 接收到的值。 +- 值得注意的是 `SumServer` 中的 `fire` 函数包含两个参数 `$job` 和 `$data`接收到客户端的数据 `array(1, 2)` +- 服务端的进程守护建议使用 `laravel` 官方推荐的 [supervisor]('https://laravel.com/docs/5.5/queues#supervisor-configuration') +# 使用方法: + +在 `app/Service` 文件夹下新增一个名为: `SendMail.php` 的类 + +```php + 'Message №' . $row)); + Queue::push('App\Services\SendMail', array('message' => 'Message' . $row)); } }); + +``` -Finally I just run on my console: - - php artisan queue:listen - -And I go to check what's on my email - -#Bugs - -Please if you notice a bug open an issue or submit request. - -Hope this package will help you +# Bugs +如果发现有任何问题,请提交issue \ No newline at end of file diff --git a/src/Phphc/Gearman/Commands/Workers.php b/src/Phphc/Gearman/Commands/Workers.php index fa7d26f..0a352e3 100644 --- a/src/Phphc/Gearman/Commands/Workers.php +++ b/src/Phphc/Gearman/Commands/Workers.php @@ -47,20 +47,38 @@ public function handle() public function seedWork() { $sum = 0; + //实例化gearman服务的客户端 $client = new GearmanClient(); - $client->addServers('127.0.0.1:4730'); //默认参数主机地址:127.0.0.1 端口:4730 + //默认参数主机地址:127.0.0.1 端口:4730 + $config = config('queue.gearman'); + if (isset($config['hosts'])) { + foreach ($config['hosts'] as $server) { + $client->addServer($server['host'], $server['port']); + } + } else { + $client->addServer($config['host'], $config['port']); + } -//可以通过setCompleteCallback函数给计算结果返回给客户端 + //可以通过setCompleteCallback函数给计算结果返回给客户端 $client->setCompleteCallback(function (GearmanTask $task) use (&$sum) { $sum = $task->data(); - $this->info("计算结果为:" . $sum . "\n"); + $this->info("Server results:" . $sum . "\n"); }); -//添加5个需要累加和的任务 - $data = ["displayName" => "App\\Services\\SumServer", "job" => "App\\Services\\SumServer", "maxTries" => null, "timeout" => null, "data" => [1, 2]]; - $client->addTask('default', json_encode($data, 1)); + for ($i = 0; $i < 2; $i++) { + //后期版本会封装单独的类来匹配数据项 + $data = [ + "displayName" => "App\\Services\\SumServer", + "job" => "App\\Services\\SumServer", + "maxTries" => null, + "timeout" => null, + "data" => [1 + $i, 2 + $i] + ]; + //添加任务 + $client->addTask('default', json_encode($data, 1)); + } + //添加任务 -//运行队列中的任务,do系列不需要runTask() $client->runTasks(); } } diff --git a/src/Phphc/Gearman/GearmanServiceProvider.php b/src/Phphc/Gearman/GearmanServiceProvider.php index 9a3dbab..0a84919 100644 --- a/src/Phphc/Gearman/GearmanServiceProvider.php +++ b/src/Phphc/Gearman/GearmanServiceProvider.php @@ -11,7 +11,7 @@ public function boot() $this->publishes([ __DIR__.'/Service' => base_path('app/Service'), __DIR__.'/Commands/Workers.php' => base_path('app/Console/Commands/Workers.php'), - ]); + ],'gearman'); } /** * Register the connectors on the queue manager. diff --git a/src/Phphc/Gearman/Service/SumServer.php b/src/Phphc/Gearman/Service/SumServer.php index 7ae815e..974719e 100644 --- a/src/Phphc/Gearman/Service/SumServer.php +++ b/src/Phphc/Gearman/Service/SumServer.php @@ -2,14 +2,16 @@ namespace App\Services; -use Illuminate\Support\Facades\DB; - class SumServer { public function fire($job, $data) { - return $data[0] + $data[1]; + $result = $data[0] + $data[1]; + //模拟任务消耗时间 + sleep(2); + echo "Client results:{$result}\n"; + return $result; } } \ No newline at end of file