配置 Docker 容器的 RabbitMQ

1,114 阅读1分钟

针对 rabbitmq 进行配置:

使用 WebSocket 连接 RabbitMQ
RabbitMQ 启用 HTTP 后台认证
创建步骤如下。

  1. 启动容器

docker run -d --hostname my-rabbit --name rabbitmq -p 8080:15672 \
rabbitmq:3.7-management-alpine
1
2

  1. 进入容器

docker exec -it rabbitmq /bin/sh
1

  1. 启用插件

umask 0022; rabbitmq-plugins enable --offline \
rabbitmq_auth_backend_http rabbitmq_auth_backend_cache rabbitmq_web_stomp

特别注意这里的 umask 0022;,只有这样配置,镜像才能读取插件配置,才能启动成功。

输出内容:

The following plugins have been configured:
rabbitmq_auth_backend_cache
rabbitmq_auth_backend_http
rabbitmq_management
rabbitmq_management_agent
rabbitmq_stomp
rabbitmq_web_dispatch
rabbitmq_web_stomp
Applying plugin configuration to rabbit@my-rabbit...
The following plugins have been enabled:
rabbitmq_auth_backend_cache
rabbitmq_auth_backend_http
rabbitmq_stomp
rabbitmq_web_stomp

set 7 plugins.
Offline change; changes will take effect at broker restart.

  1. 配置 /etc/rabbitmq/rabbitmq.conf

在配置中追加:

auth_backends.1 = cache

auth_backends.1 = http

auth_backends.2 = internal

auth_cache.cached_backend = http

auth_http.http_method = post

auth_http.user_path = http://rabbitmq-auth:8080/auth/user
auth_http.vhost_path = http://rabbitmq-auth:8080/auth/vhost
auth_http.resource_path = http://rabbitmq-auth:8080/auth/resource
auth_http.topic_path = http://rabbitmq-auth:8080/auth/topic

auth_cache.cache_ttl = 60000

特别注意,后续需要提供 rabbitmq-auth 服务进行认证。

  1. 退出容器提交

退出容器后,使用容器当前状态提交为新的镜像,参考命令如下:

docker commit rabbitmq 镜像名:版本

后续使用镜像时,就可以直接使用插件提供的功能了。