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

配置AWS Service Connect后ECS任务无法解析MongoDB域名,请求排查

问题:ECS Fargate中Spring Boot服务无法通过Service Connect解析MongoDB的DNS名称

背景与操作流程

我是AWS新手,计划在ECS Fargate上部署Spring Boot应用和MongoDB数据库,Spring集成了Mongock做数据库迁移。前期操作:

  • 创建Fargate集群
  • 构建Mongo镜像推送到ECR,创建任务定义并部署服务(最小1个任务),用Mongo任务的公网IP配置Spring应用,部署后功能正常,Mongock成功执行迁移
  • 为了解决动态IP问题,采用Service Connect实现DNS解析,配置如下:

MongoDB服务的Service Connect配置

{
  "enabled": true,
  "namespace": "test",
  "services": [
    {
      "portName": "27017mongo",
      "discoveryName": "user-mongo",
      "clientAliases": [
        {
          "port": 27017
        }
      ]
    }
  ]
}

确认DNS名称user-mongo.test已注册,服务中ecs-service-connect容器正常运行。

Spring Boot应用配置修改

data:
  mongodb:
    uri: mongodb://user-mongo.test:27017/iAutoService02
    auto-index-creation: true
    database: iAutoService02
    port: 27017
    host: user-mongo.test

Spring Boot服务的Service Connect配置

{
  "enabled": true,
  "namespace": "test",
  "services": [
    {
      "portName": "8083user",
      "discoveryName": "user-service",
      "clientAliases": [
        {
          "port": 8083
        }
      ]
    }
  ]
}

两个服务都在test命名空间下,Spring服务更新后ecs-service-connect容器也已启动,但任务启动失败,报错日志:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getBuilder' defined in io.mongock.runner.springboot.config.MongockContext: Unsatisfied dependency expressed through method 'getBuilder' parameter 0: Error creating bean with name 'connectionDriver' defined in class path resource [io/mongock/driver/mongodb/springdata/v4/config/SpringDataMongoV4Context.class]: Failed to instantiate [io.mongock.driver.api.driver.ConnectionDriver]: Factory method 'connectionDriver' threw exception with message: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=user-mongo.test:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: user-mongo.test}, caused by {java.net.UnknownHostException: user-mongo.test}}]

核心问题:Spring服务无法解析user-mongo.test主机名,导致无法连接MongoDB。

问题排查与解决方法

  • 确认任务网络模式:两个服务的任务定义必须使用awsvpc网络模式,Service Connect仅支持该模式,其他模式无法使用私有DNS解析。
  • 验证命名空间关联:检查test服务发现命名空间是否正确关联到你的Fargate集群,可通过CLI命令aws servicediscovery list-namespaces查看命名空间的集群关联信息。
  • 检查VPC DNS配置:确保任务所在VPC的DHCP选项集启用了Amazon DNS(默认配置),否则无法解析Service Connect的私有域名。
  • 核对端口名称匹配:MongoDB任务定义中的容器端口名称必须和Service Connect配置里的portName(即27017mongo)完全一致,大小写敏感,不匹配会导致代理无法正确转发流量。
  • 检查安全组规则:
    • Spring Boot服务的安全组需允许出站访问27017端口
    • MongoDB服务的安全组需允许来自Spring Boot服务(或Service Connect代理所在的安全组)的入站27017端口流量
  • 查看代理日志:检查Spring任务中ecs-service-connect容器的日志,确认代理已成功注册到test命名空间,没有启动失败或连接错误。
  • 尝试短名称解析:在Spring配置中使用user-mongo(不带.test后缀),同一命名空间下Service Connect支持短名称直接解析。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 19:18:26