首页 > 解决方案 > 使用 localstack 设置服务端口

问题描述

问题

我正在尝试启动 localstack 容器,以便将 SSM 系统暴露在特定端口上。但是,根据我读过的文档,我应该能够在环境中指定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 [测试工作者]调试 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, containerIDFile=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, memory=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.testcontainers=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)]consoleSize=null),labels={org.testcontainers=true, org.testcontainers.sessionId=6c80eb4d-594d-49e1-9a81-2a933aa5d8e4},shell=,networkingConfig=,ipv4Address=,ipv6Address=,aliases=,authConfig=AuthConfig(用户名=myemail@service.com,电子邮件=null,registryAddress=company_name.jfrog.io,stackOrchestrator=null)]consoleSize=null),labels={org.testcontainers=true, org.testcontainers.sessionId=6c80eb4d-594d-49e1-9a81-2a933aa5d8e4},shell=,networkingConfig=,ipv4Address=,ipv6Address=,aliases=,authConfig=AuthConfig(用户名=myemail@service.com,电子邮件=null,registryAddress=company_name.jfrog.io,stackOrchestrator=null)]

版本

Spring Boot:2.4.1 使用 JUnit 5

LocalStack:最新(在 LocalStackContainer.java 中,DEFAULT_TAG 为 '0.11.2')

智能:2020.3

摇篮:6.7.1


                    Gradle 6.7.1

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

科特林:1.3.72

时髦的: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

标签: javalocalstack

解决方案


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

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

推荐阅读