简体   繁体   中英

Localstack - Access SNS via java aws sdk : NoSuchFieldError: ENDPOINT_OVERRIDDEN

I am playing around with a localstack instance running on docker. 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 ?

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:

<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:

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!

/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 ?

Hope someone could help me, fix this. :)

I've also encountered this error for using the AmazonRoute53Client.

Upon checking, there's no ENDPOINT_OVERRIDDEN field on the HandlerContextKey class.

This class can be found on the aws-java-sdk-core library. The ENDPOINT_OVERRIDDEN was added starting on 1.11.873.

Solution: Update the aws-java-sdk-core to latest or at least 1.11.873

Github source code

Library: aws-java-sdk-core for version 1.11.873

Recently I encountered the above issue, and it was not because of LocalStack or any AWS local development docker image. This issue is related to the version mismatch of com.amazonaws group dependencies with AWS core module. This issue arises of the classpath AWS SDK dependencies don't match with the version of the AWS core module. To avoid this version conflict aws-java-sdk-bom should be used, and the BOM module will take care of the versioning problems.

Code Snippet

Let say if the latest version of aws-java-sdk-bom is 1.11.934 link

  • 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.

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

The recommended way to use AWS dependencies with Maven is to use BOM that will synchronize AWS dependency versions. More information: 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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