【问题标题】:How to solve issue "Could not establish TCP connection to any of the configured hosts"如何解决“无法与任何已配置主机建立 TCP 连接”的问题
【发布时间】:2020-12-15 12:42:48
【问题描述】:

我是编程新手,我正在尝试设置我的 Rails 应用程序之间的关系。我正在使用 docker-compose 和 rabbitmq。这是我的代码:

Rails 应用中控制器中的一些代码

def some_method
  connection = Bunny.new(
    host: 'localhost',
    port: 5672,
    vhost: '/',
    user: 'guest',
    password: 'guest')
  connection.start
  channel = connection.create_channel
  queue = channel.queue('hello')
  channel.default_exchange.publish('Hello World!', routing_key: queue.name)
  puts " [x] Sent 'Hello World!'"
  connection.close
end

docker-compose.yml:

version: '3'

services: 
  db:
    image: postgres
  environment:
    POSTGRES_USER: root
    POSTGRES_PASSWORD: mysecretpassword
  volumes:
    - pgdata:/var/lib/postgresql/data
   
  web:
    build: .
    command: bundle exec rails s webrick -b '0.0.0.0'
    volumes:
      - .:/lab2_app
    ports:
      - "3000:3000"
    depends_on:
      - db
    tty: true
    stdin_open: true

rabbitmq 的 docker-compose.yml:

version: '3'
services:
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - 5672:5672
      - 15672:15672

我可以在浏览器中使用 RabbitMQ 管理,并且可以通过 curl 发送请求: curl http://localhost:15672 => AMQP

但是当我调用“some_method”时出现错误“无法与任何已配置的主机建立 TCP 连接”。

我想知道是否有人可以帮助我:)

【问题讨论】:

    标签: ruby-on-rails ruby docker-compose rabbitmq


    【解决方案1】:

    据我所知,当你在容器中运行它时 Rabbit 实例不是本地的 rails 而不是'localhost' 你应该使用'rabbitmq'

    【讨论】:

    • 我已经更改了主机:'localhost' -> 主机:'rabbitmq' 但它仍然无法正常工作 =( 我做对了吗?
    • 如果您还没有解决您的问题,请尝试添加链接:- rabbitmq 到您的网络
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2020-05-22
    • 2012-05-11
    • 2018-09-03
    • 2019-03-11
    • 2019-04-26
    相关资源
    最近更新 更多