简体   繁体   English

如果Spring Boot 2中不存在mongodb服务器,Maven构建会失败吗?

[英]Maven build fails if mongodb server is not present with spring boot 2?

I am learning java and Spring boot 2 我正在学习Java和Spring Boot 2

So my problem is: 所以我的问题是:

2018-07-09 11:40:24.816  INFO 20196 --- [localhost:27017] org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.open (InternalStreamConnection.java:126) ~[mongo-java-driver-3.6.4.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run (DefaultServerMonitor.java:114) ~[mongo-java-driver-3.6.4.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171] Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
at com.mongodb.connection.SocketStreamHelper.initialize (SocketStreamHelper.java:59) ~[mongo-java-driver-3.6.4.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongo-java-driver-3.6.4.jar:na]
... 3 common frames omitted

2018-07-09 11:40:25.164  INFO 20196 --- [           main] org.mongodb.driver.cluster: Cluster description not yet available. Waiting for 30000 ms before timing out

So when Spring Boot 2 is Maven build app it stuck because of absent MongoDB server and trying to connect 3 time with timeout 30000ms, then stuck... 因此,当Spring Boot 2是Maven构建应用程序时,由于缺少MongoDB服务器而卡住了,并尝试连接3次超时30000ms,然后卡住了...

So I want to init this connection, after finishing of Maven build, from runtime with external config parameter, and then with @shedules monitoring check if MongoDB alive, and reconnect if it lost. 因此,我想在完成Maven构建之后,使用外部config参数从运行时初始化此连接,然后使用@shedules监视,检查MongoDB是否仍然存在,并重新连接(如果丢失)。

What I found for SQL is excluding autoconfiguration from topic but I dont know which file is configure MongoDB from this link . 我为SQL发现的是从主题中排除自动配置,但是我不知道从此链接配置哪个文件是MongoDB。 And may be I must configure it like a @PostConstruct @bean conf files? 可能我必须像@PostConstruct @bean conf文件一样配置它吗? (please suggest me example if it present and possibility to do this opportunity) (请给我举个例子,如果有的话,可以做这个机会)

Thank you in advance! 先感谢您!

for now I take an example of db routers: 现在,我以数据库路由器为例:

@SpringBootApplication(exclude = 
{org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.class
,org.springframework.boot.autoconfigure.data.mongo 
.MongoDataAutoConfiguration.class})
public class Application { 

exclude 2 files from autoconfigure, wrote some class for setup my properties, but I still do not know how to delay an effort of connection! 从自动配置中排除2个文件,编写了一些类来设置我的属性,但我仍然不知道如何延迟连接的工作!

since you are using spring boot it should take care of the required versions or did you added the entries for the driver in the pom yourself. 由于您使用的是Spring Boot,因此应该注意所需的版本,或者您自己在pom中添加了驱动程序的条目。

  1. try maven clean ,and force update of snapshots. 尝试使用maven clean,并强制更新快照。
  2. mongo-java-driver-3.6.4.jar either it is not compatible or not picked by the ide you are using. mongo-java-driver-3.6.4.jar不兼容或未被您使用的ide选择。 check the table mongo-db-compatibilty 检查表mongo-db-compatibility

Skip the junit test while building the project. 在构建项目时跳过junit测试。

Use -Dmaven.test.skip=true to skip the tests 使用-Dmaven.test.skip=true跳过测试

or 要么

in eclipse->Project->Run As->run configuration->Maven build-> check Skip Tests 在eclipse->项目->运行方式->运行配置-> Maven构建->检查跳过测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM