File d'attente suivante: Magento 2 + RabbitMQ

! Magento 2 -. - , Magento 2 ( M2), . , M2. , : . . : , M2 Rabbit MQ ( — RMQ), . !

. . (exchange) ( ) (publisher) (queue), (consumer) .

, :

  • — , , ;

  • — , , ;

  • — ;

  • , , .

, , , .  

« » 2 RMQ, . RMQ @artemmatveev, , ( , , ). . 

RMQ M2

, RMQ , . , , . — RMQ , M2. , RMQ , , ( ).

, RMQ , M2 . <magento_root>/app/etc/env.php :

'queue' => [
    'amqp' => [
        'host' => 'localhost',
        'port' => '5672', //  
        'user' => 'guest', //  
        'password' => 'guest', //  
        'virtualhost' => '/',
        //    SSL:
        'ssl' => 'true',
        'ssl_options' => [
            'cafile' => '/etc/pki/tls/certs/DigiCertCA.crt',
            'certfile' => '/path/to/magento/app/etc/ssl/test-rabbit.crt',
            'keyfile' => '/path/to/magento/app/etc/ssl/test-rabbit.key'
        ],
    ],
]

bin/magento setup:upgrade . , : M2 RMQ.

M2. bin/magento setup:install , :

--amqp-host="<hostname>" --amqp-port="5672" --amqp-user="<user_name>" --amqp-password="<password>" --amqp-virtualhost="/"

RMQ, Management, RMQ, ( , ).

!

, 2.

2. , (, - ..), WhiteRabbit.

, xml etc . 

communication.xml — .

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
    <topic name="white.rabbit" request="RSHB\WhiteRabbit\Api\Data\WhiteRabbitInterface" />
    <!--  request   ,   
     WhiteRabbit.       
     ,  ,      
     -->
</config>

white.rabbit , , . 

XML- .

queue_consumer.xml — , , , , , .

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
    <consumer name="WhiteRabbitConsumer" queue="white_rabbit" connection="amqp"
              handler="RSHB\WhiteRabbit\Model\WhiteRabbit\WhiteRabbitConsumer::processMessage"/>
</config>

XML- .

queue_topology.xml — , .

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/topology.xsd">
    <exchange name="rshb.white.rabbit" type="topic" connection="amqp">
        <binding id="RshbWhiteRabbit" topic="white.rabbit" destinationType="queue" destination="white_rabbit"/>
    </exchange>
</config>

rshb.white.rabbit, white.rabbit white_rabbit .

XML- .

queue_publisher.xml — , , .

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
    <publisher topic="white.rabbit">
        <connection name="amqp" exchange="rshb.white.rabbit" disabled="false"/>
    </publisher>
</config>

XML- .

. :

<?php

namespace RSHB\WhiteRabbit\Model\WhiteRabbit;

use RSHB\WhiteRabbit\Api\Data\WhiteRabbitInterface;

/**
 * Class WhiteRabbitConsumer
 *
 * @package RSHB\WhiteRabbit\Model\WhiteRabbit
 */
class WhiteRabbitConsumer
{
    /**
     * Process message from queue
     *
     * @param WhiteRabbitInterface $whiteRabbit
     */
    public function processMessage(WhiteRabbitInterface $whiteRabbit)
    {
        //    WhiteRabbit    
    }
}

:

<?php

namespace RSHB\WhiteRabbit\Model\WhiteRabbit;

use Magento\Framework\MessageQueue\PublisherInterface;
use RSHB\WhiteRabbit\Api\Data\WhiteRabbitInterface;

/**
 * Class WhiteRabbitPublisher
 *
 * @package RSHB\WhiteRabbit\Model\WhiteRabbit
 */
class WhiteRabbitPublisher
{
    /**
     * RabbitMQ Topic name
     */
    const TOPIC_NAME = 'white.rabbit';

    /**
     * @var PublisherInterface
     */
    private $publisher;

    /**
     * WhiteRabbitPublisher constructor
     *
     * @param PublisherInterface $publisher
     */
    public function __construct(
        PublisherInterface $publisher
    ) {
        $this->publisher = $publisher;
    }

    /**
     * Add message to queue
     *
     * @param WhiteRabbitInterface $whiteRabbit
     */
    public function execute(WhiteRabbitInterface $whiteRabbit)
    {
        $this->publisher->publish(self::TOPIC_NAME, $whiteRabbit);
    }
}

. :

<?php

namespace RSHB\WhiteRabbit\Model;

use RSHB\WhiteRabbit\Api\Data\WhiteRabbitInterface;
use RSHB\WhiteRabbit\Model\WhiteRabbit\WhiteRabbitPublisher;

/**
 * Class WhiteRabbitManagement
 * @package RSHB\WhiteRabbit\Model
 */
class WhiteRabbitManagement
{
    /**
     * @var WhiteRabbitPublisher
     */
    private $publisher;

    /**
     * WhiteRabbitManagement constructor
     *
     * @param WhiteRabbitPublisher $publisher
     */
    public function __construct(
        WhiteRabbitPublisher $publisher
    ) {
        $this->publisher = $publisher;
    }

    /**
     * Send White Rabbit to Queue
     *
     * @param WhiteRabbitInterface $whiteRabbit
     */
    public function sendWhiteRabbit(WhiteRabbitInterface $whiteRabbit)
    {
        //      White Rabbit
        $this->publisher->execute($whiteRabbit);
    }
}

Management. http://<yourhost>:15672 ( - guest:guest) . Queues white_rabbit , :

- ( processMessage ). cron- 1 ( sleep consumer queue_consumer.xml).

M2 .

RMQ M2 , , , RMQ, RMQ , . RMQ M2 M2  .

. . !




All Articles