LocalStack – 通过Java AWS SDK访问SNS:NoSuchFieldError:ENDPOINT_OVERRIDDEN

huangapple go评论71阅读模式
英文:

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

问题

错误信息:

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

代码部分:

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;
}

Docker Compose 文件部分:

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

通过命令行创建 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"
        }
    ]
}

希望有人能够帮助我解决这个问题。 LocalStack – 通过Java AWS SDK访问SNS: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(&lt;generated&gt;) ~[classes/:na]
	at com.example.dockerspringboot.DockerSpringBootApplication$$EnhancerBySpringCGLIB$$a65fee3d$$FastClassBySpringCGLIB$$99ad7dfd.invoke(&lt;generated&gt;) ~[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(&lt;generated&gt;) ~[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:

&lt;dependency&gt;
	&lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
	&lt;artifactId&gt;aws-java-sdk-sns&lt;/artifactId&gt;
	&lt;version&gt;1.11.875&lt;/version&gt;
&lt;/dependency&gt;

The Code in my App where the error is thrown looks like this:

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

@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: &#39;2.1&#39;

services:
  localstack:
    container_name: &quot;localstack&quot;
    image: localstack/localstack-full
    network_mode: bridge
    ports:
      - &quot;4566-4599:4566-4599&quot;
      - &quot;${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}&quot;
    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:
      - &quot;local_vol:/tmp/localstack&quot;
      - &quot;/var/run/docker.sock:/var/run/docker.sock&quot;
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
{
    &quot;TopicArn&quot;: &quot;arn:aws:sns:us-east-1:000000000000:my_topic&quot;
}
/opt/code/localstack # aws --endpoint-url=http://localhost:4566 sns list-topics
{
    &quot;Topics&quot;: [
        {
            &quot;TopicArn&quot;: &quot;arn:aws:sns:us-east-1:000000000000:my_topic&quot;
        }
    ]
}

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. LocalStack – 通过Java AWS SDK访问SNS:NoSuchFieldError:ENDPOINT_OVERRIDDEN

答案1

得分: 6

我也遇到了在使用AmazonRoute53Client时出现的这个错误。

经过检查,HandlerContextKey类上没有ENDPOINT_OVERRIDDEN字段。

这个类可以在aws-java-sdk-core库中找到。ENDPOINT_OVERRIDDEN是从1.11.873版本开始添加的。

解决方案:
将aws-java-sdk-core更新到最新版本,或者至少更新到1.11.873版本。

GitHub源代码

库:aws-java-sdk-core 版本 1.11.873

英文:

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

答案2

得分: 4

最近我遇到了上述问题,而且这并不是因为 LocalStack 或任何 AWS 本地开发 Docker 镜像引起的。这个问题与 com.amazonaws 集团依赖项与 AWS 核心模块的版本不匹配有关。这个问题出现是因为类路径中的 AWS SDK 依赖项与 AWS 核心模块的版本不匹配。为了避免这个版本冲突,应该使用 aws-java-sdk-bom,BOM 模块会处理版本问题。

代码片段

假设最新版本的 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'
        }
    }
英文:

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
 &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
      &lt;artifactId&gt;aws-java-sdk-sns&lt;/artifactId&gt;
    &lt;/dependency&gt;
  &lt;dependencies&gt;

&lt;dependencyManagement&gt;
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
			&lt;artifactId&gt;aws-java-sdk-bom&lt;/artifactId&gt;
			&lt;version&gt;1.11.934&lt;/version&gt;
			&lt;type&gt;pom&lt;/type&gt;
			&lt;scope&gt;import&lt;/scope&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
  • Gradle
    dependencies {
        implementation &#39;com.amazonaws:aws-java-sdk-sns&#39;
    }
    
    dependencyManagement {
        imports {
            mavenBom &#39;com.amazonaws:aws-java-sdk-bom:1.11.934&#39;
        }
    }

答案3

得分: 1

我也遇到了同样的问题。

错误是由于使用了两个不同的AWS库,每个库都依赖于不同的AWS SDK核心版本。

我们需要确保版本号一致。他们每隔几天就会发布更新版本:https://github.com/aws/aws-sdk-java/releases

在Maven中使用AWS依赖的推荐方法是使用BOM,它将同步AWS依赖的版本。更多信息:https://aws.amazon.com/blogs/developer/managing-dependencies-with-aws-sdk-for-java-bill-of-materials-module-bom/

英文:

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/

答案4

得分: 0

如果有人遇到这个问题,
我找不到实际的问题所在,但是使用了适用于Java 2.0的Amazon AWS SDK使其工作。

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

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 .

		&lt;dependency&gt;
			&lt;groupId&gt;software.amazon.awssdk&lt;/groupId&gt;
			&lt;artifactId&gt;sns&lt;/artifactId&gt;
			&lt;version&gt;2.15.3&lt;/version&gt;
		&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月5日 18:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64207005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定