Magento integration with Mongodb and RabbitMq
- Ecommerce
- General
Magento integration with Mongodb and RabbitMq
Magento frontend is little slow with mysql. To solve this problem, We can use mongo db for categories and products listing. We can copy store categories and products to mongo db via observer whenever any changes done by admin.
We can connect to mongo db at frontend to fetch categories and products. For order flow, we will still use mysql.
We need to create a extension to add / update products and categories when updates from magento backend.
1. etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<config> <modules> <Auriga_Mongo> <version>0.0.1</version> </Auriga_Mongo> </modules> <global> <models> <auriga_mongo> <class>Auriga_Mongo_Model</class> </auriga_mongo> </models> </global> <adminhtml> <events> <catalog_product_save_before> <observers> <auriga_mongo> <class>auriga_mongo/observer</class> <method>catalog_product_save_after</method> </auriga_mongo> </observers> </catalog_product_save_before> </events> </adminhtml> </config> |
2. Model/Observer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php class Auriga_Mongo_Model_Observer { public function catalog_product_save_after($observer) { $product = $observer->getProduct(); $data = $product->getData(); //file_put_contents("/var/www/html/magento/abc.txt", $product->getId()." ".$data['name'], FILE_APPEND); sleep(1); shell_exec("php /var/www/html/magento/mongo/amqp_publisher.php ".$product->getId()); } } |
Install RabbitMQ
1. Install rabbitmq-server_3.6.5-1_all.deb server.
2. Create server and client demo for rabbitmq.
Server: amqp_consumer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<?php include(__DIR__ . '/config.php'); use PhpAmqpLib\Connection\AMQPStreamConnection; $exchange = 'router'; $queue = 'msgs'; $consumerTag = 'consumer'; $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST); $channel = $connection->channel(); $channel->queue_declare($queue, false, true, false, false); $channel->exchange_declare($exchange, 'direct', false, true, false); $channel->queue_bind($queue, $exchange); /** * @param \PhpAmqpLib\Message\AMQPMessage $message */ function process_message($message) { $product_id = (int)$message->body; print "Product ID: ".$product_id; $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']); // Send a message with the string "quit" to cancel the consumer. if ($message->body === 'quit') { $message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']); } if($product_id) { error_reporting(1); ini_set("display_errors", 1); //connect to magento and get product details. include_once __DIR__."/../app/Mage.php"; Mage::app('default'); $_product = Mage::getModel('catalog/product')->load($product_id); $data = ['product_id' => $product_id, 'name' => $_product->getName(), 'image' => $_product->getImageUrl(), 'price' => $_product->getPrice(), 'sku' => $_product->getSku(), 'url' => $_product->getProductUrl(), 'short_desc' => $_product->getShortDescription() ]; file_put_contents("/var/www/html/magento/abc.txt", print_r($data,true), FILE_APPEND); include_once __DIR__."/MongoDB.php"; $MongoDB = new MongoDB(); $MongoDB->updateProduct($data); } } $channel->basic_consume($queue, $consumerTag, false, false, false, false, 'process_message'); /** * @param \PhpAmqpLib\Channel\AMQPChannel $channel * @param \PhpAmqpLib\Connection\AbstractConnection $connection */ function shutdown($channel, $connection) { $channel->close(); $connection->close(); } register_shutdown_function('shutdown', $channel, $connection); // Loop as long as the channel has callbacks registered while (count($channel->callbacks)) { $channel->wait(); } ?> |
Client: amqp_publisher.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php include(__DIR__ . '/config.php'); use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $exchange = 'router'; $queue = 'msgs'; $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST); $channel = $connection->channel(); $channel->queue_declare($queue, false, true, false, false); $channel->exchange_declare($exchange, 'direct', false, true, false); $channel->queue_bind($queue, $exchange); $messageBody = implode(' ', array_slice($argv, 1)); $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)); $channel->basic_publish($message, $exchange); $channel->close(); $connection->close(); ?> |
Install MongoDB
1. Install mongo db to ubuuntu 16.0.4
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
2. Start Service
sudo service mongod start
3. Install php driver for mongo db
http://php.net/manual/en/mongodb.installation.pecl.php
4. http://zetcode.com/db/mongodbphp/
MongoDB.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php class MongoDB { public $manager; public $database = 'magento'; public function __construct() { //connect to mongodb $this->manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); } public function updateProduct($product) { $bulk = new MongoDB\Driver\BulkWrite(); $bulk->update(['product_id' => $product['product_id']], ['$set' => $product], ['upsert' => true]); //$bulk->insert($product); try { $response = $this->manager->executeBulkWrite($this->database.".product", $bulk); //print_R($response); } catch(MongoDB\Driver\Exception\Exception $e){ echo "Exception:", $e->getMessage(), "\n"; echo "In file:", $e->getFile(), "\n"; echo "On line:", $e->getLine(), "\n"; } } public function getProducts() { $query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]); try { $rows = $this->manager->executeQuery($this- >database.".product", $query); return $rows; } catch(MongoDB\Driver\Exception\Exception $e){ echo "Exception:", $e->getMessage(), "\n"; echo "In file:", $e->getFile(), "\n"; echo "On line:", $e->getLine(), "\n"; } } } |
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s