testcontainers中的kafkacontainer挂起,直到超时,并带有“等待容器端口打开超时”

hs1ihplo  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(352)

我正在尝试在testcontainers中启动一个kafka容器。我的代码如下所示:

import java.io.File;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public class FirstTest {

  @Container
  public static DockerComposeContainer environment =
      new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
          .withExposedService(
              "redis_1", 6379, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(60)));

  @Container
  public static KafkaContainer kafka =
      new KafkaContainer()
          .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(360)));

  @Container public PostgreSQLContainer postgresContainer = new PostgreSQLContainer();

  @Test
  void integrationTest() {

    String redisUrl =
        environment.getServiceHost("redis_1", 6379)
            + ":"
            + environment.getServicePort("redis_1", 6379);

    String jdbcUrl = postgresContainer.getJdbcUrl();
    String username = postgresContainer.getUsername();
    String password = postgresContainer.getPassword();

    String url = kafka.getBootstrapServers();
  }
}

运行此代码时,线程将挂起在运行状态,直到收到超时异常:

Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [32937, 32939] should be listening)

我想说的是,没有Kafka容器,一切都按预期进行。我能够成功启动redis和postgres容器。
这是我使用的Kafka容器版本:

<dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>kafka</artifactId>
      <version>1.14.3</version>
      <scope>test</scope>
 </dependency>
cetgtptt

cetgtptt1#

从我在源代码中看到的 waitingFor(Wait.forListeningPort()) 只有在以前暴露了某些端口时才起作用(不过,我不是100%确定。)
如果你只是创建一个Kafka容器而没有 waitingFor() 打电话?

相关问题