0

我无法通过 java-aws-sdk、localstack 和 java-aws-kcl 设置我的本地环境。创建消费者并尝试在我的本地环境中运行它后,我收到一个错误,表明我的凭据不正确。所以 Kinesis 消费者总是去真正的 Amazon DynamoDB,我不能把它指向我的 localstack dynamodb。问题是:我怎样才能将它指向我当地的 dynamodb?

有什么建议么?

4

1 回答 1

1

您必须将 Kinesis 和 DynamoDB 客户端连接到 localstak。为此,您将需要 localStack 的 edgePort ,主机名。并形成一个本地堆栈边缘端口 URL。这里应该如何

public class public class Host {
    private static final String LOCAL = "localhost";
    public static final String DOCKER = "docker-desktop";

    public String getName(){
        ProcessBuilder processBuilder =
            new ProcessBuilder()
                .command("docker", "run", "--net", "host", "bash", "-c", "hostname", "-I");

        String hostname = null;
        try {
          Process process = processBuilder.start();
          BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
          hostname = reader.readLine().strip();
          int exitVal = process.waitFor();
          } catch (Exception e) {
            // log for exception
          }
        
         if (DOCKER.equals(hostname)) {
            return LOCAL_HOST;
        } else {
          return hostname;
        }
    }
}

现在您需要获取主机名并创建类似这样的 URL。

String localStackUrl =
        String.format("http://%s:%s", new Host().getName(), edgePort);

然后按如下方式创建客户端

SdkHttpClient httpClient = ApacheHttpClient.builder()
                                           .maxConnections(50)
                                           .build();

KinesisAsyncClient client =
        KinesisAsyncClient.builder()
            .region(Region.of(Localstack.getDefaultRegion()))
            .httpClient(httpClient)
            .credentialsProvider(provider)
            .endpointOverride(new URI(localStackUrl))
            .build();

  DynamoDbAsyncClient  dynamoClient =
        DynamoDbAsyncClient.builder()
            .region(Region.of(Localstack.getDefaultRegion()))
            .httpClient(httpClient)
            .credentialsProvider(provider)
            .endpointOverride(new URI(localStackUrl))
            .build();
于 2021-08-23T05:06:17.467 回答