[英]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.