SpringBoot项目,加入了Mongo连接代码,启动后Console出现如下问题:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongo-java-driver-3.10.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.10.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.10.2.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_191]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_191]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_191]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_191]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_191]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.10.2.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongo-java-driver-3.10.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongo-java-driver-3.10.2.jar:na]
... 3 common frames omitted
网上说是SpringBoot自动配置了支持mongodb。在启动SpringBoot时会自动实例化一个mongo实例,需要禁用自动配置 。
增加@SpringBootApplication(exclude = MongoAutoConfiguration.class)来禁用自动配置。
我这里是在SpringBoot工程启动主类上添加的配置
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
@EnableEurekaClient
public class Startupmain {
public static void main(String[] args) {
SpringApplication.run( Startupmain.class, args );
}
}