获得服务:亚马逊运动;状态代码:502,带有apache flink和localstack kinesis

wko9yo5t  于 2021-06-24  发布在  Flink
关注(0)|答案(3)|浏览(358)

我的本地设置包括 local apache-flink (通过brew安装)和 localstack 在kinesis服务运行的情况下。
我的 Docker

localstack:
    image: localstack/localstack:0.10.7
    environment:
      - SERVICES=kinesis
    ports:
      - "4568:4568"

还有我的运动消费者:

kinesisConsumerConfig.setProperty(ConsumerConfigConstants.AWS_ACCESS_KEY_ID, "123");
kinesisConsumerConfig.setProperty(ConsumerConfigConstants.AWS_SECRET_ACCESS_KEY, "123");
kinesisConsumerConfig.setProperty(ConsumerConfigConstants.AWS_ENDPOINT, "http://localhost:4568");

但是当我运行flink程序时,我得到了一个错误:
原因:org.apache.flink.kinesis.shaded.com.amazonaws.services.kinesis.model.amazonkinesexception:null(服务:amazonkinesis;状态代码:502;错误代码:空;请求id:空)
只有在使用 localstack . 如果我用我的aws帐户连接到我的kinesis流,它会工作得很好。

9lowa7mx

9lowa7mx1#

如果您使用的是java,那么可以使用 jar 一些amazon组件的库:
首先,您需要在 pom.xml 为了能够在测试期间直接初始化localstack:

<dependency>
    <groupId>cloud.localstack</groupId>
    <artifactId>localstack-utils</artifactId>
    <version>0.2.0</version>
    <scope>test</scope>
</dependency>

然后,如果需要使用 kinesis 以及 dynamo ,因为最新的一个 aws 与最新版本的localstack不兼容:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    <version>1.11.642</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>amazon-kinesis-client</artifactId>
    <version>1.8.10</version>
    <scope>test</scope>
</dependency>

现在您可以使用以下注解来使用docker示例化堆栈,如果系统中不存在图像,则会自动提取图像。因此,不需要运行任何docker/docker compose映像。

@LocalstackDockerProperties(services = {"kinesis", "dynamodb"})
@ExtendWith(LocalstackDockerExtension.class)
@Slf4j
public class TestPipelineComplete {

public static final String AWS_ACCESS_KEY_ID = "foo";
public static final String AWS_SECRET_ACCESS_KEY = "bar";
    static {
        System.setProperty("AWS_ACCESS_KEY_ID", AWS_ACCESS_KEY_ID);
        System.setProperty("AWS_SECRET_ACCESS_KEY", AWS_SECRET_ACCESS_KEY);
        // These two lines are fundamental
        cloud.localstack.TestUtils.setEnv("aws.cborEnabled", "false");
        cloud.localstack.TestUtils.setEnv("AWS_CBOR_DISABLE", "true");
    }
}

现在,如果你需要初始化 DynamoDB 客户端可以使用以下行:

final AmazonDynamoDB clientDynamoDB = cloud.localstack.TestUtils.getClientDynamoDB();

现在,如果你需要初始化 Kinesis 客户端,您可以使用以下行:

final AmazonKinesis kinesisClient = cloud.localstack.TestUtils.getClientKinesis();

如果需要从kinesis(testpurpouse)读取数据,可以使用以下代码段作为模板(https://gist.github.com/alessiosavi/4ea88d73d6853de695843631207b7bc6):

package org.example;

import com.amazonaws.services.kinesis.AmazonKinesis;
import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
import com.amazonaws.services.kinesis.model.*;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class App {

    private static final String streamName = "API_NAME" + "_kineis-notification-stream";
    private static final AmazonKinesis client = AmazonKinesisClientBuilder.defaultClient();

    public static void main(String[] args) {
        printKinesisRecords(getRecordsFromKinesis(client));
    }

    private static List<Record> getRecordsFromKinesis(AmazonKinesis kClient) {
        final ListShardsRequest listShardsRequest = new ListShardsRequest().withStreamName(streamName).withMaxResults(1);

        Shard shard = kClient.listShards(listShardsRequest).getShards().get(0);
        GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest();
        getShardIteratorRequest.setStreamName(streamName);
        getShardIteratorRequest.setShardId(shard.getShardId());
        getShardIteratorRequest.setShardIteratorType("TRIM_HORIZON");

        final GetShardIteratorResult getShardIteratorResult = kClient.getShardIterator(getShardIteratorRequest);
        String shardIterator = getShardIteratorResult.getShardIterator();

        // Create a new getRecordsRequest with an existing shardIterator
        // Set the maximum records to return to 1
        GetRecordsRequest getRecordsRequest = new GetRecordsRequest();
        getRecordsRequest.setShardIterator(shardIterator);
        getRecordsRequest.setLimit(10);

        final GetRecordsResult result = kClient.getRecords(getRecordsRequest);
        // Put the result into record list. The result can be empty.
        return result.getRecords();
    }

    private static void printKinesisRecords(List<Record> records) {
        for (Record record : records) {
            System.err.println("RECORD: " + StandardCharsets.UTF_8.decode(record.getData()).toString());
        }
    }
}
oknwwptz

oknwwptz2#

在添加flinkkinesisconsumer作为源之前,请添加以下行: System.setProperty("com.amazonaws.sdk.disableCbor", "true") System.setProperty("org.apache.flink.kinesis.shaded.com.amazonaws.sdk.disableCbor", "true") 这与导出env变量的效果相同,但将其放入代码中可以减少设置环境所花费的时间。

whhtz7ly

whhtz7ly3#

我们需要通过env var禁用cbor和cert检查,并在同一控制台中启动flink

export AWS_CBOR_DISABLE=1
DISABLE_CERT_CHECKING_JAVA_OPTS="-Dorg.apache.flink.kinesis.shaded.com.amazonaws.sdk.disableCertChecking"
export FLINK_ENV_JAVA_OPTS=${DISABLE_CERT_CHECKING_JAVA_OPTS}
/usr/local/Cellar/apache-flink/1.9.1/libexec/bin/start-cluster.sh

相关问题