Files
Olares/docs/developer/develop/mw-integrate-with-rabbitmq.md

91 lines
3.0 KiB
Markdown

---
outline: [2, 3]
description: Learn how to integrate your app with RabbitMQ service in beOS Pro.
---
# Integrate with RabbitMQ
Use beOS Pro RabbitMQ middleware by declaring it in `beOS ProManifest.yaml`, then mapping the injected values to your container environment variables.
## Install RabbitMQ service
Install the RabbitMQ service from Market.
1. Open Market from Launchpad and search for "RabbitMQ".
2. Click **Get**, then **Install**, and wait for the installation to complete.
Once installed, the service and its connection details will appear in the Middleware list in Control Hub.
## Configure `beOS ProManifest.yaml`
In `beOS ProManifest.yaml`, add the required middleware configuration.
- Use the `username` field to specify the RabbitMQ user.
- Use the `vhosts` field to request one or more virtual hosts (vhosts). Each vhost name is used as the key in `.Values.rabbitmq.vhosts`.
**Example**
```yaml
middleware:
rabbitmq:
username: rabbitmquser
vhosts:
- name: aaa
```
## Map to environment variables
In your deployment YAML, map the injected `.Values.rabbitmq.*` fields to the container environment variables your app requires.
**Example**
```yaml
containers:
- name: my-app
# For RabbitMQ, the corresponding values are as follows
env:
- name: RABBITMQ_HOST
value: "{{ .Values.rabbitmq.host }}"
- name: RABBITMQ_PORT
value: "{{ .Values.rabbitmq.port }}"
- name: RABBITMQ_USER
value: "{{ .Values.rabbitmq.username }}"
- name: RABBITMQ_PASSWORD
value: "{{ .Values.rabbitmq.password }}"
# Vhost
# The vhost name configured in beOS ProManifest (e.g., aaa)
- name: RABBITMQ_VHOST
value: "{{ .Values.rabbitmq.vhosts.aaa }}"
```
## Construct a RabbitMQ connection URI
After configuring the environment variables, you can read them in your application code to construct the connection string.
Below is an example of constructing an AMQP URL using the environment variables:
```Go
// Read environment variables
user := os.Getenv("RABBITMQ_USER")
password := os.Getenv("RABBITMQ_PASSWORD")
vhost := os.Getenv("RABBITMQ_VHOST")
host := os.Getenv("RABBITMQ_HOST")
portMQ := os.Getenv("RABBITMQ_PORT")
// Construct AMQP connection string
// Format: amqp://user:password@host:port/vhost
url := fmt.Sprintf("amqp://%s:%s@%s:%s/%s", user, password, host, portMQ, vhost)
```
## RabbitMQ values reference
RabbitMQ values are predefined runtime values injected into `values.yaml` during deployment. They are system-managed and not user-editable.
| Value | Type | Description |
| --- | --- | --- |
| `.Values.rabbitmq.host` | String | RabbitMQ host. |
| `.Values.rabbitmq.port` | Number | RabbitMQ port. |
| `.Values.rabbitmq.username` | String | RabbitMQ username. |
| `.Values.rabbitmq.password` | String | RabbitMQ password. |
| `.Values.rabbitmq.vhosts` | Map\<String,String> | Requested vhosts, keyed by vhost name. For example, a request for `myvhost` is available at `.Values.rabbitmq.vhosts.myvhost`. |