我正在尝试使用 3 个服务设置 docker compose:1 个 rabbitmq 服务和 2 个 java 应用程序(1 个生产者和 1 个消费者)。 rabbitmq 服务启动时,默认没有队列。只有当产生发送消息到队列时,它才实际创建它。同时,消费者需要它已经监听的队列。所以问题来了:当我运行我的 docker compose 时,rabbitmq 在我的生产者开始时很好,但是消费者找不到队列并且失败了。那么,我可以以某种方式设置 docker compose rabbitmq 服务以默认启动和创建队列吗?
version: '3.3'
services:
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq
restart: always
environment:
RABBITMQ_DEFAULT_USER: help
RABBITMQ_DEFAULT_PASS: 51243
ports:
- "5672:5672"
- "15672:15672"
@更新 我找到了很好的方法来实现我想要的。我可以启动rabbitmq,创建我需要的所有东西(队列)并导出definitions.json。然后我需要把它放在/etc/rabbitmq/definitions.json
version: '3.3'
services:
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq
restart: always
environment:
RABBITMQ_DEFAULT_USER: ete
RABBITMQ_DEFAULT_PASS: 1402
ports:
- "5672:5672"
- "15672:15672"
volumes:
- ./definitions.json:/etc/rabbitmq/definitions.json
但这里有另一个问题:这个定义文件覆盖了我由环境变量设置的用户。 这是定义.json:
{
"rabbit_version": "3.8.9",
"rabbitmq_version": "3.8.9",
"product_name": "RabbitMQ",
"product_version": "3.8.9",
"users": [
{
"name": "eternal",
"password_hash": "wz1jzbGjNMZ115U7XhEUvF271uImnOfho2jpx2pOvLSY/Ssl",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": "administrator"
}
],
"vhosts": [
{
"name": "/"
}
],
"permissions": [
{
"user": "eternal",
"vhost": "/",
"configure": ".*",
"write": ".*",
"read": ".*"
}
],
"topic_permissions": [],
"parameters": [],
"global_parameters": [
{
"name": "cluster_name",
"value": "rabbit@58cb492cd4ee"
},
{
"name": "internal_cluster_id",
"value": "rabbitmq-cluster-id-DlZ_FZVpiFx93CVZXneG4A"
}
],
"policies": [],
"queues": [
{
"name": "qqq",
"vhost": "/",
"durable": false,
"auto_delete": false,
"arguments": {}
}
],
"exchanges": [],
"bindings": []
}
如果我将删除用户部分和其他所有内容并仅保留队列部分,那么在 docker-compose up 之后,我将无法登录到管理页面。因为我的登录名和密码不被接受。