Docker 中的 SpringBoot 未连接到 Docker 中的 Mongo [英] SpringBoot in Docker not connecting to Mongo in Docker

查看:23
本文介绍了Docker 中的 SpringBoot 未连接到 Docker 中的 Mongo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序,并使用在 brew 服务中运行的 mongo db 开发它.

I have a Spring Boot Application and developed it with a mongo db which was running in brew services.

要连接到数据库,我只需将以下内容放入 Spring Boot 中的 application.properties

To get a connection to the db I just had to put the following into application.properties in Spring Boot

spring.data.mongodb.uri=mongodb://localhost:27017/db

将应用程序属性更改为

spring.data.mongodb.uri=mongodb://mongo:27017/db

没有改变任何东西,和之前一样的错误.

didtn't change anything, same Error as before.

现在我正在尝试将 SpringBoot 应用程序和 MongoDB 放入 Docker-Containers,但无法获得任何连接.

Now I'm trying to put the SpringBoot Application and the MongoDB into Docker-Containers, but cant get any connection working.

这是我在 SpringBoot 应用程序中的 Dockerfile:

So this is my Dockerfile in the SpringBoot Application:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ADD /build/libs/dg-0.0.1-SNAPSHOT.jar dg-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]

这是我的 Docker-Compose.yml 文件:

This is my Docker-Compose.yml file:

version: '3'

services:

mongo:
 container_name: docker-mongo
 image: mongo:latest
 ports:
   - "27017:27017"
 volumes:
  - ./data/db:/data/db

spring:
 depends_on:
   - mongo
 image:
   docker-spring-http-alpine
 ports:
   - "8080:8080"
 links:
   - mongo

使用

docker-compose up

我收到以下错误:(这是实际的错误消息)

I get the following error: (this is the actual error message )

2019-07-08 23:10:19.990  INFO 1 --- [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:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar!/:na]
... 3 common frames omitted

有人知道这里有什么问题吗?在开发环境中,它就像一个魅力.

Does someone know whats the problem here? In development environment it works like a charm.

提前致谢

在 Dockerfile 中添加以下行

Adding the following line to the Dockerfile

"-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg"

像这样进入 Entrypoint 解决了连接问题

into Entrypoint like this solved the connection issue

ENTRYPOINT ["java", "-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]

我能够在 Dockerfile 的入口点中没有上面提到的行的情况下连接到数据库.我想这是如果你想通过链接"连接你的数据库

I was able to get connection to the db without the mentioned line above in the Entrypoint in the Dockerfile. I guess this is if you like to connect your db over "links"

但是现在我可以通过网络连接了,这是我的代码:

But now I was able to connect over Network, this is my code:

version: '3.6'

services:

 mongo:
 container_name: docker_mongo
 networks:
   - gateway
 ports:
   - "27017:27017"
 hostname: mongo
 image: mongo:latest
 volumes:
   - ./data/db:/data/db

 spring:
 container_name: docker-spring
 networks:
   - gateway
 ports:
   - "8080:8080"
 hostname: spring
 depends_on:
   - mongo
 image: dg-docker-spring-http-alpine-j
 networks:
gateway:
driver: "bridge"

以及应用程序属性中的以下内容

and the following in application properties

spring.data.mongodb.host=docker_mongo
spring.data.mongodb.port=27017
spring.data.mongodb.database=db

所以看起来连接现在正在通过网络工作.相同的代码不适用于 3.0 版

So it looks like the connection is working over Network now. The same code did not work with Version 3.0

为了防止 SpringBoot 通过 localhost 自动连接到 mongo,还需要排除 MongoAutoConfiguration!

To prevent SpringBoot to connect automatically to mongo over localhost it's also necessary to exclude MongoAutoConfiguration!

@SpringBootApplication(exclude={MongoAutoConfiguration.class})

谢谢大家的帮助

推荐答案

我从来没有做过 spring-boot 开发,但是你说的显示错误很可能与 mongo 问题无关.但是,这里解释了为什么您的 mongo-connection 失败:

I've never done spring-boot development, but the error you are saying is being displayed may very well be unrelated to the mongo issue. However, here is an explanation as to why your mongo-connection is failing:

docker-compose 如果文件中没有指定一个虚拟网络(比如你的情况),则会创建一个虚拟网络.

docker-compose creates a virtual network if one hasn't been specified in the file (like in your case).

您的所有应用程序都在此网络内运行,彼此完全隔离.因此,spring-boot 容器中的 localhost 实际上是指它自己.这意味着您的 spring-boot 应用程序期望 mongo 实例在其容器内运行(它不是,它在不同的容器中).

All your applications run inside of this network, completely isolated from each other. As such, localhost in your spring-boot container actually refers to itself. Meaning your spring-boot application is expecting the mongo instance to be running inside of its container (which its not, it's in a different container).

当数据库和应用程序都在您的笔记本电脑网络上运行时,这会很好.但如前所述,它们现在完全隔离地在 docker-compose 网络中运行.

This would have been fine when both the database and application was running on your laptop's network. But as mentioned, they are now running in the docker-compose network, in complete isolation.

然而,docker-compose 真的很聪明!它为您的每个容器创建一个 DNS,使用您的 docker-compose 中指定的服务名称(在您的情况下为 mongospring)文件以允许轻松访问网络内部的容器.

However, docker-compose is really clever! It creates a DNS for each of your containers which uses the service-name (in your case mongo and spring) specified in your docker-compose file to allow for easy access to the containers inside of the network.

因此,您应该能够将 spring.data.mongodb.uri=mongodb://localhost:27017/db 更改为 spring.data.mongodb.uri=mongodb://mongo:27017/db 这应该允许它连接.

So, you should be able to change spring.data.mongodb.uri=mongodb://localhost:27017/dbto spring.data.mongodb.uri=mongodb://mongo:27017/db and that should allow it to connect.

这篇关于Docker 中的 SpringBoot 未连接到 Docker 中的 Mongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆