如何通过SSH隧道连接MongoDB服务器与NodeJS集群(多个worker)。

问题描述 投票:0回答:1

我正在实施 cluster 中,所以将性能最大化。

问题来了,当我通过SSH连接到MongoDB的时候,第一次连接是通过 worker 通过SSH到MongoDB服务器是成功的,但当其余的 worker 试图连接到MongoDB时,会遇到错误。{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017} 我相信这个端口是在使用中的,它应该只连接一次。

下面是我的代码。

server.js

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const mongodb = require("./server/services/mongodb")
const express = require('express');
const app = express()
const https = require('https').createServer(serverOptions, app)
const http = require('http').createServer(app);


if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
        let worker = cluster.fork();
    }

} else {
    console.log(`Worker ${process.pid} started`)
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    //------------Turn on Web Server

    https.listen(webserverConfig.port, function () {
        console.log('listening on *:' + webserverConfig.port);
    })

    http.listen(80, function () {
        console.log('listening on *:80');
    });
}

mongodb.js

const tunnel = require("tunnel-ssh")
const mongo = require("mongodb")
const MongoClient = mongo.MongoClient;

var config = require("../config/config.js")
var dbConfig = new config.db(config.env)
var url = "mongodb://" + dbConfig.username + dbConfig.password + "localhost:27017/" + dbConfig.name;

var DB



var dbServer = tunnel(sshConfig, function (error, tnl) {
  if (error) {
    return console.log({ desc: 'configuration error on SSH connection :', error });
  }
  console.log("using SSH now")
  connect()
});
// Use a listener to handle errors outside the callback
dbServer.on('error', function (err) {
  console.log({ desc: 'Something bad happened on SSH connection :', err });
});


function connect() {
  MongoClient.connect(url, function (err, db) {
    if (err) {
      console.log(err)
    };
    db.on("close", () => {
      console.log("Mongodb connection closed")
      connect()
    })

    DB = db.db(config.name)
    console.log("MongoDB Succesfully connected to", dbConfig.name);
  });
}

Console Error

MongoDB Succesfully connected to _____
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}
{"desc":"Something bad happened on SSH connection :","err":{"errno":"EADDRINUSE","code":"EADDRINUSE","syscall":"bind","address":"127.0.0.1","port":27017}}

//(Appear 8 times due to 8 cores)

It working fine without cluster, please advise and thanks in advance. 它在没有集群的情况下也能正常工作,请给我建议,提前感谢。

node.js cluster-computing ssh-tunnel
1个回答
0
投票

不知为何,当我通过SSH连接MongoDB时,worker第一次连接MongoDB服务器是成功的,但当... imports 要放进去 elsecluster.master,那么它将工作。

server.js

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
        let worker = cluster.fork();
    }

} else {
    console.log(`Worker ${process.pid} started`)
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    const cluster = require('cluster');
    const numCPUs = require('os').cpus().length;
    const mongodb = require("./server/services/mongodb")
    const express = require('express');
    const app = express()
    const https = require('https').createServer(serverOptions, app)
    const http = require('http').createServer(app);
    //------------Turn on Web Server

    https.listen(webserverConfig.port, function () {
        console.log('listening on *:' + webserverConfig.port);
    })

    http.listen(80, function () {
        console.log('listening on *:80');
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.