将RabbitMQ添加到Docker中:错误:连接ECONNREFUSED 192.168.80.2:5672

huangapple go评论69阅读模式
英文:

Add rabbitmq to Docker: Error: connect ECONNREFUSED 192.168.80.2:5672

问题

当运行命令docker compose up -d --build并检查网关服务的日志时,出现错误

错误: 连接ECONNREFUSED 192.168.80.2:5672
在TCPConnectWrap.afterConnect [as oncomplete] (node:net:1229:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.80.2',
port: 5672
}

docker:

message_broker:
image: rabbitmq:3.10-management-alpine
hostname: rabbitmq
restart: unless-stopped
volumes:
- message_broker_data:/var/lib/rabbitmq
ports:
- 15672:15672
networks:
- tms

gateway:
container_name: gateway
stdin_open: true
tty: true
build:
context: ./micro_services/gateway
dockerfile: Dockerfile
depends_on:
- storage
volumes:
- ./micro_services/gateway/src/:/app/src/
- ./micro_services/gateway/public/:/app/public/
- tms_logs:/var/log/gateway
environment:
PORT: 3000
STORAGE_URL: 'http://storage:3000'
LOGS_PATH: '/var/log/gateway'
ports:
- '3000:3000'
networks:
- tms

并且我使用了amqplib

module.exports = async () => {
try {
const queue = 'tasks'
const conn = await amqplib.connect('amqp://message_broker:5672')
// 其余部分未提供,请补充
}

英文:

When run command docker compose up -d --build and check logs of gateway service then get an error

Error: connect ECONNREFUSED 192.168.80.2:5672
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1229:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.80.2',
port: 5672
}

docker:

 message_broker:
    image: rabbitmq:3.10-management-alpine
    hostname: rabbitmq
    restart: unless-stopped
    volumes:
      - message_broker_data:/var/lib/rabbitmq
    ports:
      - 15672:15672
    networks:
      - tms

 gateway:
    container_name: gateway
    stdin_open: true
    tty: true
    build:
       context: ./micro_services/gateway
       dockerfile: Dockerfile
    depends_on:
       - storage
    volumes:
       - ./micro_services/gateway/src/:/app/src/
       - ./micro_services/gateway/public/:/app/public/
       - tms_logs:/var/log/gateway
    environment:
       PORT: 3000
       STORAGE_URL: 'http://storage:3000'
       LOGS_PATH: '/var/log/gateway'
    ports:
       - '3000:3000'
    networks:
       - tms

and i use amqplib

module.exports = async () => {
    try {
const queue = 'tasks'
const conn = await amqplib.connect('amqp://message_broker:5672')

答案1

得分: 1

以下是翻译好的部分:

我添加类似以下内容的代码

async function connectWithRetry(url, options) {
  let retries = options.retries
  let delay = options.delay
  let lastError

  while (retries > 0) {
    try {
      const conn = await amqplib.connect(url)
      return conn
    } catch (error) {
      lastError = error
      console.log(`连接失败。剩余重试次数:${retries}`)
      await sleep(delay)
      retries--
    }
  }

  throw lastError
}

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

或者简单的方式是

setTimeout(async () => {
  await initAmqp() // 在这个函数中包含来自amqp的逻辑
  console.log('消息代理成功启动')
}, 10000)
英文:

I add something like it

async function connectWithRetry(url, options) {
   let retries = options.retries
   let delay = options.delay
   let lastError

   while (retries > 0) {
     try {
  const conn = await amqplib.connect(url)
  return conn
} catch (error) {
  lastError = error
  console.log(`Connection failed. Retries left: ${retries}`)
  await sleep(delay)
  retries--
  }
}

throw lastError
}

  function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms))
 }

or the simple way is

 setTimeout(async () => {
    await initAmqp() // in this func a logic from amqp
    console.log('message brocker started successfully')
  }, 10000)

huangapple
  • 本文由 发表于 2023年6月30日 05:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定