无法连接到测试容器Neo4J实例?

如何解决无法连接到测试容器Neo4J实例?

这是我的测试课:

@Testcontainers
@ReactiveDataNeo4jTest
internal class RepositoryIT {

    @Container
    private val container = KNeo4jContainer.instance

    @Test
    fun `should answer with One`() {
        val boltUrl = container.getBoltUrl()
        GraphDatabase.driver(
                boltUrl,AuthTokens.basic("neo4j","123456"))
                .use { driver ->
                    driver.session().use { session ->
                        val res = session.run("OPTIONAL MATCH(n) RETURN 1 AS value")
                        val one = res.single().get("value").asInt()
                        assertThat(one).isEqualTo(2)
                    }
                }

    }

}

object KNeo4jContainer {

    val instance by lazy {
        startNeo4jContainer()
    }

    private fun startNeo4jContainer(): Neo4jContainer<*> =
            Neo4jContainer<Nothing>("neo4j:4.1.0").apply {
                withEnv("NEO4J_AUTH","neo4j/123456")
                withExposedPorts(7687)
                withExposedPorts(7473)
                withExposedPorts(7474)
                start()
            }
}

当我检查docker ps -a时,它似乎已经开始

  CONTAINER ID   IMAGE                               COMMAND                  CREATED              STATUS              PORTS                                                                       NAMES                                                     
 -------------- ----------------------------------- ------------------------ -------------------- ------------------- --------------------------------------------------------------------------- ---------------------------------------------------------- 
  102b11fa5c7c   neo4j:4.1.0                         "/sbin/tini -g -- /d…"   About a minute ago   Up About a minute   0.0.0.0:32791->7473/tcp,0.0.0.0:32790->7474/tcp,0.0.0.0:32789->7687/tcp   pensive_williams                                          
  92fac72190e0   testcontainersofficial/ryuk:0.3.0   "/app"                   About a minute ago   Up About a minute   0.0.0.0:32788->8080/tcp                                                     testcontainers-ryuk-ecc3bfaf-44ba-482d-ae5f-ddba9a70182f

但是我无法连接到它:

org.neo4j.driver.exceptions.ServiceUnavailableException:无法执行 连接到localhost:7687,确保数据库正在运行,并且 到它的网络连接正常。

我可以通过docker-compose up对以相同配置启动的容器进行测试:

version: "3.1"
services:
  neo4j:
    image:  neo4j:4.1.0
    ports:
      # Http
      - "7474:7474"
      # Https
      - "7473:7473"
      # Bolt
      - "7687:7687"
    environment:
      # initial password reset
      "NEO4J_AUTH": "neo4j/secret"

但是我认为那仅仅是因为在开发测试时不必启动新容器。

我在做什么错了?

修改

在启动代码中将admin passwort设置为null后,已将AuthTokens更改为AuthTokens.none()-仍然没有成功。

修改

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/>
    </parent>
    <!-- groupId,artifactId,version,name and description ommitted -->
    <properties>
        <java.version>11</java.version>
        <kotlin.version>1.3.72</kotlin.version>
        <neo4j.test.port>7687</neo4j.test.port>
        <neo4j.test.auth>neo4j/secret</neo4j.test.auth>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.kotlin</groupId>
            <artifactId>reactor-kotlin-extensions</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-coroutines-reactor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.neo4j.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency><dependency>
        <groupId>org.neo4j.springframework.data</groupId>
        <artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
        <dependency>
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rx-spring-boot-test-autoconfigure</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.neo4j.test</groupId>
                    <artifactId>neo4j-harness</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>1.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>1.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>neo4j</artifactId>
            <version>1.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.33.0</version>
                <configuration>
                    <images>
                        <image>
                            <name>neo4j:4.1.0</name>
                            <alias>neo4j</alias>
                            <run>
                                <env>
                                    <NEO4J_AUTH>${neo4j.test.auth}</NEO4J_AUTH>
                                </env>
                                <ports>
                                    <port>${neo4j.test.port}:3306</port>
                                </ports>
                                <wait>
                                    <!-- time based waiting is the only option,because TCP-based or log-based polling are not reliable.
                                   see https://github.com/fabric8io/docker-maven-plugin/issues/328
                                   Alternatively we can wait log- or tcp-based and try to connect for a while in the test code -->
                                    <time>8000</time>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
                <executions>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*IT.*</include>
                    </includes>
                    <systemPropertyVariables>
                        <neo4j.port>${neo4j.test.port}</neo4j.port>
                        <neo4j.auth>${neo4j.test.auth}</neo4j.auth>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <excludes>
                        <exclude>**/*IT.*</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

解决方案

 private fun startNeo4jContainer(): Neo4jContainer<*> {
        val container = Neo4jContainer<Nothing>("neo4j:4.1.0").apply {
            withAdminPassword(null)
            start()
        }
        return container
    }

解决方法

考虑使用Neo4jContainer#withAdminPassword来设置密码,而不是设置环境变量。

否则,它将在此处被覆盖: https://github.com/testcontainers/testcontainers-java/blob/6bdbc8ffe257fc879791eb7fddcf3b5ecccdd52d/modules/neo4j/src/main/java/org/testcontainers/containers/Neo4jContainer.java#L118

,

之所以会这样,是因为在Windows中,默认的0.0.0.0地址未转换为本地主机。

我无法复制,但是添加以下环境变量应该可以完成工作:

version: "3.1"
services:
  neo4j:
    image:  neo4j:4.1.0
    ports:
      # Http
      - "7474:7474"
      # Https
      - "7473:7473"
      # Bolt
      - "7687:7687"
    environment:
      # initial password reset
      - NEO4J_AUTH=neo4j/secret
      - NEO4J_dbms_connector_https_advertised__address=localhost:7473
      - NEO4J_dbms_connector_http_advertised__address=localhost:7474
      - NEO4J_dbms_connector_bolt_advertised__address=localhost:7687
,

这与https://www.testcontainers.org/features/networking/

有点不同

从主持人的角度来看,Testcontainers实际上将此内容公开在 随机的免费端口。这是设计使然,以避免可能发生端口冲突的情况。 在本地运行的软件中或在并行测试运行之间出现。

https://www.testcontainers.org/modules/databases/jdbc/的底部

现在,在测试代码(或合适的设置方法)中,您可以获得 连接到该数据库所需的详细信息:

  • mysql.getJdbcUrl()提供了您的代码可以连接到的JDBC URL
  • mysql.getUsername()提供了您的代码应传递给驱动程序的用户名
  • mysql.getPassword()提供了您的代码应传递给驱动程序的密码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-