简体   繁体   English

Localstack - 通过 java aws sdk 访问 SNS:NoSuchFieldError:ENDPOINT_OVERRIDDEN

[英]Localstack - Access SNS via java aws sdk : NoSuchFieldError: ENDPOINT_OVERRIDDEN

I am playing around with a localstack instance running on docker.我正在玩一个在 docker 上运行的 localstack 实例。 I try to access the sns service, but receiving a error while executing methods like "listTopics()" or "createTopic()" I didnt find any help on Google yet, so may anyone has a idea whats wrong here ?我尝试访问 sns 服务,但是在执行“listTopics()”或“createTopic()”等方法时收到错误我还没有在 Google 上找到任何帮助,所以有人知道这里出了什么问题吗?

Error:错误:

Caused by: java.lang.NoSuchFieldError: ENDPOINT_OVERRIDDEN
    at com.amazonaws.services.sns.AmazonSNSClient.executeListTopics(AmazonSNSClient.java:1830) ~[aws-java-sdk-sns-1.11.875.jar:na]
    at com.amazonaws.services.sns.AmazonSNSClient.listTopics(AmazonSNSClient.java:1812) ~[aws-java-sdk-sns-1.11.875.jar:na]
    at com.amazonaws.services.sns.AmazonSNSClient.listTopics(AmazonSNSClient.java:1853) ~[aws-java-sdk-sns-1.11.875.jar:na]
    at com.example.dockerspringboot.DockerSpringBootApplication.getSnsClient(DockerSpringBootApplication.java:62) ~[classes/:na]
    at com.example.dockerspringboot.DockerSpringBootApplication$$EnhancerBySpringCGLIB$$a65fee3d.CGLIB$getSnsClient$2(<generated>) ~[classes/:na]
    at com.example.dockerspringboot.DockerSpringBootApplication$$EnhancerBySpringCGLIB$$a65fee3d$$FastClassBySpringCGLIB$$99ad7dfd.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at com.example.dockerspringboot.DockerSpringBootApplication$$EnhancerBySpringCGLIB$$a65fee3d.getSnsClient(<generated>) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 98 common frames omitted

I try to access the localstack sns service via the java aws sdk:我尝试通过 java aws sdk 访问 localstack sns 服务:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-sns</artifactId>
    <version>1.11.875</version>
</dependency>

The Code in my App where the error is thrown looks like this:我的应用程序中抛出错误的代码如下所示:

private String serviceEndpoint = "http://localhost:4566";
private String signingRegion = "eu-west-1";

@Bean
    public AmazonSNS getSnsClient() {
        AmazonSNS sns = AmazonSNSClient.builder().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, signingRegion)).withCredentials(new DefaultAWSCredentialsProviderChain()).build();

        System.out.println(sns.listTopics().toString());

        return sns;
    }

I am running localstack on Docker with this docker-compose file:我正在使用这个 docker-compose 文件在 Docker 上运行 localstack:

version: '2.1'

services:
  localstack:
    container_name: "localstack"
    image: localstack/localstack-full
    network_mode: bridge
    ports:
      - "4566-4599:4566-4599"
      - "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
    environment:
      - SERVICES=s3,sns
      - DEBUG=${DEBUG- }
      - DATA_DIR=/tmp/localstack/data
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock

    volumes:
      - "local_vol:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
volumes:
  local_vol:
      driver: local

Creating a SNS topic via cmdline works fine, so the service is running!通过 cmdline 创建 SNS 主题工作正常,因此服务正在运行!

/opt/code/localstack # aws --endpoint-url=http://localhost:4566 sns create-topic --name my_topic
{
    "TopicArn": "arn:aws:sns:us-east-1:000000000000:my_topic"
}
/opt/code/localstack # aws --endpoint-url=http://localhost:4566 sns list-topics
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:us-east-1:000000000000:my_topic"
        }
    ]
}

btw.: I do also use the S3 of localstock, this one is working - may those 2 conflict in anyway ?顺便说一句:我也使用 localstock 的 S3,这个正在工作 - 这两个可能有冲突吗?

Hope someone could help me, fix this.希望有人能帮助我,解决这个问题。 :) :)

I've also encountered this error for using the AmazonRoute53Client.我在使用 AmazonRoute53Client 时也遇到过这个错误。

Upon checking, there's no ENDPOINT_OVERRIDDEN field on the HandlerContextKey class.检查后,HandlerContextKey 类上没有 ENDPOINT_OVERRIDDEN 字段。

This class can be found on the aws-java-sdk-core library.此类可以在 aws-java-sdk-core 库中找到。 The ENDPOINT_OVERRIDDEN was added starting on 1.11.873. ENDPOINT_OVERRIDDEN 从 1.11.873 开始添加。

Solution: Update the aws-java-sdk-core to latest or at least 1.11.873解决方案:将 aws-java-sdk-core 更新到最新或至少 1.11.873

Github source code Github 源代码

Library: aws-java-sdk-core for version 1.11.873 库:版本 1.11.873 的 aws-java-sdk-core

Recently I encountered the above issue, and it was not because of LocalStack or any AWS local development docker image.最近遇到了上面的问题,不是LocalStack或者任何AWS本地开发的LocalStack镜像。 This issue is related to the version mismatch of com.amazonaws group dependencies with AWS core module.此问题与com.amazonaws组依赖项与 AWS 核心模块的版本不匹配有关。 This issue arises of the classpath AWS SDK dependencies don't match with the version of the AWS core module.此问题是由于类路径 AWS 开发工具包依赖项与 AWS 核心模块的版本不匹配而引起的。 To avoid this version conflict aws-java-sdk-bom should be used, and the BOM module will take care of the versioning problems.为避免此版本冲突,应使用aws-java-sdk-bom ,BOM 模块将处理版本控制问题。

Code Snippet代码片段

Let say if the latest version of aws-java-sdk-bom is 1.11.934 link假设最新版本的aws-java-sdk-bom1.11.934 链接

  • Maven马文
 <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-sns</artifactId>
    </dependency>
  <dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.934</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • Gradle摇篮
    dependencies {
        implementation 'com.amazonaws:aws-java-sdk-sns'
    }
    
    dependencyManagement {
        imports {
            mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.934'
        }
    }

I also run into the same problem.我也遇到了同样的问题。

The error comes from using 2 different AWS libraries that each depend on a different AWS SDK core versions.错误来自使用 2 个不同的 AWS 库,每个库都依赖于不同的 AWS SDK 核心版本。

We need to make sure that version numbers are consistent.我们需要确保版本号一致。 They are releasing updated versions every couple of days: https://github.com/aws/aws-sdk-java/releases他们每隔几天发布一次更新版本: https : //github.com/aws/aws-sdk-java/releases

The recommended way to use AWS dependencies with Maven is to use BOM that will synchronize AWS dependency versions.将 AWS 依赖项与 Maven 一起使用的推荐方法是使用将同步 AWS 依赖项版本的 BOM。 More information: https://aws.amazon.com/blogs/developer/managing-dependencies-with-aws-sdk-for-java-bill-of-materials-module-bom/更多信息: https : //aws.amazon.com/blogs/developer/managing-dependencies-with-aws-sdk-for-java-bill-of-materials-module-bom/

If anyone run into this, i couldnt find the actual problem here, but got it working with the amazon aws sdk for java 2.0 .如果有人遇到这个问题,我在这里找不到实际问题,但是可以使用 amazon aws sdk for java 2.0 来解决这个问题。

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>sns</artifactId>
            <version>2.15.3</version>
        </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM