gpt4 book ai didi

java - 在 Docker 中连接 Spring Boot 和 MongoDB

转载 作者:行者123 更新时间:2023-12-01 06:01:12 26 4
gpt4 key购买 nike

我正在尝试在 docker-compose.yml 中连接 MongoDb 和 Spring Boot。

就是这样 -

version: "3.7"

services:
app-server:
container_name: spring
build: .
ports:
- "8080:8080"
restart: always
depends_on:
- db

db:
container_name: mongo
image: mongo
ports:
- "27017:27017"
restart: always
volumes:
- ./datadir:/var/lib/mongo

我的 Spring Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

用于构建 JAR_FILE 的 POM.xml 插件 -

            <plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>maximko/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>

application.properties文件

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

My PROBLEM

当我运行“maven package”来构建我的 jar 时,出现以下错误。我的 JAR 文件未创建

    com.mongodb.MongoSocketException: db
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:na]
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]
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_181]
Caused by: java.net.UnknownHostException: db
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_181]
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_181]
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_181]
at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_181]
at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_181]
at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_181]
at java.net.InetAddress.getByName(InetAddress.java:1076) ~[na:1.8.0_181]
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:na]
... 5 common frames omitted

当我运行 docker-compose.yml 时,出现以下错误 -

    spring        | com.mongodb.MongoSocketOpenException: Exception opening socket
spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
spring | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
spring | Caused by: java.net.ConnectException: Connection refused (Connection refused)
spring | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
spring | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
spring | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
spring | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
spring | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
spring | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]

MY QUESTIONS

1)所有这些问题都是来自“application.properties”文件,还是我在 docker-compose 文件中错误配置了某些内容?

2)我可以以某种方式更改 mongoDB 的主机名吗?

3) 在运行“docker-compose.yml”之前,我是否需要运行“mvn package”来构建我的 App.jar 文件?

感谢您的帮助!

最佳答案

终于...我解决了我的问题。这是我的更改:

1)我从pom.xml中删除了这个插件

        <plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>maximko/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>

并在 Dockerfile 中替换此代码。在 ENTRYPOINT 中,我将 mongodb 连接 uri 添加到我的数据库服务(这是主要更改!)

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/spring-mongo-demo-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c "touch /app.jar"
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db:27017/HotelDB","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

为了构建我的 app.jar,我使用了 ma​​vem 包,但如果未生成您的 jar 文件,您还应该使用 ma​​vem install

还在 application.properties 文件中,我指示 spring.data.mongodb.host=db 并捕获以下错误

Caused by: java.net.UnknownHostException: db

为了在构建 JAR 文件时避免出现此异常,我编写了 spring.data.mongodb.host=localhost

关于java - 在 Docker 中连接 Spring Boot 和 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com