Issue
I have Maven Multi-Module application(Spring Boot + MySql + MongoDB) with using docker image, but i can't get connection to MongoDB.
Thing is when local mongo instance "MongoDB Server on Windows Services" is turned on and use spring.data.mongodb.host=localhost all is working fine.
But when I turn off mongo instance and try to go with: spring.data.mongodb.host=$(MONGO_HOST) to use it with docker I'm getting error "Exception opening socket"
I start my application with execute command: mvn install and tryed with docker-compose up --build
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.1)
2021-09-25 12:19:22.635 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : Starting NoteControllerTest using Java 16.0.1 on AntonioPC with PID 8588 (started by ajago in D:\Programming\Eclipse-workspace\OC_Project10\Notes)
2021-09-25 12:19:22.642 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : No active profile set, falling back to default profiles: default
2021-09-25 12:19:23.776 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-25 12:19:23.777 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-09-25 12:19:23.824 INFO 8588 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data JPA - Could not safely identify store assignment for repository candidate interface com.openclassrooms.ocproject10.notes.repository.NoteRepository. If you want this repository to be a JPA repository, consider annotating your entities with one of these annotations: javax.persistence.Entity, javax.persistence.MappedSuperclass (preferred), or consider extending one of the following types with your repository: org.springframework.data.jpa.repository.JpaRepository.
2021-09-25 12:19:23.826 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 36 ms. Found 0 JPA repository interfaces.
2021-09-25 12:19:24.093 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-25 12:19:24.094 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-09-25 12:19:24.126 INFO 8588 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 31 ms. Found 1 MongoDB repository interfaces.
2021-09-25 12:19:24.876 INFO 8588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-09-25 12:19:25.458 INFO 8588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-09-25 12:19:25.596 INFO 8588 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-09-25 12:19:25.685 INFO 8588 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-09-25 12:19:25.882 INFO 8588 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-09-25 12:19:26.112 INFO 8588 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-09-25 12:19:27.051 INFO 8588 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-09-25 12:19:27.065 INFO 8588 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-09-25 12:19:27.486 INFO 8588 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-09-25 12:19:27.586 INFO 8588 --- [localhost:27017] 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.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:831) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:645) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar:na]
... 4 common frames omitted
2021-09-25 12:19:28.302 WARN 8588 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-09-25 12:19:28.988 INFO 8588 --- [ main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring TestDispatcherServlet ''
2021-09-25 12:19:28.988 INFO 8588 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Initializing Servlet ''
2021-09-25 12:19:28.990 INFO 8588 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 2 ms
2021-09-25 12:19:29.018 INFO 8588 --- [ main] c.o.o.n.controller.NoteControllerTest : Started NoteControllerTest in 7.33 seconds (JVM running for 9.77)
2021-09-25 12:19:29.380 INFO 8588 --- [ main] org.mongodb.driver.cluster : Cluster description not yet available. Waiting for 30000 ms before timing out
docker-compose.yml
version: '2'
services:
reports:
image: 'reports:latest'
build:
context: ./reports
container_name: reports
ports:
- 8080:8080
environment:
- PATIENTS_URL=http://gps:8081
- NOTES_URL=http://rewards:8082
patients:
image: 'patients:latest'
build:
context: ./patients
container_name: patients
ports:
- 8081:8081
environment:
- MYSQL_HOST=db-mysql
depends_on:
- db-mysql
notes:
image: 'notes:latest'
build:
context: ./notes
container_name: notes
ports:
- 8082:8082
environment:
- MONGO_HOST=mongodb
depends_on:
- mongodb
db-mysql:
image: mysql:latest
container_name: mysql
ports:
- 3306:3306
command: --innodb --init-file /data/application/init.sql
volumes:
- ./init.sql:/data/application/init.sql
environment:
MYSQL_DATABASE: project_10
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: Musapa1990..
mongodb:
image: mongo:latest
container_name: mongodb
network_mode: host
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_DATABASE: project_10
application.properties
# App config
logging.level.org.springframework=INFO
spring.application.name=P10_notes
server.port=8082
################### MongoDB Configuration ##########################
spring.data.mongodb.database=project_10
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=$(MONGO_HOST)
spring.data.mongodb.authentication-database=admin
# thymeleaf configurations for Auto reload Thymeleaf templates without restart
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=file:src/main/resources/templates/
I read similar articles here and tried different things, but nothing helped me.
Solution
In the end I messed up something when installing mongodb. When I reinstalled the mongo it worked. I did not make any crucial changes in the code. Only what I added was:
################### MongoDB Configuration ##########################
spring.data.mongodb.database=project_10
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=${MONGO_HOST}
spring.data.mongodb.authentication-database=admin
I added that on all 3 module on application.properties because each of modules are using MongoDB.
Answered By - Antonio Jagodić