螺栓连接,Docker Compose + Neo4j + Node

时间:2017-04-27 20:43:55

标签: node.js docker neo4j

我是Docker Compose的新手,我正在尝试获取两个图像(一个是Node应用程序,另一个是Neo4j db),可以在本地协同工作。我的操作系统是Ubuntu。虽然每个图像分别工作正常,但我无法将Node应用程序图像连接到数据库图像。

我几乎肯定与端口连接有关;确切地说,使用我在驱动程序连接中使用的URL参数:


    var driver = neo4j.driver("bolt://neo4j:7687", neo4j.auth.basic("tester", "tester"));

其中“7687”是Docker-compose.yml中的Neo4j的Bolt端口,“neo4j”是我分配给Neo4j图像的名称(见下文)。

我正在关注(使用不同的JS驱动程序)我能找到的唯一Node + Neo4j + Docker教程(https://medium.com/@slavahatnuke/neo4j-node-js-docker-docker-compose-fdc1cc9cf405),用户注释似乎确认错误在我的连接URL中。< / p>

尝试总结:

  1. Node应用程序正在使用官方的neo4j-javascript-driver通过Bolt连接到Neo4j数据库。确切的代码是:
  2. 
        "use strict";
    
        var express = require('express');
        var neo4j = require('neo4j-driver').v1;
        const PORT = 8080;
    
        var app = express();
    
        //below is a test of the Neo4j connection, with which I have trouble connecting from in a Docker container
    
        router.get('/test', function (req, res) {
            var driver = neo4j.driver("bolt://neo4j:7687", neo4j.auth.basic("tester", "tester"));
    
            var session = driver.session();
            console.log(session);
            var countPromise = session.readTransaction(function(transaction) {
            var result = transaction.run("MATCH (n) RETURN COUNT(n) AS c")
                .then(function(result) {
                    console.log(result);
                    return result.records[0].get("c");
                });
                return result;
            });
            countPromise.then(function(result) {
                res.json(result);
                session.close();
                driver.close();
            });
    
        });
    
    
        app.listen(PORT);
        console.log("Running on " + PORT);
    
        module.exports = app;
    
    
    1. Neo4j图像是Docker的官方Neo4j图像。

    2. 我使用的是以下docker-compose.yml文件:

    3. nodeapp:
        image: node:latest
        volumes:
         - ./nodeapp:/nodeapp
        links:
         - neo4j
        ports:
         - "18080:8080"
        working_dir: /nodeapp
        entrypoint: node index.js
      
      neo4j:
        image: neo4j
        ports:
         - "17474:7474"
         - "17687:7687"
        volumes:
        - ./db/dbms:/data/dbms
      
      1. 当我运行它时(使用“docker-compose up”),我可以:

        一个。通过将浏览器指向:“localhost:17474”,可以正常访问Neo4j浏览器控制台。我使用提供的用户凭据登录,可以正常运行Cypher查询。

        B中。如果我不尝试创建Neo4j驱动程序,我可以通过将浏览器指向“localhost:18080”来运行Node中的任何常规GET函数。

        ℃。输出显示以下内容:“在0.0.0.0:7687上启用螺栓。”

      2. 所以我一直专注于Node中的连接URL。

        但它还没有连接。具体来说,根据日志,程序只是继续尝试并且无法创建驱动程序(它不断重复“这是一个Neo4j连接测试”这一行。)

        我确信Docker Compose有一些简单的东西,我错过了。

        没有Docker,我使用URL“bolt:// localhost:7687”将应用程序成功连接到数据库。现在它在Docker中,我不知道应该使用什么。

        感谢您提供任何指导。

        更新

        我担心这些建议都没有奏效。

        以下是一些更多信息:

        1. 当我运行“docker-compose ps”时,我得到了这个,所以看起来端口映射正确。
        2.       Name             Command             State              Ports       
          -------------------------------------------------------------------------
          initialnode_neo4   /docker-           Up                 7473/tcp, 0.0.0. 
          j_1                entrypoint.sh                         0:17474->7474/tc 
                             neo4j                                 p, 0.0.0.0:17687 
                                                                   ->7687/tcp       
          initialnode_node   node index.js      Up                 0.0.0.0:18080->8 
          app_1                                                    080/tcp  
          
          1. 我在跑步时检查过Neo4j图像,以下是一些相关结果:
          2. 
                 "NetworkMode": "default",
                            "PortBindings": {
                                "7474/tcp": [
                                    {
                                        "HostIp": "",
                                        "HostPort": "17474"
                                    }
                                ],
                                "7687/tcp": [
                                    {
                                        "HostIp": "",
                                        "HostPort": "17687"
                                    }
                                ]
                            },
            
                ...
                "ExposedPorts": {
                                "7473/tcp": {},
                                "7474/tcp": {},
                                "7687/tcp": {}
                            },
                ...
                "NetworkSettings": {
                            "Bridge": "",
                            "SandboxID": "5eb2b7805cc802391c08be8c85cdbb19fd42de6cc794e76e57f5214c6763c140",
                            "HairpinMode": false,
                            "LinkLocalIPv6Address": "",
                            "LinkLocalIPv6PrefixLen": 0,
                            "Ports": {
                                "7473/tcp": null,
                                "7474/tcp": [
                                    {
                                        "HostIp": "0.0.0.0",
                                        "HostPort": "17474"
                                    }
                                ],
                                "7687/tcp": [
                                    {
                                        "HostIp": "0.0.0.0",
                                        "HostPort": "17687"
                                    }
                                ]
                            },
            
                ...
            
                "Gateway": "172.17.0.1",
                            "GlobalIPv6Address": "",
                            "GlobalIPv6PrefixLen": 0,
                            "IPAddress": "172.17.0.2",
                            "IPPrefixLen": 16,
                            "IPv6Gateway": "",
                            "MacAddress": "02:42:ac:11:00:02",
                            "Networks": {
                                "bridge": {
                                    "IPAMConfig": null,
                                    "Links": null,
                                    "Aliases": null,
                                    "NetworkID": "8ffa640b707301bb9b8b6c2a1cfb6f5deac8c1a5f5de9e9de0976697b01027ef",
                                    "EndpointID": "941911b65cf0b714fa13ac45ca967478a513fe5c282283aa0bfe20afcc0df06f",
                                    "Gateway": "172.17.0.1",
                                    "IPAddress": "172.17.0.2",
                                    "IPPrefixLen": 16,
                                    "IPv6Gateway": "",
                                    "GlobalIPv6Address": "",
                                    "GlobalIPv6PrefixLen": 0,
                                    "MacAddress": "02:42:ac:11:00:02"
                                }
                            }
                        }
                ...
            
            
            1. 我检查了日志中的Neo4j问题,我一直都是这样:
            2. 
                  {"log":"  _writeConnectionHolder: \n","stream":"stdout","time":"2017-05-05T16:20:59.37931736Z"}
                  {"log":"   ConnectionHolder {\n","stream":"stdout","time":"2017-05-05T16:20:59.379324034Z"}
                  {"log":"     _mode: 'WRITE',\n","stream":"stdout","time":"2017-05-05T16:20:59.379330305Z"}
                  {"log":"     _connectionProvider: \n","stream":"stdout","time":"2017-05-05T16:20:59.379353139Z"}
                  {"log":"      DirectConnectionProvider {\n","stream":"stdout","time":"2017-05-05T16:20:59.379363823Z"}
                  {"log":"        _address: 'neo4j:7687',\n","stream":"stdout","time":"2017-05-05T16:20:59.3793706Z"}
                  {"log":"        _connectionPool: [Object],\n","stream":"stdout","time":"2017-05-05T16:20:59.379377162Z"}
                  {"log":"        _driverOnErrorCallback: [Function: bound _driverOnErrorCallback] },\n","stream":"stdout","time":"2017-05-05T16:20:59.379384507Z"}
                  {"log":"     _referenceCount: 0,\n","stream":"stdout","time":"2017-05-05T16:20:59.379391793Z"}
                  {"log":"     _connectionPromise: Promise { null } },\n","stream":"stdout","time":"2017-05-05T16:20:59.379398668Z"}
                  {"log":"  _open: true,\n","stream":"stdout","time":"2017-05-05T16:20:59.379405771Z"}
                  {"log":"  _hasTx: false,\n","stream":"stdout","time":"2017-05-05T16:20:59.379412739Z"}
                  {"log":"  _lastBookmark: undefined,\n","stream":"stdout","time":"2017-05-05T16:20:59.379437046Z"}
                  {"log":"  _transactionExecutor: \n","stream":"stdout","time":"2017-05-05T16:20:59.379445641Z"}
                  {"log":"   TransactionExecutor {\n","stream":"stdout","time":"2017-05-05T16:20:59.379452547Z"}
                  {"log":"     _maxRetryTimeMs: 30000,\n","stream":"stdout","time":"2017-05-05T16:20:59.379459251Z"}
                  {"log":"     _initialRetryDelayMs: 1000,\n","stream":"stdout","time":"2017-05-05T16:20:59.379466191Z"}
                  {"log":"     _multiplier: 2,\n","stream":"stdout","time":"2017-05-05T16:20:59.379472573Z"}
                  {"log":"     _jitterFactor: 0.2,\n","stream":"stdout","time":"2017-05-05T16:20:59.379478888Z"}
                  {"log":"     _inFlightTimeoutIds: [] } }\n","stream":"stdout","time":"2017-05-05T16:20:59.379485201Z"}
                  {"log":"(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Connection was closed by server\n","stream":"stderr","time":"2017-05-05T16:21:32.301674097Z"}
              
              

              我有一个能够连接的同事,但他不得不使用粗略的第三方库,他无法通过Bolt连接,只有7474。

2 个答案:

答案 0 :(得分:0)

尝试:var driver = neo4j.driver("bolt://neo4j:7687", neo4j.auth.basic("neo4j", "neo4j"));

端口应为7687,登录凭据为默认值,除非您在启动容器后手动更改了这些凭据。

还有一件事,neo4j需要几秒钟才能启动,所以如果你试图立即连接,很可能连接失败。

答案 1 :(得分:0)

问题似乎是由于我的个人计算机设置,这解释了为什么没有其他人遇到此问题。

我有一个本地版本的Neo4j正在运行,除了 Docker Neo4j图像外,他们都使用相同的端口。

这导致连接被拒绝。

我的解决方案是:

  1. 停止使用本地版本的Neo4j(启动计算机时会自动启动)
  2. 然后使用几乎任何建议的URL运行docker-compose(我只使用&#34; bolt:neo4j:因为Docker镜像自动将Bolt映射到7687)。