Introduction
In RabbitMQ, a virtual host (vhost) is like a “namespace” or “container” inside the broker.
It separates exchanges, queues, and bindings so different applications or environments can share the same RabbitMQ cluster without interfering with each other.
If you know databases:
- RabbitMQ server = database server.
- vhost = database.
- queue/exchange = tables inside the database.
What is a vhost?
- A vhost is an isolated environment inside RabbitMQ.
- Each vhost has its own:
- Queues
- Exchanges
- Bindings
- User permissions
- Users can only access resources inside the vhosts where they have rights.
By default, RabbitMQ creates one vhost: /
.
Why use vhosts?
- Isolation
- Apps in different vhosts cannot see each other’s queues or exchanges.
- Example: one vhost for payments, another for notifications.
- Security
- You can give different permissions per vhost.
- Example: user
payments_app
can only publish/consume in/payments
.
- Multi-tenancy
- One RabbitMQ cluster can serve many teams or customers.
- Each team gets its own vhost.
- Environment separation
- Use different vhosts for dev, staging, and production in the same cluster.
How to Create and Use vhosts
Create vhost
rabbitmqctl add_vhost payments_vhost
Add user and set permission
rabbitmqctl add_user payments_app StrongP@ssw0rd rabbitmqctl set_permissions -p payments_vhost payments_app ".*" ".*" ".*"
-p payments_vhost
→ apply permissions to that vhost only..*
means full rights (configure, write, read).
Connect to vhost in client (Go example)
conn, err := amqp.Dial("amqp://payments_app:StrongP@ssw0rd@localhost:5672/payments_vhost") if err != nil { log.Fatal(err) } defer conn.Close()
Notice at the end: /payments_vhost
. That’s how the client chooses the vhost.
Pages: 1 2
Category: RabbitMQ