简体   繁体   English

应用程序中的 Localstack AWS 端点

[英]Localstack AWS endpoint in application

I would like to run the functional tests for my application which tries to publish a message to AWS SNS.我想为尝试将消息发布到 AWS SNS 的应用程序运行功能测试。 I tried LocalStack and found that it does everything I need to mock and publish messages locally.我尝试了 LocalStack,发现它可以完成我在本地模拟和发布消息所需的一切。 But my application uses amazon sdk client for java and when I run it locally it still tries to post requests to amazon region instead of the LocalStack但是我的应用程序使用 amazon sdk client for java,当我在本地运行它时,它仍然尝试将请求发布到亚马逊区域而不是 LocalStack

What is the configuration required to make sure that the application interacts with the local stack instead of the AWS URL?确保应用程序与本地堆栈而不是 AWS URL 交互所需的配置是什么? Can we specify the endpoint URL in AWS config?我们可以在 AWS config 中指定端点 URL 吗? I found this to be a open issue in AWS CLI https://github.com/aws/aws-cli/issues/1270我发现这是 AWS CLI https://github.com/aws/aws-cli/issues/1270中的一个未解决问题

Is there any workaround that anyone has implemented for this?是否有人为此实施了任何解决方法?

To use your localstack, you have to set your EndpointConfiguration . 要使用您的本地堆栈,必须设置EndpointConfiguration

AmazonSNS amazonSNS = AmazonSNSClientBuilder.standard()
  .withEndpointConfiguration(new EndpointConfiguration("http://localhost:4575", "eu-west-1")) 
  .build();

You should also use localstack/localstack instead of atlassian/localstack (no longer actively maintained). 您还应该使用localstack / localstack而不是atlassian / localstack (不再进行主动维护)。

If you're using JUnit 5 for the tests, let me recommend you JUnit 5 extensions for AWS , a few JUnit 5 extensions that could be useful for testing AWS-related code.如果您使用 JUnit 5 进行测试,让我向您推荐适用于 AWS的 JUnit 5 扩展,一些 JUnit 5 扩展可能对测试 AWS 相关代码很有用。 These extensions can be used to inject clients for AWS service clients provided by tools like localstack (or the real ones).这些扩展可用于为本地堆栈(或真实工具)等工具提供的 AWS 服务客户端注入客户端。 Both AWS Java SDK v 2.x and v 1.x are supported:支持 AWS Java SDK v 2.x 和 v 1.x:

public static class Endpoint implements AWSEndpoint {
    @Override
    public String url() { return "http://localhost:4575"; }

    @Override
    public String region() { return "us-east-1"; }

    @Override
    public String accessKey() { return ""; }

    @Override
    public String secretKey() { return ""; }
}

@ExtendWith(SNS.class)
class AmazonDynamoDBInjectionTest {
    @AWSClient(endpoint = Endpoint.class)
    private AmazonSNS client;

    @Test
    void test() throws Exception {
        Assertions.assertNotNull(client);

        …
    }
}

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

相关问题 带有 Localstack 的 AWS S3 在 Java 应用程序中返回 500 - AWS S3 with Localstack return 500 in a Java application 从 Docker 连接到本地主机(Java 应用程序使用 mocking AWS 的 localstack) - Connecting to localhost from Docker (Java Application using localstack for mocking AWS) Localstack - 通过 java aws sdk 访问 SNS:NoSuchFieldError:ENDPOINT_OVERRIDDEN - Localstack - Access SNS via java aws sdk : NoSuchFieldError: ENDPOINT_OVERRIDDEN LocalStack 无法通过 Java 中的 AWS SES SDK 模拟发送电子邮件端点 - LocalStack fails for mocking send email endpoint through AWS SES SDK in Java 使用Apache Camel测试Localstack AWS - Testing Localstack AWS with Apache Camel Spring Aws SQS 未连接到本地堆栈 - Spring Aws SQS don't connect to localstack 在网络级别在 AWS 和 localstack 之间切换 - Switch between AWS and localstack on network level 无法使用测试容器的 localstack 模块连接到 Aws SQS - Unable to connect to Aws SQS using localstack module of test container Spring 集成测试 - 没有 RunWith 注释的自动装配类(AWS localstack) - Spring integration test - AutoWiring classes without RunWith annotation (AWS localstack) 如何保护Java应用程序将调用的AWS API Gateway端点? - How can I protect an AWS API Gateway endpoint that will be called by a Java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM