我試圖配置我的Spring Boot後端連接到MongoLab上承載的MongoDB。Spring Boot - MongoDB - MongoLab配置
問題是它試圖連接到本地主機。我得到的錯誤是:
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);
}
}
application.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>
任何幫助是非常感謝!
非常感謝!我在那裏感到困惑 – spacewalk92