RabbitMQ (message queue service)
Back to home
On this page
RabbitMQ is a message broker that supports multiple messaging protocols, such as the Advanced Message Queuing Protocol (AMQP). It gives your apps a common platform to send and receive messages and your messages a safe place to live until they’re received.
Supported versions
You can select the major and minor version.
Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.
- 3.12
- 3.11
- 3.10
- 3.9
Deprecated versions
The following versions are deprecated. They’re available, but they aren’t receiving security updates from upstream and aren’t guaranteed to work. They’ll be removed in the future, so migrate to one of the supported versions.
- 3.8
- 3.7
- 3.6
- 3.5
Usage example
1. Configure the service
To define the service, use
the rabbitmq type:
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: rabbitmq:<VERSION>Note that changing the name of the service replaces it with a brand new service and all existing data is lost. Back up your data before changing the service.
2. Add the relationship
To define the relationship, use the rabbitmq endpoint
:
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# Relationships enable access from this app to a given service.
relationships:
<RELATIONSHIP_NAME>: "<SERVICE_NAME>:rabbitmq"
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: rabbitmq:<VERSION>You can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, but it’s best if they’re distinct.
With this definition, the application container (<APP_NAME>) now has access to the service via the relationship <RELATIONSHIP_NAME>.
Example Configuration
App and Service configuration
applications:
# The name of the app container. Must be unique within a project.
myapp:
# Relationships enable access from this app to a given service.
relationships:
rabbitmqqueue: "queuerabbit:rabbitmq"
services:
# The name of the service container. Must be unique within a project.
queuerabbit:
type: rabbitmq:3.9 Use in app
To use the configured service in your app, add a configuration file similar to the following to your project.
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
# Relationships enable an app container's access to a service.
relationships:
rabbitmqqueue: "queuerabbit:rabbitmq"
services:
# The name of the service container. Must be unique within a project.
queuerabbit:
type: rabbitmq:3.9
disk: 256This configuration defines a single application myapp, whose source code exists in the directory <PROJECT_ROOT>/myapp, and has been provided access to the service (queuerabbit) via the relationship rabbitmqqueue.
From this, myapp can retrieve access credentials to the service through the environment variable PLATFORM_RELATIONSHIPS.
That variable is a base64-encoded JSON object, but can be decoded at runtime (using the built-in tool jq) to provide more accessible environment variables to use within the application itself:
# Decode the built-in credentials object variable.
export RELATIONSHIPS_JSON=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode)
# Set environment variables for individual credentials.
export QUEUE_SCHEME=$(echo $RELATIONSHIPS_JSON | jq -r ".rabbitmqqueue[0].scheme")
export QUEUE_USERNAME=$(echo $RELATIONSHIPS_JSON | jq -r ".rabbitmqqueue[0].username")
export QUEUE_PASSWORD=$(echo $RELATIONSHIPS_JSON | jq -r ".rabbitmqqueue[0].password")
export QUEUE_HOST=$(echo $RELATIONSHIPS_JSON | jq -r ".rabbitmqqueue[0].host")
export QUEUE_PORT=$(echo $RELATIONSHIPS_JSON | jq -r ".rabbitmqqueue[0].port")
# Set a single RabbitMQ connection string variable for AMQP.
export AMQP_URL="${QUEUE_SCHEME}://${QUEUE_USERNAME}:${QUEUE_PASSWORD}@${QUEUE_HOST}:${QUEUE_PORT}/"The above file โ .environment in the myapp directory โ is automatically sourced by Upsun into the runtime environment, so that the variable AMQP_URL can be used within the application to connect to the service.
Note that AMQP_URL, and all Upsun-provided environment variables like PLATFORM_RELATIONSHIPS, are environment-dependent. Unlike the build produced for a given commit, they can’t be reused across environments and only allow your app to connect to a single service instance on a single environment.
A file very similar to this is generated automatically for your when using the upsun ify command to migrate a codebase to Upsun.
Connect to RabbitMQ
When debugging, you may want to connect directly to your RabbitMQ service. You can connect in multiple ways:
- An SSH tunnel
- A web interface
In each case, you need the login credentials that you can obtain from the relationship.
Via SSH
To connect directly to your RabbitMQ service in an environment, open an SSH tunnel with the Upsun CLI.
To open an SSH tunnel to your service with port forwarding, run the following command:
upsun tunnel:single --gateway-portsThen configure a RabbitMQ client to connect to this tunnel using the credentials from the relationship. See a list of RabbitMQ client libraries.
Access the management UI
RabbitMQ offers a management plugin with a browser-based UI. You can access this UI with an SSH tunnel.
To open a tunnel, follow these steps.
-
SSH into your app container with a flag for local port forwarding:
-
ssh $(upsun ssh --pipe) -L 15672:RELATIONSHIP_NAME.internal:15672RELATIONSHIP_NAME is the name you defined.
- Open
http://localhost:15672in your browser. Log in using the username and password from the relationship.
Configuration options
You can configure your RabbitMQ service in the services configuration with the following options:
| Name | Type | Required | Description |
|---|---|---|---|
vhosts |
List of strings |
No | Virtual hosts used for logically grouping resources. |
You can configure additional virtual hosts, which can be useful for separating resources, such as exchanges, queues, and bindings, into their own namespaces. To create virtual hosts, add them to your configuration as in the following example:
services:
# The name of the service container. Must be unique within a project.
rabbitmq:
type: "rabbitmq:3.9"
configuration:
vhosts:
- host1
- host2 Relationship reference
Example information available through the PLATFORM_RELATIONSHIPS environment variable
or by running upsun relationships.
Note that the information about the relationship can change when an app is redeployed or restarted
or the relationship is changed.
So your apps should only rely on the PLATFORM_RELATIONSHIPS environment variable directly rather than hard coding any values.
{
"username": "guest",
"scheme": "amqp",
"service": "rabbitmq38",
"fragment": null,
"ip": "169.254.57.5",
"hostname": "iwrccysk3gpam2zdlwdr5fgs2y.rabbitmq38.service._.eu-3.upsunapp.com",
"port": 5672,
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "rabbitmq.internal",
"rel": "rabbitmq",
"path": null,
"query": [],
"password": "ChangeMe",
"type": "rabbitmq:3.9",
"public": false,
"host_mapped": false
} Upgrading
When upgrading RabbitMQ, skipping major versions (e.g. 3.7 -> 3.11) is not supported. Make sure you upgrade sequentially (3.7 -> 3.8 -> 3.9 -> 3.10 -> 3.11) and that each upgrade commit translates into an actual deployment.