Set up the rabbitmq service

The Message Queue Framework (MQF) is a system within Magento Enterprise Edition that allows a module to publish messages to queues. It also defines the consumers that will receive the messages asynchronously. The MQF uses RabbitMQ as the messaging broker, which provides a scalable platform for sending and receiving messages. It also includes a mechanism for storing undelivered messages. RabbitMQ is based on the Advanced Message Queuing Protocol (AMQP) 0.9.1 specification.

We support RabbitMQ version 3.5.

More information about RabbitMQ

Relationship

The format exposed in the $MAGENTO_CLOUD_RELATIONSHIPS follows:

{
   "rabbitmq" : [
      {
         "password" : "guest",
         "ip" : "246.0.129.2",
         "scheme" : "amqp",
         "port" : 5672,
         "host" : "rabbitmq.internal",
         "username" : "guest"
      }
   ]
}

Usage example

In your .magento/services.yaml:

myrabbitmq:
    type: rabbitmq
    disk: 1024

In your .magento.app.yaml:

relationships:
    mq: "myrabbitmq:rabbitmq"

You can use the preceding service in a configuration file of your application as follows:

$relationships = getenv("MAGENTO_CLOUD_RELATIONSHIPS");
if (!$relationships) {
  return;
}

$relationships = json_decode(base64_decode($relationships), TRUE);

foreach ($relationships['mq'] as $endpoint) {
  $container->setParameter('rabbitmq_host', $endpoint['host']);
  $container->setParameter('rabbitmq_port', $endpoint['port']);
}

Connect to RabbitMQ

For debugging purposes, it’s sometimes useful to directly connect to a service instance in one of the following ways:

Connect from your local development environment

You can do this using SSH tunneling:

  1. Use magento-cloud tunnel:open to open a tunnel to the app.
  2. Use the following command to pretty-print your relationships. This lets you see which username and password to use, and you can double check that the remote service’s port is 5672.

    php -r 'print_r(json_decode(base64_decode($_ENV["MAGENTO_CLOUD_RELATIONSHIPS"])));'
    
  3. Use the ssh -L command to enable local port forwarding to RabbitMQ as follows:

    ssh -L 5672:mq.internal:5672 <project ID>-<branch ID>@ssh.na.magentosite.cloud
    
  4. While the session is open, you can start a RabbitMQ client of your choice from your local workstation, configured to connect to localhost:5672 using the user name and password you found in the relationship variable.

Connect from the application

To connect to RabbitMQ running in an application, you should install a client like amqp-utils as a project dependency in your .magento.app.yaml file.

For example,

dependencies:
  ruby:
    amqp-utils: "0.5.1"

Then, when you SSH into your PHP container, you enter any amqp- command available to manage your queues.

Connect from your PHP application

To connect to RabbitMQ using your PHP application, add a PHP library (like PHP AMQPlib) to your source tree.