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

Mac M1环境下Maven启动Testcontainers失败问题求助

Testcontainers启动Schema Registry容器失败问题排查

问题详情

使用Testcontainers创建confluentinc/cp-schema-registry:5.5.0容器时启动失败,相关信息如下:

容器内日志

[2022-08-16 07:22:52,694] INFO 172.29.0.1 - - [16/Aug/2022:07:22:52 +0000] "GET /info HTTP/1.1" 404 49  21 (io.confluent.rest-utils.requests)
[2022-08-16 07:22:53,708] ERROR Request Failed with exception  (io.confluent.rest.exceptions.DebuggableExceptionMapper)
javax.ws.rs.NotFoundException: HTTP 404 Not Found

本地日志

12:41:07.851 [main] ERROR 🐳 [confluentinc/cp-schema-registry:5.5.0] - Could not start container
org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [57338] should be listening)
    at org.testcontainers.containers.wait.strategy.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:90)

实现代码

public class SchemaRegistryContainer extends GenericContainer<SchemaRegistryContainer> {
  private static final int SCHEMA_PORT = 8081;

  public SchemaRegistryContainer(String version) {
    super("confluentinc/cp-schema-registry:" + version);
    withExposedPorts(8081);
    waitStrategy = Wait
        .forHttp("/info")
        .forStatusCode(200)
        .withStartupTimeout(Duration.ofMinutes(5));
  }
}

环境信息

  • OS:MacBook Air (M1, 2020) Version 12.1 Apple M1
  • Java版本:openjdk version "13.0.2" 2020-01-14
  • Testcontainers版本:1.17.2
  • Testcontainers JUnit Jupiter版本:1.17.2

问题原因及解决方法

核心原因

Schema Registry是依赖Kafka的服务,必须配置Kafka地址才能完成初始化。当前代码未关联Kafka容器,导致服务未完全启动,/info接口返回404,进而触发Testcontainers的启动超时。

修复方案

1. 关联Kafka容器启动

先启动Kafka容器,再为Schema Registry配置Kafka连接地址:

public class SchemaRegistryContainer extends GenericContainer<SchemaRegistryContainer> {
  private static final int SCHEMA_PORT = 8081;

  public SchemaRegistryContainer(String version, KafkaContainer kafkaContainer) {
    super("confluentinc/cp-schema-registry:" + version);
    withExposedPorts(8081);
    // 配置Schema Registry连接Kafka集群
    addEnv("SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());
    addEnv("SCHEMA_REGISTRY_HOST_NAME", "localhost");
    // 替换健康检查接口为/subjects,该接口在服务就绪时返回200
    waitStrategy = Wait
        .forHttp("/subjects")
        .forStatusCode(200)
        .withStartupTimeout(Duration.ofMinutes(5));
  }
}

使用示例:

// 初始化并启动Kafka容器
KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.0"));
kafka.start();

// 启动关联Kafka的Schema Registry容器
SchemaRegistryContainer schemaRegistry = new SchemaRegistryContainer("5.5.0", kafka);
schemaRegistry.start();

2. 调整健康检查接口

部分旧版本Schema Registry的/info接口并非标准就绪检查路径,即使配置了Kafka,也建议替换为/subjects(服务就绪时返回空数组或已存在的主题列表),避免因接口404导致启动超时。

3. M1芯片兼容处理

M1为ARM架构,旧版本confluentinc/cp-schema-registry镜像可能无ARM版本,可尝试:

  • 升级镜像版本至7.0.0及以上(官方从该版本开始提供ARM镜像)
  • 在Docker设置中启用Rosetta模拟x86架构(路径:Docker设置 -> Features in development -> 勾选"Use Rosetta for x86/amd64 emulation on Apple Silicon")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 16:36:22