从Spring启动应用程序

时间:2018-05-09 16:31:29

标签: mongodb spring-boot replicaset

我们有一个mongodb副本集,有三个节点(说A,B,C) 当A和B关闭时,mongo使节点C成为只读(这很棒!)

我们的要求是,当只读取节点是可用的(无主要)时,应用程序仍应继续以只读模式工作。

如果我使用java MongoClient库与mongo客户端连接打开 ?readPreference = secondaryPreferred ,客户端程序能够连接到mongo并从只读辅助节点读取数据(不按预期写入)。

但是如果我在Spring启动应用程序中尝试相同的操作,那么当应用程序启动Mongo Client无法连接到可用的只读辅助服务器时,tt会抛出错误。我是否可以在Spring启动应用程序中传递额外的参数,让应用程序知道它需要连接到只读辅助?

----- ---- application.properties

spring.data.mongodb.uri = mongodb的://10.9.9.71:27017,10.9.9.71:27018,10.9.9.71:27019 / readPreference = secondaryPreferred

任何帮助都将受到高度赞赏。

调用init方法失败;嵌套异常是org.springframework.dao.DataAccessResourceFailureException:在等待与WritableServerSelector匹配的服务器的30000毫秒后超时。集群状态的客户端视图是{type = REPLICA_SET,servers = [{address = 10.9.9.71:27018,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java。 net.ConnectException:连接被拒绝(Connection refused)}},{address = 10.9.9.71:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java.net引起.ConnectException:连接被拒绝(Connection refused)}},{address = 10.9.9.71:27019,type = REPLICA_SET_SECONDARY,roundTripTime = 1.8 ms,state = CONNECTED}];嵌套异常是com.mongodb.MongoTimeoutException:在等待与WritableServerSelector匹配的服务器时30000 ms后超时。集群状态的客户端视图是{type = REPLICA_SET,servers = [{address = 10.9.9.71:27018,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java。 net.ConnectException:连接被拒绝(Connection refused)}},{address = 10.9.9.71:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java.net引起.ConnectException:连接被拒绝(Connection refused)}},{address = 10.9.9.71:27019,type = REPLICA_SET_SECONDARY,roundTripTime = 1.8 ms,state = CONNECTED}]     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)     在org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)

0 个答案:

没有答案