upingli/

hofan

Hofan 洗车系统组件。

v0.1.1 2020-11-26 05:53 UTC

This package is not auto-updated.

Last update: 2024-09-27 00:18:21 UTC


README

安装

您可以通过以下命令使用 composer 安装此包:

composer require upingli/hofan

此包中的队列组件基于 vladimir-yuldashev/laravel-queue-rabbitmq。因为 laravel-queue-rabbitmq 使用 PHP serialize() 序列化队列中的消息,这不能由其他语言实现,因此不友好。此组件为消息提供 JSON 序列化格式,以便与其他非 PHP 系统交换消息。

在使用此组件之前,您必须进行配置。

将连接添加到 config/queue.php

'default' => env('QUEUE_CONNECTION', 'sync'),
//Not required, if you want to use a second conenection to avoid use the default one, add the below line. Otherwise, will use the default.
'second'  => env('QUEUE_CONNECTION2', 'rabbitmq'),

'connections' => [
    // ...

    'rabbitmq' => [
    
       'driver' => 'rabbitmq',
       'queue' => env('RABBITMQ_QUEUE', 'default'),
       'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
   
       'hosts' => [
           [
               'host' => env('RABBITMQ_HOST', '127.0.0.1'),
               'port' => env('RABBITMQ_PORT', 5672),
               'user' => env('RABBITMQ_USER', 'guest'),
               'password' => env('RABBITMQ_PASSWORD', 'guest'),
               'vhost' => env('RABBITMQ_VHOST', '/'),
           ],
       ],
   
       'options' => [
           'ssl_options' => [
               'cafile' => env('RABBITMQ_SSL_CAFILE', null),
               'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
               'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
               'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
               'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
           ],
       ],
   
       /*
        * Set to "horizon" if you wish to use Laravel Horizon.
        */
       'worker' => env('RABBITMQ_WORKER', 'default'),
        
    ],

    // ...    
],

使用 rabbitmq 队列创建订单的示例

class TestController extends \Illuminate\Routing\Controller
{
    $order = new OrderTransit();
    OrderQueueCall::createOrder($order);
}

createOrder() 的结果将由 cn\hofan\Queue\Jobs\OrderJsonJob 处理。如果您想自己处理,这里有一个示例。

定义您的作业

class MyJob extends JsonJob
{
    protected function handle(string $command, $result)
    {
        if($command = "CREATE_ORDER_RESULT")
        {
            //Handle result for createOrder
        }

        return true;
    }
}

然后更改控制器中的代码

class TestController extends \Illuminate\Routing\Controller
{
    $order = new OrderTransit();
    OrderQueueCall::createOrder($order, MyJob::class);
}