docker - 无法从同一网络中的另一个测试容器连接到 neo4j 测试容器

标签 docker neo4j integration-testing microservices testcontainers

这里有一个在同一网络中创建测试容器的很好的示例:Can Testcontainers create docker network for me if it does not exist?

我的环境有点不同,不幸的是我没有获得两个容器之间的连接。

我的设置如下:

import org.junit.Rule;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;

@ContextConfiguration(initializers = RuleCombination.Initializer.class)
public abstract class RuleCombination {

private static final Integer NEO4j_EXPOSED_PORT = 7687;

private static final Integer MYSERVICE_EXPOSED_PORT = xxxx;

private static Network network = Network.newNetwork();

private static GenericContainer neo4jContainer =
        new GenericContainer("preconfigured-neo4j-container")
                .withExposedPorts(NEO4j_EXPOSED_PORT)
                .withNetwork(network)
                .withNetworkAliases("neo4j")
                .withEnv("NEO4J_dbms_security_auth__enabled", "false")
                .waitingFor(Wait.forListeningPort());


static {
    neo4jContainer.start();
}

private static GenericContainer myserviceContainer =
        new GenericContainer("myservice-container")
                .withExposedPorts(MYSERVICE_EXPOSED_PORT)
                .withNetwork(network)
                .withEnv("spring.data.neo4j.uri",
                        "bolt://" + "neo4j" + ":" +
                                neo4jContainer.getMappedPort(NEO4j_EXPOSED_PORT));

static {
    myserviceContainer.start();
}


@Rule
public TestRule containerCombination = RuleChain.outerRule(myserviceContainer).around(neo4jContainer);

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        EnvironmentTestUtils.addEnvironment(
            configurableApplicationContext.getEnvironment(),   
            "feign.myservice.url: http://" + myserviceContainer.getContainerIpAddress() + ":" + myserviceContainer.getMappedPort(MYSERVICE_EXPOSED_PORT));
    }
}
}

Wenn 我使用此配置运行测试,收到错误

ServiceUnavailableException: Unable to connect to neo4j:32819

知道如何正确地将 neo4j 容器的 IP 传递给 myservice 容器吗?

最佳答案

错误是在 myserviceContainer 中使用映射的 Neo4j 端口 neo4jContainer.getMappedPort(NEO4j_EXPOSED_PORT) 而不是公开的端口本身。

在这种情况下,也不需要带有 RuleChain 的 @Rule。

关于docker - 无法从同一网络中的另一个测试容器连接到 neo4j 测试容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389344/

相关文章:

node.js - docker "Node.js version undefined detected"

java - Neo4j 驱动程序正在运行

当使用节点(*)副节点(1,2,3...)时,Java Neo4j Cypher 查询会导致 unsupportedException

testing - 我如何将一些 jvm 选项添加到 arquillian 测试

ruby-on-rails-4 - 在 ruby​​ on rails 4 中测试 div 的文本内容

docker - Kubernetes 描述 pod - 来自服务器的错误 (NotFound)

Docker swarm - 如何在 swarm 中的每个节点上复制服务

docker - 有没有办法从主机启动同级 docker 容器安装卷?

neo4j - Cypher:具有多个源节点返回重复项的个性化 PageRank

c# - 如何从集成测试将文件上传到端点