You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署NATS遇连接超时,咨询原配置错误原因

原Docker Compose配置的问题分析

问题背景

尝试连接Docker Compose部署的NATS服务时,调用CreateConnection方法抛出NATSConnectionException: timeout异常,相关C#代码如下:

try
{
    _connection = new ConnectionFactory().CreateConnection("nats://nats:4222");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

原Docker Compose配置:

version: '3.9'

services:
  consul-server:
    image: hashicorp/consul:1.13.1
    container_name: consul-server
    restart: always
    volumes:
      - ./consul/server.json:/consul/config/server.json:ro
    networks:
      - hashicorp
    ports:
      - "8500:8500"
      - "8600:8600/tcp"
      - "8600:8600/udp"
    command: "agent -bootstrap-expect=1"

  nats:
    image: nats
    ports:
      - "8222:8222"
    command: "--cluster_name NATS --cluster nats://0.0.0.0:6222 --http_port 8222 "
    networks:
      - nats

  nats-1:
    image: nats
    command: "--cluster_name NATS --cluster nats://0.0.0.0:6222 --routes=nats://ruser:T0pS3cr3t@nats:6222"
    networks:
      - nats
    depends_on:
      - nats

  nats-2:
    image: nats
    command: "--cluster_name NATS --cluster nats://0.0.0.0:6222 --routes=nats://ruser:T0pS3cr3t@nats:6222"
    networks:
      - nats
    depends_on:
      - nats

networks:
  hashicorp:
    driver: bridge
  nats:
    name: nats

我已通过修改配置并将NATS连接地址改为nats://localhost:14222解决问题,修改后的配置如下:

version: '3.9'

services:
  consul-server:
    image: hashicorp/consul:1.13.1
    container_name: consul-server
    restart: always
    volumes:
      - ./consul/server.json:/consul/config/server.json:ro
    networks:
      - hashicorp
    ports:
      - "8500:8500"
      - "8600:8600/tcp"
      - "8600:8600/udp"
    command: "agent -bootstrap-expect=1"

  nats-1:
    command:
      - "--debug"
      - "--cluster"
      - "nats://0.0.0.0:6222"
      - "--http_port"
      - "8222"
      - "--port"
      - "4222"
    image: "nats:2.1.4"
    networks:
      - main
    ports:
      - "14222:4222"
      - "18222:8222"
  nats-2:
    command:
      - "--debug"
      - "--cluster"
      - "nats://0.0.0.0:6222"
      - "--http_port"
      - "8222"
      - "--port"
      - "4222"
      - "--routes"
      - "nats://nats-1:6222"
    image: "nats:2.1.4"
    networks:
      - main
    ports:
      - "24222:4222"
      - "28222:8222"
  nats-3:
    command:
      - "--debug"
      - "--cluster"
      - "nats://0.0.0.0:6222"
      - "--http_port"
      - "8222"
      - "--port"
      - "4222"
      - "--routes"
      - "nats://nats-1:6222"
    image: "nats:2.1.4"
    networks:
      - main
    ports:
      - "34222:4222"
      - "38222:8222"

networks:
  hashicorp:
    driver: bridge
  main:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.255.0/24
      driver: default

原配置的问题点

  • 核心客户端端口未暴露到宿主机:原nats服务仅映射了监控用的8222端口,而NATS默认的客户端连接端口4222没有做宿主机端口映射。如果C#程序在宿主机运行,无法直接访问容器内部的4222端口,必然会超时。
  • 服务名无法被宿主机解析:代码中使用nats://nats:4222,其中nats是Docker内部的服务名,仅在Docker的nats网络内可解析。宿主机环境无法识别这个服务名,必须通过localhost加映射端口访问,或者将客户端程序也加入到nats网络中。
  • 集群路由认证不匹配:nats-1、nats-2的路由配置使用了ruser:T0pS3cr3t认证信息,但主nats服务并未配置对应的集群认证参数,会导致集群节点间无法正常组建连接,虽然这不是客户端连接超时的直接原因,但会引发集群功能异常。
  • 未固定NATS镜像版本:原配置使用image: nats,默认拉取latest版本,不同NATS版本的参数逻辑可能存在差异,版本不固定会增加部署的不确定性,修改后的配置指定了2.1.4版本,更稳定可控。

内容的提问来源于stack exchange,提问作者nop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 15:20:42