gpt4 book ai didi

java - Spring Boot - MongoDB - MongoLab 配置

转载 作者:行者123 更新时间:2023-11-29 04:29:16 26 4
gpt4 key购买 nike

我正在尝试配置我的 Spring Boot 后端以连接到托管在 MongoLab 上的 MongoDB。

问题是它试图连接到本地主机。我得到的错误是:

2017-06-12 17:34:05.046  INFO 11843 --- [127.0.0.1:27017]
org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_25]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_25]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_25]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na]
... 3 common frames omitted

我尝试排除 MongoAutoConfiguration.class,但我得到了同样的错误。

我的代码:

FoodappBackendApplication.java:

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
//@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class FoodappBackendApplication {

public static void main(String[] args) {
SpringApplication.run(FoodappBackendApplication.class, args);
}
}

应用程序.properties:

server.port=8181
spring.data.mongodb.host=mongoserver
spring.data.mongodb.database=foodapp
spring.data.mongodb.uri=mongodb://<myusername>:<mypassword>@ds123456.mlab.com:12345/foodapp
spring.data.mongodb.authentication-database: admin

SpringMongoConfig.java:

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

@Override
public String getDatabaseName() {
return "foodapp";
}

@Override
@Bean
public Mongo mongo() throws Exception {
return new MongoClient();
}
}

pom.xml 依赖项:

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.4.RELEASE</version>
</dependency>
</dependencies>

非常感谢任何帮助!

最佳答案

SpringMongoConfig 中删除 @Bean mongo() 并为此删除所有类,因为您已经在 应用程序中提供了所有必要的配置.properties 文件。

将您的 FoodappBackendApplication 更新为

@SpringBootApplication
public class FoodappBackendApplication {

public static void main(String[] args) {
SpringApplication.run(FoodappBackendApplication.class, args);
}
}

对于基于 Java 的配置,您可以查看此内容。

Spring Boot does not read MongoDB configuration from Java class

关于java - Spring Boot - MongoDB - MongoLab 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44503593/

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