【发布时间】: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