Lambda函数运行在localstack Testcontainers上,无法连接到redis Testcontainers

sxissh06  于 2023-10-15  发布在  Redis
关注(0)|答案(2)|浏览(130)

我正在为AWS lambda函数编写集成测试,该函数通过KinesisEvent调用,然后将一些数据写入Redis和Kafka。我使用LocalStack和Testcontainers来实现同样的功能。
我已经用docker-compose.yml测试了代码,它工作得很好。但是Java测试中出现了一些问题。
这是我的容器设置-

@ClassRule
  public static final LocalStackContainer localstack =
      new LocalStackContainer(localstackImage)
          .withEnv("AWS_DEFAULT_REGION", US_EAST_1.getName())
              .withNetwork(lambdaNetwork)
          .withServices(Service.KINESIS, Service.LAMBDA);

  @ClassRule
  public static final KafkaContainer kafkaContainer =
      new KafkaContainer(kafkaImage).withNetwork(lambdaNetwork).withExposedPorts(9093);

    redisContainer = new GenericContainer<>(redisImage).withNetwork(lambdaNetwork).withExposedPorts(6379);
    redisContainer.start();

    String redisHost = redisContainer.getHost();
    Integer redisPort = redisContainer.getFirstMappedPort();

当我把记录放在kinesis上时,lambda被成功调用,但是我得到了以下异常-

2023-08-24T18:41:03.309  WARN --- [   asgi_gw_1] l.s.a.i.executor_endpoint  : Execution environment startup failed: {"errorMessage":"Failed to connect to any host resolved for DNS name.","errorType":"redis.clients.jedis.exceptions.JedisConnectionException"}

我也在建立集装箱网络。有人能指出我错过了什么吗?

mwngjboj

mwngjboj1#

具有以下定义将允许三个容器使用相同的网络

  • Postgres容器
  • LocalStack容器
  • LocalStack创建的Lambda容器
static Network network = Network.newNetwork();

    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine")
            .withNetwork(network)
            .withNetworkAliases("postgres");

    @Container
    static LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:2.2.0"))
            .withNetwork(network)
            .withEnv("LAMBDA_DOCKER_NETWORK", ((Network.NetworkImpl)network).getName())
            .withNetworkAliases("localstack");

关键部分是LocalStackContainer中的.withEnv("LAMBDA_DOCKER_NETWORK", ((Network.NetworkImpl)network).getName())
考虑使用以下信息从lambda连接到postgres(在本例中)

url=jdbc:postgresql://postgres:5432/test
username=test
password=test

网络别名postgres是在PostgresContainer中声明的,因此lambda应该使用它来通过Docker网络进行连接。

c0vxltue

c0vxltue2#

感谢Eddu!他的文章沿着了端口Map。

List<String> portBindings = new ArrayList<>(); portBindings.add("6379:6379"); // hostPort:containerPort 
redisContainer =
        new GenericContainer<>(redisImage)
            .withNetwork(lambdaNetwork)
            .withNetworkMode(lambdaNetwork.getId())
            .withCreateContainerCmdModifier(
                new Consumer<CreateContainerCmd>() {
                  @Override
                  public void accept(CreateContainerCmd createContainerCmd) {
                    createContainerCmd.withHostName("cache");
                    createContainerCmd.withName("cache");
                  }
                })
            .withExtraHost("host.docker.internal", "host-gateway")
            .withCommand("redis-server", "--save", "20", "1", "--loglevel", "warning")
            .withNetworkAliases("cache")
            .withExposedPorts(6379);
redisContainer.setPortBindings(portBindings);
redisContainer.start();

相关问题