等待 RabbitMQ docker 容器以 docker-maven-plugin 启动

Waiting for RabbitMQ docker container to start with docker-maven-plugin

如何告诉 docker-maven-plugin 在 运行 集成测试之前等待 RabbitMQ 容器完全启动?

这是我在 pom.xml:

中使用的插件配置
<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>start-rabbitmq-container</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <images>
                    <image>
                        <alias>rabbitmq</alias>
                        <name>rabbitmq:3.6.10-management</name>
                        <run>
                            <log>
                                <prefix>RABBITMQ</prefix>
                                <color>cyan</color>
                            </log>
                            <namingStrategy>alias</namingStrategy>
                            <ports>
                                <port>5672:5672</port>
                                <port>15672:15672</port>
                            </ports>
                        </run>
                    </image>
                </images>
            </configuration>
        </execution>
        <execution>
            <id>stop-rabbitmq-container</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

目前 IT 开始执行,而 RabbitMQ 仍在初始化并因服务器不可用而失败。

"While starting a container is it possible to block the execution until some condition is met"

https://dmp.fabric8.io/#start-wait

您可以 wait 使用 log:

从 RabbitMQ 容器输出一些日志

Regular expression which is applied against the log output of an container and blocks until the pattern is matched. You can use (?s) in the pattern to switch on multi line matching.

我设法找到了一种更可靠的方法来检查 RabbitMQ 的状态。当我使用 rabbitmq:3.6.10-management docker 图像时,我可以检查 localhost:15672 上的管理 url 是否已启动:

<wait>
    <http>
        <url>http://localhost:15672</url>
        <method>GET</method>
        <status>200..399</status>
    </http>
    <time>10000</time>
</wait>

wait配置会检查获取管理url的return值,最多10秒,直到它在指定的HTTP响应状态范围内,但RabbitMQ正常在 2 到 3 秒内启动。