Docker 和 Rabbitmq:容器之间的 ECONNREFUSED
Docker and Rabbitmq: ECONNREFUSED between containers
我正在尝试为 rabbitmq 和容器的消费者设置单独的 docker 容器,即监听队列并执行必要任务的进程。我创建了 yml 文件和 docker 文件。
我能够 运行 yml 文件,但是当我检查 docker-compose 日志时,我看到哪里有 ECONNREFUSED 错误。
NewUserNotification.js:
require('seneca')()
.use('seneca-amqp-transport')
.add('action:new_user_notification’, function(message, done) {
…
return done(null, {
pid: process.pid,
status: `Process ${process.pid} status: OK`
})
.listen({
type: 'amqp',
pin: ['action:new_user_notification’],
name: 'seneca.new_user_notification.queue',
url: process.env.AMQP_RECEIVE_URL,
timeout: 99999
});
docker-撰写日志中的错误消息:
{"notice":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.","code":
"act_execute","err":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"act_execute","syscall":"connect","address":"127.0.0.1",
"port":5672,"eraro":true,"orig":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},
"seneca":true,"package":"seneca","msg":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.",
"details":{"message":"connect ECONNREFUSED 127.0.0.1:5672","pattern":"hook:listen,role:transport,type:amqp","instance":"Seneca/…………/…………/1/3.4.3/-“,
”orig$":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},"isOperational":true,
"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672}
示例docker-compose.yml 文件:
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- /rabbitmq/lib:/var/lib/rabbitmq
- /rabbitmq/log:/var/log/rabbitmq
- /rabbitmq/conf:/etc/rabbitmq/
account:
container_name: "account"
build:
context: .
dockerfile: ./Account/Dockerfile
ports:
- 3000:3000
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: ./Account/dev.newusernotification.Dockerfile
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "90", "--", "node", “newusernotification.js"]
amqp 连接字符串:
(我尝试了两种方法,有和没有 user/pass)
amqp://用户名:密码@rabbitmq:5672
我将 link 属性添加到 docker-compose 文件,并在 .env 文件 (rabbitmq) 中引用了名称。我尝试从容器外部 运行 NewUserNotification.js 文件,它开始正常。是什么导致了这个问题?连接字符串问题? Docker-Compose.yml 配置问题?其他?
似乎环境变量 AMQP_RECEIVE_URL
构造不正确。根据错误日志,侦听器正在尝试连接到不是 rabbitmq 服务容器 IP 的 localhost(127.0.0.1)。查找工作示例的修改配置。
1 docker-compose.yml
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- ./rabbitmq/lib:/var/lib/rabbitmq
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: Dockerfile
env_file:
- ./un.env
links:
- rabbitmq
depends_on:
- rabbitmq
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "120", "--", "node", "newusernotification.js"]
2 un.env
AMQP_RECEIVE_URL=amqp://guest:guest@rabbitmq:5672
请注意,我已使用 env_file
将 AMQP_RECEIVE_URL
作为环境变量传递给 new_user_notification
服务,并删除了 account
服务
3 个 Dockerfile
FROM node:7
WORKDIR /app
COPY newusernotification.js /app
COPY wait-for-it.sh /app
RUN npm install --save seneca
RUN npm install --save seneca-amqp-transport
4 newusernotification.js
在问题中使用相同的文件。
在尝试从消费服务建立连接时,您的 RabbitMQ 服务可能未完全启动。
如果是这种情况,在 Docker Compose 中,您可以 wait
使用名为 dadarek/wait-for-dependencies.
的容器启动服务
1). 添加新服务 waitforrabbit
到您的 docker-compose.yml
waitforrabbit:
image: dadarek/wait-for-dependencies
depends_on:
- rabbitmq
command: rabbitmq:5672
2). 将此服务包含在需要 RabbitMQ 启动的服务的 depends_on
部分中。
depends_on:
- waitforrabbit
3). 启动 compose
docker-compose run --rm waitforrabbit
docker-compose up -d account new_user_notification
以这种方式开始撰写将实质上等待 RabbitMQ 完全启动,然后才能建立来自使用服务的连接。
我正在尝试为 rabbitmq 和容器的消费者设置单独的 docker 容器,即监听队列并执行必要任务的进程。我创建了 yml 文件和 docker 文件。
我能够 运行 yml 文件,但是当我检查 docker-compose 日志时,我看到哪里有 ECONNREFUSED 错误。
NewUserNotification.js:
require('seneca')()
.use('seneca-amqp-transport')
.add('action:new_user_notification’, function(message, done) {
…
return done(null, {
pid: process.pid,
status: `Process ${process.pid} status: OK`
})
.listen({
type: 'amqp',
pin: ['action:new_user_notification’],
name: 'seneca.new_user_notification.queue',
url: process.env.AMQP_RECEIVE_URL,
timeout: 99999
});
docker-撰写日志中的错误消息:
{"notice":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.","code":
"act_execute","err":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"act_execute","syscall":"connect","address":"127.0.0.1",
"port":5672,"eraro":true,"orig":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},
"seneca":true,"package":"seneca","msg":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.",
"details":{"message":"connect ECONNREFUSED 127.0.0.1:5672","pattern":"hook:listen,role:transport,type:amqp","instance":"Seneca/…………/…………/1/3.4.3/-“,
”orig$":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},"isOperational":true,
"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672}
示例docker-compose.yml 文件:
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- /rabbitmq/lib:/var/lib/rabbitmq
- /rabbitmq/log:/var/log/rabbitmq
- /rabbitmq/conf:/etc/rabbitmq/
account:
container_name: "account"
build:
context: .
dockerfile: ./Account/Dockerfile
ports:
- 3000:3000
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: ./Account/dev.newusernotification.Dockerfile
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "90", "--", "node", “newusernotification.js"]
amqp 连接字符串: (我尝试了两种方法,有和没有 user/pass) amqp://用户名:密码@rabbitmq:5672
我将 link 属性添加到 docker-compose 文件,并在 .env 文件 (rabbitmq) 中引用了名称。我尝试从容器外部 运行 NewUserNotification.js 文件,它开始正常。是什么导致了这个问题?连接字符串问题? Docker-Compose.yml 配置问题?其他?
似乎环境变量 AMQP_RECEIVE_URL
构造不正确。根据错误日志,侦听器正在尝试连接到不是 rabbitmq 服务容器 IP 的 localhost(127.0.0.1)。查找工作示例的修改配置。
1 docker-compose.yml
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- ./rabbitmq/lib:/var/lib/rabbitmq
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: Dockerfile
env_file:
- ./un.env
links:
- rabbitmq
depends_on:
- rabbitmq
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "120", "--", "node", "newusernotification.js"]
2 un.env
AMQP_RECEIVE_URL=amqp://guest:guest@rabbitmq:5672
请注意,我已使用 env_file
将 AMQP_RECEIVE_URL
作为环境变量传递给 new_user_notification
服务,并删除了 account
服务
3 个 Dockerfile
FROM node:7
WORKDIR /app
COPY newusernotification.js /app
COPY wait-for-it.sh /app
RUN npm install --save seneca
RUN npm install --save seneca-amqp-transport
4 newusernotification.js
在问题中使用相同的文件。
在尝试从消费服务建立连接时,您的 RabbitMQ 服务可能未完全启动。
如果是这种情况,在 Docker Compose 中,您可以 wait
使用名为 dadarek/wait-for-dependencies.
1). 添加新服务 waitforrabbit
到您的 docker-compose.yml
waitforrabbit:
image: dadarek/wait-for-dependencies
depends_on:
- rabbitmq
command: rabbitmq:5672
2). 将此服务包含在需要 RabbitMQ 启动的服务的 depends_on
部分中。
depends_on:
- waitforrabbit
3). 启动 compose
docker-compose run --rm waitforrabbit
docker-compose up -d account new_user_notification
以这种方式开始撰写将实质上等待 RabbitMQ 完全启动,然后才能建立来自使用服务的连接。