You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Maven fabric8插件启动Docker本地DynamoDB,集成测试无法连接的问题及解决

本地Docker部署DynamoDB集成测试连接失败问题排查

问题场景

通过Maven在集成测试(IT)前启动Docker部署的本地DynamoDB,测试完成后停止。Docker Desktop显示容器已正常启动,但集成测试的Java代码无法连接到该服务。

相关代码片段

Maven配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <argLine>${failsafe.jacoco.args}</argLine>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.42.0</version>
            <executions>
                <execution>
                    <id>start-dynamodb-local</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <images>
                            <image>
                                <name>amazon/dynamodb-local</name>
                                <alias>dynamodb-local</alias>
                                <run>
                                    <ports>
                                        <port>1234:1234</port>
                                    </ports>
                                    <wait>
                                        <log>Initializing DynamoDB Local with the following configuration:</log>
                                        <time>1000</time>
                                    </wait>
                                </run>
                            </image>
                        </images>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-dynamodb-local</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                    <configuration>
                        <images>
                            <image>
                                <name>amazon/dynamodb-local</name>
                                <alias>dynamodb-local</alias>
                            </image>
                        </images>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Java测试代码

System.out.println(String.format("DynamoDB url: %s", dynamoURL));
AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(dynamoURL,null);
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
        .withEndpointConfiguration(endpointConfig)
        .build();

异常信息

代码打印出DynamoDB url: http://127.0.0.1:1234,但抛出以下异常:

com.amazonaws.SdkClientException: Unable to execute HTTP request: The target server failed to respond
        at com.p81.delta.gw.DynamoDBPersistenceManagerIT.createTable(DynamoDBPersistenceManagerIT.java:48)
        at com.p81.delta.gw.DynamoDBPersistenceManagerIT.before(DynamoDBPersistenceManagerIT.java:37)
Caused by: org.apache.http.NoHttpResponseException: The target server failed to respond
        at com.p81.delta.gw.DynamoDBPersistenceManagerIT.createTable(DynamoDBPersistenceManagerIT.java:48)
        at com.p81.delta.gw.DynamoDBPersistenceManagerIT.before(DynamoDBPersistenceManagerIT.java:37)

原因分析及解决方法

问题核心是端口映射配置错误:

  • Amazon DynamoDB Local镜像默认监听的端口是8000,而非配置中使用的1234。
  • 虽然Maven配置将宿主机的1234端口映射到容器的1234端口,但容器内的DynamoDB服务并未在1234端口启动,导致宿主机请求该端口时无响应。

解决方式:将Maven中的端口映射改为<port>8000:8000</port>,同时Java代码中的dynamoURL调整为http://127.0.0.1:8000,即可正常连接服务。


内容的提问来源于stack exchange,提问作者balderman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:08:19