gpt4 book ai didi

java - com.mongodb.MongoSocketOpenException : Exception opening socket 异常

转载 作者:行者123 更新时间:2023-12-02 19:44:57 29 4
gpt4 key购买 nike

你能帮我吗。我尝试在 ubuntu 服务器上部署应用程序。我使用 docker-compose up。我有这个错误。

com.mongodb.MongoSocketOpenException: Exception opening socket
spring-mongo | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
spring-mongo | Caused by: java.net.ConnectException: Connection refused (Connection refused)
spring-mongo | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
spring-mongo | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
spring-mongo | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
spring-mongo | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
spring-mongo | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
spring-mongo | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
spring-mongo | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.5.jar!/:na]
spring-mongo | ... 3 common frames omitted

文件

FROM openjdk:8-jdk-alpine
ADD target/spring-mongo.jar spring-mongo.jar
ENTRYPOINT ["java","-jar","spring-mongo.jar"]

应用程序属性

spring.data.mongodb.host=mymongodb
spring.data.mongodb.port=27017
spring.data.mongodb.database=mongo
spring.data.mongodb.repositories.enabled=true

docer-compose.yml

version: "3"
services:
mymongodb:
image: mongo:latest
container_name: "mymongodb"
ports:
- 27017:27017
spring-mongo:
image: spring-mongo:1.0
container_name: spring-mongo
ports:
- 8080:8080
links:
- mymongodb

最佳答案

在您的 docker-compose.yml 中,您仅将 spring boot 应用程序与您的 mongodb 链接起来。

在您的application.properties 中,您正在使用不是有效主机的 mongodb。

您需要为您的 spring 应用程序传递环境变量。为此,您可以在 docker-compose.yml 中执行类似的操作。

version: "3"
services:
mymongodb:
image: mongo:latest
container_name: "mymongodb"
ports:
- 27017:27017
spring-mongo:
image: spring-mongo:1.0
container_name: spring-mongo
ports:
- 8080:8080
environment:
- MONGO_HOST=mymongodb
links:
- mymongodb

并且在您的application.properties中,

spring.data.mongodb.host=${MONGO_HOST}

关于java - com.mongodb.MongoSocketOpenException : Exception opening socket 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63961721/

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