Соединение Mongodb в моем докеризованном проекте закрывается непосредственно перед выполнением любой работы. - PullRequest
0 голосов
/ 18 февраля 2019

Я использую mongo-scala-driver для подключения к mongodb.Когда я использую sbt run, проект работает нормально.Но когда я его докеризирую, соединение закрывается слишком быстро, и я не могу выполнять какую-либо работу.

Мой проект - это простой проект, который читает записи из коллекции, изменяет их и вставляет их в другую коллекцию.Mongodb устанавливается как локальная служба, а не как служба докера.

Я пытался изменить bindIp в mongodb.conf на 0.0.0.0, но безуспешно.

Вотстандартная конфигурация.

mongo {
  credentials {
    username = ""
    password = ""
  }
  uri = "mongodb://localhost:27017"
  db_name = "mydb"
  conversion {
    old_collection: "old"
    new_collection: "new"
  }
}

А это стандартное соединение дб в scala.

private val uri: String = config.getString("mongo.uri").trim
private val dbName: String = config.getString("mongo.db_name").trim
private val username: String = config.getString("mongo.credentials.username").trim
private val password: String = config.getString("mongo.credentials.password").trim
private val domain: String = uri.substring(10)
private val mongoURI = s"mongodb://$username:$password@$domain".replace("mongodb://:@", "mongodb://")
private val mongoClient = MongoClient(mongoURI)

My entrypoint.sh

#!/bin/sh

ENTRY_VAR="${CONFIG_ENV:-"-Xmx1024M -Xms1024M -Xmn128M"}"

JMX_EXPOSE="${CONFIG_JMX:-"-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=8081 -Dcom.sun.management.jmxremote.rmi.port=8081 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"}"

java -Dconfig.resource=production.conf -Dlogger.resource=production.logback.xml -Dpidfile.path=/dev/null ${ENTRY_VAR} ${JMX_EXPOSE} -server -XX:+UseG1GC -jar /app/server.jar

Вот журнал работыsbt run project

17:37:28.799 [run-main-1] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
17:37:28.829 [run-main-1] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]
17:37:28.877 [cluster-ClusterId{value='5c6a8ae8e69acb16ed15064c', description='null'}-localhost:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:354}] to localhost:27017 // This line looks fine. It opens connection beautifully
17:37:28.878 [cluster-ClusterId{value='5c6a8ae8e69acb16ed15064c', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
17:37:28.906 [cluster-ClusterId{value='5c6a8ae8e69acb16ed15064c', description='null'}-localhost:27017] DEBUG org.mongodb.driver.protocol.command - Sending command '{"ismaster": 1}' with request id 4 to database admin on connection [connectionId{localValue:1, serverValue:354}] to server localhost:27017
17:37:28.908 [cluster-ClusterId{value='5c6a8ae8e69acb16ed15064c', description='null'}-localhost:27017] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 4 completed successfully in 27.12 ms on connection [connectionId{localValue:1, serverValue:354}] to server localhost:27017

Однако, когда я запускаю его с версией докера, журнал выглядит так:

10:46:59.755 [main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
10:46:59.779 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]
10:46:59.809 [cluster-ClusterId{value='5c6a8d23789654490d761e32', description='null'}-localhost:27017] DEBUG org.mongodb.driver.connection - Closing connection connectionId{localValue:1}  // The connection close immediately
10:46:59.813 [cluster-ClusterId{value='5c6a8d23789654490d761e32', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:117)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:128)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
    at sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:293)
    ... 1 common frames omitted
...