使用 localstack

问题描述

问题

我正在尝试启动 localstack 容器,以便在特定端口上公开 SSM 系统。但是,根据 docs I have read 我应该能够在环境中指定 SSM_PORT_EXTERNAL ;这似乎不起作用。

我的尝试

在我的 gradle 文件中,我尝试了以下操作:

test {
    // AWS Properties for tests
    systemProperty 'SSM_PORT_EXTERNAL','22680'
    useJUnitPlatform()
    ...
}

在我的容器启动代码中,我也尝试过

        static final LocalStackContainer INSTANCE = new AwsTestContainer()
            .withServices(SSM,S3)
            .withEnv(SSM_PORT_EXTERNAL,"22680")
            .withStartupAttempts(3)
            .withStartupTimeout(Duration.ofSeconds(60L));

AWSTestContainer 扩展了 LocalStackContainer

在这两种情况下,localstack 都启动但忽略了我指定的端口,并且我收到错误,因为 SSM 客户端无法连接到端口 22680。查看日志似乎 LocalStack 仍在“55003”端口上启动。

12:22:16.526 [Test worker] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd:org.testcontainers.shaded.com.github.dockerjava.core.command.CreateContainerCmdImpl @66a7cb59[name=,hostName=,domainName=,user=,attachStdin=,attachStdout=,attachStderr=,portSpecs=,tty=,stdinopen=,stdInOnce=,env={HOSTNAME_EXTERNAL=localhost,SERVICES=ssm,s3,SSM_PORT_EXTERNAL =22680},cmd={},healthcheck=,argsEscaped=,entrypoint=,image=company_name.jfrog.io/localstack/localstack:latest,volumes=com.github.dockerjava.api.model.Volumes@6fc8387e,workingDir=,macAddress=,onBuild=,networkdisabled=,exposedPorts=com.github.dockerjava.api.model.ExposedPorts@10cdb47a,stopSignal=,stopTimeout=,hostConfig=HostConfig(binds=[/var/run/docker.sock:/var /run/docker.sock:rw],blkioWeight=null,blkioWeightDevice=null,blkioDeviceReadBps=null,blkioDeviceWriteBps=null,blkioDeviceReadiops=null,blkioDeviceWriteIOps=null,memorySwappiness=null,nanocpus=null,capAdd=null,capDrop=null,容器IDFi le=null,cpuPeriod=null,cpuRealtimePeriod=null,cpuRealtimeRuntime=null,cpuShares=null,cpuQuota=null,cpusetcpus=null,cpusetMems=null,devices=null,deviceCgroupRules=null,deviceRequests=null,diskQuota=null,dns= null,dnsOptions=null,dnsSearch=null,extraHosts=[],groupAdd=null,ipcMode=null,cgroup=null,links=[],logConfig=LogConfig(type=null,config=null),lxcConf=null,内存=null,memorySwap=null,memoryReservation=null,kernelMemory=null,networkMode=null,oomKilldisable=null,init=null,autoRemove=null,oomscoreAdj=null,portBindings={},privileged=null,publishAllPorts=true,readonlyRootfs= null、restartPolicy=null、ulimits=null、cpuCount=null、cpuPercent=null、ioMaximumIOps=null、ioMaximumBandwidth=null、volumesFrom=[]、mounts=null、pidMode=null、isolation=null、securityOpts=null、storageOpt=null,cgroupParent=null,volumeDriver=null,shmSize=null,pidsLimit=null,runtime=null,tmpFs=null,utSMode=null,usernsMode=null,sysctls=null,consoleSize=null),labels={org.testcon tainers=true,org.testcontainers.sessionId=6c80eb4d-594d-49e1-9a81-2a933aa5d8e4},shell=,networkingConfig=,ipv4Address=,ipv6Address=,aliases=,authConfig=AuthConfig(username=myemail@service.com,email null,registryAddress=company_name.jfrog.io,stackorchestrator=null)]

版本

Spring Boot:2.4.1 使用 JUnit 5

LocalStack:最新(LocalStackContainer.java 中的 DEFAULT_TAG 为“0.11.2”)

IntelliJ:2020.3

Gradle:6.7.1


                    Gradle 6.7.1

构建时间:2020-11-16 17:09:24 UTC 修订版:2972ff02f3210d2ceed2f1ea880f026acfbab5c0

科特林:1.3.72

Groovy:2.5.12

Ant:2020 年 5 月 10 日编译的 Apache Ant(TM) 版本 1.10.8

JVM:15.0.1(采用OpenJDK 15.0.1+9)

操作系统:Mac OS X 10.15.7 x86_64

解决方法

对于localstack,我们还需要设置环境变量来指定从哪里启动服务。 就像在 docker 中一样,我们使用 yaml 配置。 对于弹簧靴:尝试类似

.withEnv("DEFAULT_REGION","eu-central-1")
  .withEnv("EDGE_PORT","4576")
  .withEnv("SQS_PORT_EXTERNAL","4576")
  .withServices(LocalStackContainer.Service.SQS);