Shopify $1 For 3 Months: The Limited-Time Offer Is Back!
The Shopify 1 dollar 3-month deal is officially back from Oct 31, 2024. Now, all new merchants can enjoy 3 months of Shopify for $1/month. See how to claim the deal now!
Vinh Jacker | 10-01-2024
The Most Popular Extension Builder for Magento 2
With a big catalog of 224+ extensions for your online store
In the Magento 2 ecosystem, RabbitMQ plays a crucial role in enhancing system performance and scalability. Configuring and integrating RabbitMQ into Magento 2 not only helps to offload the server but also improves the speed of processing background tasks, ensuring smooth and continuous operation of your online store.
This article will provide an overview of RabbitMQ, how RabbitMQ works, and a detailed step-by-step guide to configuring it in Magento 2.
RabbitMQ is a free, open-source message broker that enables applications to send and receive messages via queues. It acts as an intermediary, facilitating effective and reliable communication between different services and applications. RabbitMQ operates using the Advanced Message Queuing Protocol (AMQP) to ensure that messages are delivered correctly and processed appropriately.
In the Magento 2 system, RabbitMQ can be used to improve performance by handling background tasks asynchronously. This helps reduce server load, speeds up application response time, and ensures that important tasks are not delayed.
Advantages of RabbitMQ
High Reliability: RabbitMQ ensures that messages are sent and received reliably through mechanisms such as message acknowledgments and persistence.
Scalability: RabbitMQ supports various mechanisms for scaling the system, including clustering, federation, and sharding.
Flexibility: RabbitMQ supports multiple protocols, not only AMQP but also STOMP, MQTT, and HTTP, simplifying integration with various applications and systems.
RabbitMQ plays a significant role in improving system performance and scalability by handling background tasks asynchronously, reducing server load, and accelerating application response.
Step 1: To configure RabbitMQ in Magento 2, you need to install RabbitMQ for Magento 2. To install RabbitMQ on an Ubuntu operating system, you need to issue the following command:
sudo apt install -y rabbitmq-server
Next, you need to integrate RabbitMQ with Magento 2. To achieve this, you should create a queue section within the **
'queue' => [
'amqp' => [
'host' => 'yourhost.yourdomain.com',
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'virtualhost' => '/'
],
],
Next, execute the command to implement the modifications.
bin/magento setup:upgrade
In the official documentation, instructions are also available for other systems, as well as for Magento EE and Cloud versions.
Additionally, it’s necessary to enable the rabbitmq_management extension. This extension provides a web interface and helps in managing Magento 2 message queues.
rabbitmq-plugins enable rabbitmq_management
Next, navigate to http://127.0.0.1:5672/ and log in using the default data: guest/guest. Alternatively, you can set up an SSH tunnel for remote access:
ssh -L 5672:localhost:5672 [email protected]
Next, we proceed by creating a new module.xml file in app/code/Mageplaza/QExample/etc with the code below:
<<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mageplaza_QExample" setup_version="1.0.0">
</module>
</config>
To register it, create a registration.php file in app/code/Mageplaza/QExample and include the following code:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mageplaza_QExample',
__DIR__
);
According to the Magento 2 documentation, when using an AMQP (RabbitMQ) connection, you must configure the message queue by modifying several files in the app/code/Mageplaza/Mageplaza_QExample/etc directory:
communication.xml: Specifies the components of the message queue system utilized by all communication types.
queue_consumer.xml: Defines the relationship between a queue and its consumer.
queue_topology.xml: Establishes the routing rules for messages and sets up queues and exchanges.
queue_publisher.xml: Specifies the exchange where a topic is published.
Let’s examine an example of implementing RabbitMQ in a Magento 2 module and the required configurations.
In communication.xml, define the topic and its associated elements. You can include as many topics as needed.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
<topic name="customer.clean" request="Magento\Customer\Api\Data\CustomerInterface"/>
</config>
Next, navigate to queue_consumer.xml and define the consumer elements, including name, queue, handler, consumerInstance, connection, and maxMessages. In our example, we have used only name, queue, handler, and connection, but you can adjust and combine these elements as needed.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
<consumer name="AmCustomerClean" queue="customer_clean" connection="queue"
handler="Mageplaza\QExample\Model\Customer\DeleteConsumer::processMessage"/>
</config>
Next, open queue_topology.xml to set up the exchange and optionally configure three elements: binding, arguments, and binding/arguments. In this case, we have only used the binding to link the topic ID with the consumer.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/topology.xsd">
<exchange name="am.customer.delete" type="topic" connection="queue">
<binding id="AmCustomerClean" topic="customer.clean" destinationType="queue"
destination="customer_clean"/>
</exchange>
</config>
The final file, queue_publisher.xml, contains just two elements: publisher and connection. This file is responsible for publishing all the data to Magento’s RabbitMQ.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
<publisher topic="customer.clean">
<connection name="amqp" exchange="am.customer.delete" />
</publisher>
</config>
Next, we need to set up a plugin. To do this, create the di.xml file in the app/code/Mageplaza/QExample/etc directory.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Model\ResourceModel\Customer">
<plugin name="customer_clean_publisher"
type="Mageplaza\QExample\Plugin\DeleteCustomerPlugin"/>
</type>
</config>
Additionally, for this plugin, you must create two new directories: app/code/Mageplaza/QExample/Model/Customer and app/code/Mageplaza/QExample/Plugin.
Within the Plugin directory, create a PHP file to define its functionality. In this instance, the DeleteCustomerPlugin will be structured as follows:
<?php namespace Mageplaza\QExample\Plugin;
use Mageplaza\QExample\Model\Customer\DeletePublisher;
use Magento\Customer\Model\Customer as CustomerModel;
use Magento\Customer\Model\ResourceModel\Customer; /**
* Class DeleteCustomerPlugin
* @package Mageplaza\QExample\Plugin
*/
class DeleteCustomerPlugin
{
/**
* @var DeletePublisher
*/
private $customerCleanPublisher;
/**
* DeleteCustomerPlugin constructor.
* @param DeletePublisher $customerCleanPublisher
*/
public function __construct(DeletePublisher $customerCleanPublisher)
{
$this->customerCleanPublisher = $customerCleanPublisher;
}
/**
* @param Customer $subject
* @param Customer $result
* @param CustomerModel $customer
* @return Customer
*/
public function afterDelete(Customer $subject, Customer $result, CustomerModel $customer)
{
$this->customerCleanPublisher->execute($customer);
return $result;
}
}
The final step involves specifying the plugin model with two PHP files. These files will define the actions required for the constructor and specify which customer data needs to be deleted.
DeleteConsumer.php:
<?php
namespace Mageplaza\QExample\Model\Customer;
use Magento\Customer\Api\Data\CustomerInterface;
/**
* Class DeleteConsumer
* @package Mageplaza\QExample\Model\Customer
*/
class DeleteConsumer
{
/**
* DeleteConsumer constructor.
*/
public function __construct()
{
do smth what you need in constructor
}
/**
* @param CustomerInterface $customer
*/
public function processMessage(CustomerInterface $customer)
{
clean up data actions for customer
}
}
DeletePublisher.php:
<?php
namespace Mageplaza\QExample\Model\Customer;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\MessageQueue\PublisherInterface;
class DeletePublisher
{
const TOPIC_NAME = 'customer.clean';
/**
* @var PublisherInterface
*/
private $publisher;
/**
* DeletePublisher constructor.
* @param PublisherInterface $publisher
*/
public function __construct(PublisherInterface $publisher)
{
$this->publisher = $publisher;
}
/**
* @param CustomerInterface $customer
*/
public function execute(CustomerInterface $customer)
{
$this->publisher->publish(self::TOPIC_NAME, $customer);
}
}
When you’re set, make sure to execute:
*bin/magento setup:upgrade*
And start your consumer using the command:
*bin/magento queue:consumers:start AmCustomerClean*
Configuring RabbitMQ in Magento 2 is a crucial step to enhance the performance and scalability of your online store. By using RabbitMQ, you can efficiently handle background tasks, offload the server, and ensure smooth operation of your application.
We hope this article has provided you with a clear and detailed guide on how to configure RabbitMQ in Magento 2. If you have any questions, feel free to reach out to our experts for the best support.
Happy coding!