spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Mongo tries to connect automatically to the server when microservice is not fully configured #4582

Closed lukasz-cpu closed 10 months ago

lukasz-cpu commented 10 months ago

Hello,

I have output in my application:

[organisation] 
[organisation]   .   ____          _            __ _ _
[organisation]  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
[organisation] ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
[organisation]  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
[organisation]   '  |____| .__|_| |_|_| |_\__, | / / / /
[organisation]  =========|_|==============|___/=/_/_/_/
[organisation]  :: Spring Boot ::               (v3.0.13)
[organisation] 
[organisation] 2023-12-05 10:26:36 - INFO Located property source: [BootstrapPropertySource {name='bootstrapProperties-configmap.organisation.default'}]
[organisation] 2023-12-05 10:26:36 - INFO Located property source: [BootstrapPropertySource {name='bootstrapProperties-secret.organisation.default'}]
[organisation] 2023-12-05 10:26:36 - INFO The following 1 profile is active: "kubernetes"
[organisation] 2023-12-05 10:26:36 - INFO Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
[organisation] 2023-12-05 10:26:37 - INFO Finished Spring Data repository scanning in 214 ms. Found 1 MongoDB repository interfaceinterface.
[organisation] 2023-12-05 10:26:37 - INFO BeanFactory id=e000a9ed-25f1-387c-87af-39308ddee687
[organisation] 2023-12-05 10:26:37 - INFO Tomcat initialized with port(s): 8080 (http)
[organisation] 2023-12-05 10:26:37 - INFO Starting service [Tomcat]
[organisation] 2023-12-05 10:26:37 - INFO Starting Servlet engine: [Apache Tomcat/10.1.16]
[organisation] 2023-12-05 10:26:37 - INFO Initializing Spring embedded WebApplicationContext
[organisation] 2023-12-05 10:26:37 - INFO Root WebApplicationContext: initialization completed in 1288 ms
[organisation] 2023-12-05 10:26:38 - INFO MongoClient with metadata {"driver": {"name": "mongo-java-driver|sync|spring-boot", "version": "4.8.2"}, "os": {"type": "Linux", "name": "Linux", "architecture": "amd64", "version": "6.6.3-200.fc39.x86_64"}, "platform": "Java/Oracle Corporation/21+35-2513"} created with settings MongoClientSettings{readPreference=primary, writeConcern=WriteConcern{w=null, wTimeout=null ms, journal=null}, retryWrites=true, retryReads=true, readConcern=ReadConcern{level=null}, credential=MongoCredential{mechanism=null, userName='admin', source='admin', password=<hidden>, mechanismProperties=<hidden>}, streamFactoryFactory=null, commandListeners=[], codecRegistry=ProvidersCodecRegistry{codecProviders=[ValueCodecProvider{}, BsonValueCodecProvider{}, DBRefCodecProvider{}, DBObjectCodecProvider{}, DocumentCodecProvider{}, CollectionCodecProvider{}, IterableCodecProvider{}, MapCodecProvider{}, GeoJsonCodecProvider{}, GridFSFileCodecProvider{}, Jsr310CodecProvider{}, JsonObjectCodecProvider{}, BsonCodecProvider{}, EnumCodecProvider{}, com.mongodb.Jep395RecordCodecProvider@5a4ed68f]}, clusterSettings={hosts=[mongodb:27017], srvServiceName=mongodb, mode=SINGLE, requiredClusterType=UNKNOWN, requiredReplicaSetName='null', serverSelector='null', clusterListeners='[]', serverSelectionTimeout='30000 ms', localThreshold='30000 ms'}, socketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=0, receiveBufferSize=0, sendBufferSize=0}, heartbeatSocketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=10000, receiveBufferSize=0, sendBufferSize=0}, connectionPoolSettings=ConnectionPoolSettings{maxSize=100, minSize=0, maxWaitTimeMS=120000, maxConnectionLifeTimeMS=0, maxConnectionIdleTimeMS=0, maintenanceInitialDelayMS=0, maintenanceFrequencyMS=60000, connectionPoolListeners=[], maxConnecting=2}, serverSettings=ServerSettings{heartbeatFrequencyMS=10000, minHeartbeatFrequencyMS=500, serverListeners='[]', serverMonitorListeners='[]'}, sslSettings=SslSettings{enabled=false, invalidHostNameAllowed=false, context=null}, applicationName='null', compressorList=[], uuidRepresentation=JAVA_LEGACY, serverApi=null, autoEncryptionSettings=null, contextProvider=null}
[organisation] 2023-12-05 10:26:38 - INFO Exception in monitor thread while connecting to server mongodb:27017
[organisation] com.mongodb.MongoSocketOpenException: Exception opening socket
[organisation]  at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:73) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:183) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:198) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:158) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
[organisation] Caused by: java.net.ConnectException: Connection refused
[organisation]  at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
[organisation]  at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) ~[na:na]
[organisation]  at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
[organisation]  at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) ~[na:na]
[organisation]  at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
[organisation]  at java.base/java.net.Socket.connect(Socket.java:751) ~[na:na]
[organisation]  at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:82) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:68) ~[mongodb-driver-core-4.8.2.jar!/:na]
[organisation]  ... 4 common frames omitted
[organisation] 2023-12-05 10:26:38 - WARN Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
[organisation] 2023-12-05 10:26:38 - INFO Tomcat started on port(s): 8080 (http) with context path ''
[organisation] 2023-12-05 10:26:38 - INFO Started OrganisationServiceApplication in 4.323 seconds (process running for 5.08)
[organisation] 2023-12-05 10:26:48 - INFO Monitor thread successfully connected to server with description ServerDescription{address=mongodb:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=21, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=24674601}

In the above stacktrace we can see, that Mongo is trying to connect to the database when application is not fully set-up. When my application is fully set-up we can see that

INFO Monitor thread successfully connected to server with description - it means success.

I am using

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>3.2.0</version>
        </dependency>

Application is hosted on k8s with default configuration and properties are stored in k8s's secret.


kind: Secret
apiVersion: v1
metadata:
  name: organisation
data:
  spring.data.mongodb.password: YWRtaW4=
  spring.data.mongodb.username: YWRtaW4=
type: Opaque

and in the ConfigMap:

kind: ConfigMap
apiVersion: v1
metadata:
  name: organisation
data:
  application.properties: |-
    spring.data.mongodb.host: mongodb
    spring.data.mongodb.database: admin
    spring.data.mongodb.authentication-database: admin

Here is the reference branch that I created. To run it please go to the root folder and run script: build-organisation-service.sh https://github.com/lukasz-cpu/Spring-Cloud-Kubernetes-Reference-Architecture-2024/tree/debug-error

mp911de commented 10 months ago

This isn't something Spring can help with as Spring cannot wait on your infrastructure to be ready. You need to externally synchronize the services to ensure you start your application only once your MongoDB service is ready.

lukasz-cpu commented 10 months ago

Understand, thanks for your answer. This is resolving problem. Best regards.