gpt4 book ai didi

ruby-on-rails - Rails将请求发送到其他容器[无法打开TCP连接]

转载 作者:行者123 更新时间:2023-12-02 20:01:27 24 4
gpt4 key购买 nike

我正在尝试通过多个Rails node + docker + MySQL + Redis创建多个节点分发系统。

因此,我的主节点需要与其他节点进行通信。

这是我的 docker-compose.yml

**version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=pubsub_1_development
- MYSQL_USER=appuser
- MYSQL_PASSWORD=password
ports:
- "3308:3306"

redis:
image: 'redis:4.0-alpine'

app:
image: pubsub_2:1.0.8
command: /bin/sh -c "rm -f ./tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/pubsub_2"
ports:
- "3001:3000"
depends_on:
- db
- redis
- sub_node
- pub_node
links:
- db
- redis
- sub_node
- pub_node
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
SUB_NODE: 0.0.0.0:4001
PUB_NODE: 0.0.0.0:4000
stdin_open: true
tty: true

sub_node:
image: sub_node:1.0.1
command: /bin/sh -c "rm -f /sub_node/tmp/pids/server.pid && bundle exec rails s -p 4001 -b '0.0.0.0'"
ports:
- "4001:4001"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4001"


pub_node:
image: pub_node:1.0.1
command: /bin/sh -c "rm -f /pub_node/tmp/pids/server.pid && bundle exec rails s -p 4000 -b '0.0.0.0'"
ports:
- "4000:4000"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4000"**

但是,当我尝试使用 app节点将请求发送到 pub_node时,抛出了此错误。
Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:4000 (Connection refused - connect(2) for "127.0.0.1" port 4000)
from /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect'

我正在通过此代码执行 post
rvlist = '127.0.0.1:4000'
HTTParty.post("http://#{rvlist}/publish", options)

它可以在没有docker环境的情况下以我的开发模式运行。

最佳答案

在docker-compose中联网:

Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.



引用: https://docs.docker.com/compose/networking/

在您的情况下,docker-compose中的任何容器都可以使用其容器名称和端口 pub_node打开与 pub_node:4000容器的TCP连接。

关于ruby-on-rails - Rails将请求发送到其他容器[无法打开TCP连接],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com