如何在不依赖 MongoDB 的情况下启动 spring-boot 应用程序?

Posted

技术标签:

【中文标题】如何在不依赖 MongoDB 的情况下启动 spring-boot 应用程序?【英文标题】:How to start spring-boot app without depending on MongoDB? 【发布时间】:2017-04-29 20:51:18 【问题描述】:

问题How to start spring-boot app without depending on Database?解决了mysql宕机时如何启动spring-boot应用。

mongoDB 也可以这样吗?

在Spring boot application properties 中找不到任何支持它的属性。

春季启动配置

spring.data.mongodb.uri: mongodb://localhost:27017/measurements-db

我得到以下堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'diseaseRepository': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector. Client view of cluster state is type=Unknown, servers=[address=docker.me:27017, type=Unknown, state=Connecting, exception=com.mongodb.MongoException$Network: Exception opening the socket, caused by java.net.ConnectException: Connection refused]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector. Client view of cluster state is type=Unknown, servers=[address=docker.me:27017, type=Unknown, state=Connecting, exception=com.mongodb.MongoException$Network: Exception opening the socket, caused by java.net.ConnectException: Connection refused]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 24 common frames omitted
Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector. Client view of cluster state is type=Unknown, servers=[address=docker.me:27017, type=Unknown, state=Connecting, exception=com.mongodb.MongoException$Network: Exception opening the socket, caused by java.net.ConnectException: Connection refused]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector. Client view of cluster state is type=Unknown, servers=[address=docker.me:27017, type=Unknown, state=Connecting, exception=com.mongodb.MongoException$Network: Exception opening the socket, caused by java.net.ConnectException: Connection refused]
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:75) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:162) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:51) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:326) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:179) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:139) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:66) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:118) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:92) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 34 common frames omitted
Caused by: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector. Client view of cluster state is type=Unknown, servers=[address=docker.me:27017, type=Unknown, state=Connecting, exception=com.mongodb.MongoException$Network: Exception opening the socket, caused by java.net.ConnectException: Connection refused]
at com.mongodb.BaseCluster.getServer(BaseCluster.java:82) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:664) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBTCPConnector.access$500(DBTCPConnector.java:40) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:513) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:456) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:415) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:378) ~[mongo-java-driver-2.13.3.jar:na]
at com.mongodb.DBCollection.createIndex(DBCollection.java:597) ~[mongo-java-driver-2.13.3.jar:na]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:142) ~[spring-data-mongodb-1.8.4.RELEASE.jar:na]
... 54 common frames omitted

【问题讨论】:

您在尝试使用 MongoDB 时是否遇到完全相同的异常? 我没有得到与 mysql 相同的异常。使用堆栈跟踪更新问题。 根据@jsorah 的回答,我发现这很有用question 可以解决问题 根据@jsorah 的回答,我发现这个question 非常有用 【参考方案1】:

这是 3 年后的事......但现在这是可能的。

问题是启用了自动索引创建。使用 Spring Boot 2.2,现在可以在启动时禁用任何必要的索引创建并将其推迟到 ApplicationReadyEvent 侦听器。

你需要更改的具体属性是spring.data.mongodb.auto-index-creation并设置 设置为 false(这将是未来版本的默认值)。

以下是可以(并且应该!)取代它的 ApplicationReadyEvent 侦听器的示例。

@EventListener(ApplicationReadyEvent.class)
public void initIndicesAfterStartup() 
    IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
    IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
    resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);

【讨论】:

以上是关于如何在不依赖 MongoDB 的情况下启动 spring-boot 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不出现以下错误的情况下启动纱线?

在不重新启动节点应用程序的情况下更新节点应用程序依赖项

在不重启linux的情况下用systemd启动nacos

如何在不创建 Mongoose 模型的情况下将 GraphQL 与 Mongoose 和 MongoDB 一起使用

如何在不指定字段名称的情况下使用 mongodb 在 nodejs 中进行部分数据搜索? [复制]

如何在不使用GridFSTemplate的情况下在spring boot应用程序中上传和检索mongodb中的文件?