如何在启动时在 RabbitMQ 中创建队列 [英] How to create a queue in RabbitMQ upon startup

查看:122
本文介绍了如何在启动时在 RabbitMQ 中创建队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用预先创建的队列 qwer 在 docker 容器中启动 RMQ.

I am trying to start RMQ inside docker container, with precreated queue qwer.

在此之前,我使用的是简单的 docker-compose.yml 文件:

Prior to this, I was using simple docker-compose.yml file:

rabbit:
    image: rabbitmq:management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest

它工作得很好,只是它在开始时没有预先创建队列.现在我已经切换到自定义图像,使用以下 Dockerfile:

And it worked fine, except that it has no queues pre-created at start. Now I've switched to custom image, with following Dockerfile:

FROM rabbitmq:management-alpine

ADD rabbitmq.conf /etc/rabbitmq/
ADD definitions.json /etc/rabbitmq/

RUN chown rabbitmq:rabbitmq /etc/rabbitmq/rabbitmq.conf /etc/rabbitmq/definitions.json

其中 rabbitmq.conf 是 v3.7+ sysctl 样式的配置,行:

where rabbitmq.conf is v3.7+ sysctl-styled config, with line:

management.load_definitions = /etc/rabbitmq/definitions.json

definitions.json 包含创建队列的尝试:

and definitions.json contains attempt to create queue:

{
    "vhosts":[
        {"name":"/"}
    ],
    "queues":[
        {"name":"qwer","vhost":"/","durable":true,"auto_delete":false,"arguments":{}}
    ]
}

现在开始拒绝登录:

Error on AMQP connection <0.660.0> (172.18.0.6:48916 -> 172.18.0.10:5672, state: starting):
PLAIN login refused: user 'guest' - invalid credentials

本以为任务有些简单,但是rabbit本身的配置过程是最复杂的任务,文档有些不清楚.

I thought that the task is somewhat simple, but configuration process of rabbit itself is most complex task, and documentation is somewhat unclear.

我无法弄清楚它应该如何工作,即使经过 4 天的试验和谷歌搜索..

I was unable to figure out how should it work, even after 4 days of trials and googling..

您能帮我吗,如何编写配置文件,以创建队列并保留连接和交谈的能力?

Could you help me, how to write configuration file, in order to create a queue and preserve ability to connect and talk to it?

推荐答案

实际上你快到了.

RabbitMQ 有一条规则,guest"用户只能从 localhost 连接.由于您在 docker 上运行它,我假设您尝试通过以下方式从外部访问它:docker run <rabbitmq-docker-img>-p 15672:15672

RabbitMQ has a rule that the "guest" user can only connect from localhost. Since you are running it on a docker, I'm assuming you are trying to access it from outside by exposing port "15672" by doing: docker run <rabbitmq-docker-img> -p 15672:15672

所以要解决这个问题,您需要做的是创建一个具有管理员权限的用户.

So to get around this, what you have to do is create a user with admin privileges.

首先,改变这个:

rabbit:
    image: rabbitmq:management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password

您可以使用 ath,我使用用户/密码作为您的用户/密码.

You can use ath, I used user/password as your user/password.

在你的Dockerfile中,你可以添加:EXPOSE 15672如果你不想每次运行都暴露.

In your Dockerfile, you can add: EXPOSE 15672 If you don't want to expose each time you run.

最后,对你的 definitions.json 文件进行如下修改:

Lastly, make amends to your definitions.json file as follows:

{
    "users": [
      {
        "name": "user",  
        "password_hash": "password",
        "hashing_algorithm": "rabbit_password_hashing_sha256",
        "tags": "administrator"
      }
    ],

    "vhosts":[
        {"name":"/"}
    ],
    "queues":[
        {"name":"qwer","vhost":"/","durable":true,"auto_delete":false,"arguments":{}}
    ]
}

告诉我进展如何!

查看此链接

使用这个 Dockerfile:

Use this Dockerfile:

FROM rabbitmq

# Define environment variables.
ENV RABBITMQ_USER user
ENV RABBITMQ_PASSWORD password

ADD init.sh /init.sh
EXPOSE 15672

# Define default command
CMD ["/init.sh"]

并使用这个 init.sh:

And use this init.sh:

#!/bin/sh

# Create Rabbitmq user
( sleep 5 ; 
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD 2>/dev/null ; 
rabbitmqctl set_user_tags $RABBITMQ_USER administrator ; 
rabbitmqctl set_permissions -p / $RABBITMQ_USER  ".*" ".*" ".*" ; 
echo "*** User '$RABBITMQ_USER' with password '$RABBITMQ_PASSWORD' completed. ***" ; 
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &

# $@ is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2
rabbitmq-server $@

这篇关于如何在启动时在 RabbitMQ 中创建队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆