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

通过Ocelot网关路由容器化微服务时出现502错误排查

问题分析与解决方案

核心问题:Docker网络隔离导致网关无法访问Identity服务

你同时混用了docker-compose和docker service(Swarm)部署服务,这两种方式默认处于不同的网络环境:

  • docker-compose创建的服务默认加入项目专属的桥接网络
  • docker service创建的服务默认加入Swarm的ingress overlay网络
    两个网络相互隔离,网关容器无法解析到identity主机,最终触发502错误。本地测试正常是因为所有服务处于同一网络环境下。

解决方案

方案1:统一用Docker Compose部署所有服务

放弃docker service命令,直接通过docker-compose管理网关和Identity服务,确保它们在同一个网络中:

  1. 保留现有docker-compose.yml配置(已包含identity和gateway)
  2. 在服务器上执行部署命令:
docker-compose up -d
  1. 验证网络连通性:进入gateway容器测试能否访问Identity服务
docker exec -it glb.gateway curl http://identity:5000/api/v1/identity-srv

如果能正常返回内容,说明网络连通正常,此时外部访问http://193.188.136.135:5173/api/v1/identity-srv即可正常工作。

方案2:统一用Swarm模式部署所有服务

如果需要使用Swarm集群能力,将所有服务部署到Swarm并加入同一个overlay网络:

  1. 创建共享overlay网络:
docker network create --driver overlay glb-network
  1. 修改docker-compose.yml,指定使用该网络:
version: "3.8"

networks:
  glb-network:
    external: true

services:
  identity:
    image: hadaou/glb.identity:1.0.9
    container_name: glb.identity
    hostname: identity
    ports:
      - 5000:5000
    environment:
      - RabbitMQSettings__Host=rabbitmq
      - MongoDbSettings__Host=mongo
      - SeqSettings__Host=seq
      - JaegerSettings__Host=jaeger
    networks:
      - glb-network
  gateway:
    image: hadaou/glb.gateway:1.0.18
    container_name: glb.gateway
    ports:
      - 5173:5173
    networks:
      - glb-network
  1. 使用Swarm部署服务:
docker stack deploy -c docker-compose.yml glb-stack
  1. 调整端口映射(如果需要通过80端口访问):
docker service update glb-stack_gateway --publish-add published=80,target=5173

额外排查步骤

  1. 检查Identity服务启动状态:
docker logs glb.identity

确认服务无启动报错,且正常监听5000端口。
2. 查看Ocelot网关日志:

docker logs glb.gateway

日志会包含具体错误信息(如DNS解析失败、连接超时等),帮助精准定位问题。
3. 调整Ocelot的BaseUrl:如果外部访问使用80端口,建议将BaseUrl改为http://193.188.136.135:80,或留空让Ocelot自动识别请求地址。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 15:15:39