我正在使用Spring Boot 2.0.3和MongoDB。我需要在应用程序中连接到2个数据库。每个数据库都有自己的副本集。我没有使用 application.properties 中的默认 spring.data.mongodb.uri 属性连接到这些数据库。我使用具有以下连接URI的MongoTemplate配置bean创建了2个单独的数据源:
mongodb.db1.uri=mongodb://user1:password@18.220.X.X:27017,18.222.X.X:27017,18.223.X.X:27017/db1?authSource=db1
mongodb.db2.uri=mongodb://user2:password@18.188.X.X/db2?authSource=db2
我正在尝试通过提供Spring documentation中提到的辅助实例的公共ip来配置上述第一个URI中的副本集。但是每当我尝试使用它进行连接时,它就会超时并抛出以下异常。
当您仅在连接URI中提供主实例时,它可以很好地工作。
2018-09-07T11:26:56,654 INFO [main] c.m.d.l.SLF4JLogger: Cluster created with settings {hosts=[18.220.X.X:27017, 18.222.X.X:27017, 18.223.X.X:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-09-07T11:26:56,655 INFO [main] c.m.d.l.SLF4JLogger: Adding discovered server 18.220.X.X:27017 to client view of cluster
2018-09-07T11:26:56,680 INFO [main] c.m.d.l.SLF4JLogger: Adding discovered server 18.222.X.X:27017 to client view of cluster
2018-09-07T11:26:56,682 INFO [main] c.m.d.l.SLF4JLogger: Adding discovered server 18.223.X.X:27017 to client view of cluster
2018-09-07T11:26:57,090 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Opened connection [connectionId{localValue:2, serverValue:4383}] to 18.222.X.X:27017
2018-09-07T11:26:57,092 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.220.X.X:27017] c.m.d.l.SLF4JLogger: Opened connection [connectionId{localValue:1, serverValue:8820}] to 18.220.X.X:27017
2018-09-07T11:26:57,092 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.223.X.X:27017] c.m.d.l.SLF4JLogger: Opened connection [connectionId{localValue:3, serverValue:4366}] to 18.223.X.X:27017
2018-09-07T11:26:57,126 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Monitor thread successfully connected to server with description ServerDescription{address=18.222.X.X:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 2]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=33697993, setName='s0', canonicalAddress=172.31.13.153:27017, hosts=[172.31.1.69:27017, 172.31.0.163:27017, 172.31.13.153:27017], passives=[], arbiters=[], primary='172.31.0.163:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Fri Sep 07 11:26:55 EDT 2018, lastUpdateTimeNanos=1247772166805941}
2018-09-07T11:26:57,127 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.220.X.X:27017] c.m.d.l.SLF4JLogger: Monitor thread successfully connected to server with description ServerDescription{address=18.220.X.X:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 2]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=34897475, setName='s0', canonicalAddress=172.31.0.163:27017, hosts=[172.31.1.69:27017, 172.31.0.163:27017, 172.31.13.153:27017], passives=[], arbiters=[], primary='172.31.0.163:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000004, setVersion=1, lastWriteDate=Fri Sep 07 11:26:55 EDT 2018, lastUpdateTimeNanos=1247772168059625}
2018-09-07T11:26:57,128 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Discovered cluster type of REPLICA_SET
2018-09-07T11:26:57,128 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.223.X.X:27017] c.m.d.l.SLF4JLogger: Monitor thread successfully connected to server with description ServerDescription{address=18.223.X.X:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 2]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=35436355, setName='s0', canonicalAddress=172.31.1.69:27017, hosts=[172.31.1.69:27017, 172.31.0.163:27017, 172.31.13.153:27017], passives=[], arbiters=[], primary='172.31.0.163:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Fri Sep 07 11:26:55 EDT 2018, lastUpdateTimeNanos=1247772169059561}
2018-09-07T11:26:57,129 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Adding discovered server 172.31.1.69:27017 to client view of cluster
2018-09-07T11:26:57,130 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Adding discovered server 172.31.0.163:27017 to client view of cluster
2018-09-07T11:26:57,130 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Adding discovered server 172.31.13.153:27017 to client view of cluster
2018-09-07T11:26:57,132 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.222.X.X:27017] c.m.d.l.SLF4JLogger: Canonical address 172.31.13.153:27017 does not match server address. Removing 18.222.X.X:27017 from client view of cluster
2018-09-07T11:26:57,133 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.223.X.X:27017] c.m.d.l.SLF4JLogger: Canonical address 172.31.1.69:27017 does not match server address. Removing 18.223.X.X:27017 from client view of cluster
2018-09-07T11:26:57,134 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.220.X.X:27017] c.m.d.l.SLF4JLogger: Server 18.220.X.X:27017 is no longer a member of the replica set. Removing from client view of cluster.
2018-09-07T11:26:57,134 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-18.220.X.X:27017] c.m.d.l.SLF4JLogger: Canonical address 172.31.0.163:27017 does not match server address. Removing 18.220.X.X:27017 from client view of cluster
2018-09-07T11:26:57,171 DEBUG [main] o.r.e.q.s.MDSFileUploadServiceImpl: mds upload temp dir: /var/folders/k6/t2lhgt2j1b75zmwhhfcjyj7r0000gq/T/mds_uploads
2018-09-07T11:26:57,180 INFO [main] c.m.d.l.SLF4JLogger: Cluster created with settings {hosts=[18.188.48.47:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-09-07T11:26:57,332 INFO [main] c.m.d.l.SLF4JLogger: No server chosen by com.mongodb.Mongo$4@fe42a09 from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=172.31.1.69:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=172.31.13.153:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=172.31.0.163:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2018-09-07T11:26:57,406 INFO [cluster-ClusterId{value='5b9298c11e27010f371833e0', description='null'}-18.188.48.47:27017] c.m.d.l.SLF4JLogger: Opened connection [connectionId{localValue:7, serverValue:65931}] to 18.188.48.47:27017
2018-09-07T11:26:57,437 INFO [cluster-ClusterId{value='5b9298c11e27010f371833e0', description='null'}-18.188.48.47:27017] c.m.d.l.SLF4JLogger: Monitor thread successfully connected to server with description ServerDescription{address=18.188.48.47:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 1]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=29340617, setName='s0', canonicalAddress=172.31.22.223:27017, hosts=[172.31.12.43:27017, 172.31.43.141:27017, 172.31.22.223:27017], passives=[], arbiters=[], primary='172.31.22.223:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000006, setVersion=1, lastWriteDate=Fri Sep 07 11:26:49 EDT 2018, lastUpdateTimeNanos=1247772477666312}
2018-09-07T11:27:17,135 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-172.31.13.153:27017] c.m.d.l.SLF4JLogger: Exception in monitor thread while connecting to server 172.31.13.153:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
... 3 more
2018-09-07T11:27:17,135 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-172.31.0.163:27017] c.m.d.l.SLF4JLogger: Exception in monitor thread while connecting to server 172.31.0.163:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
... 3 more
2018-09-07T11:27:17,135 INFO [cluster-ClusterId{value='5b9298c01e27010f371833df', description='null'}-172.31.1.69:27017] c.m.d.l.SLF4JLogger: Exception in monitor thread while connecting to server 172.31.1.69:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
... 3 more
Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@fe42a09. Client view of cluster state is {type=REPLICA_SET, servers=[{address=172.31.1.69:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.13.153:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.0.163:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@fe42a09. Client view of cluster state is {type=REPLICA_SET, servers=[{address=172.31.1.69:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.13.153:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.0.163:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:87)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2592)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2459)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2244)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2227)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:770)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:365)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:197)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy104.findAll(Unknown Source)
at org.x.x.x.configuration.QMRulesConfiguration.qualityMeasures(QMRulesConfiguration.java:37)
at org.x.x.x.configuration.QMRulesConfiguration$$EnhancerBySpringCGLIB$$4410cf1a.CGLIB$qualityMeasures$2(<generated>)
at org.x.x.x.configuration.QMRulesConfiguration$$EnhancerBySpringCGLIB$$4410cf1a$$FastClassBySpringCGLIB$$f0f9b03.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
at org.x.x.x.configuration.QMRulesConfiguration$$EnhancerBySpringCGLIB$$4410cf1a.qualityMeasures(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 82 more
Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@fe42a09. Client view of cluster state is {type=REPLICA_SET, servers=[{address=172.31.1.69:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.13.153:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=172.31.0.163:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:389)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:108)
at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:887)
at com.mongodb.Mongo.createClientSession(Mongo.java:877)
at com.mongodb.Mongo$3.getClientSession(Mongo.java:866)
at com.mongodb.Mongo$3.execute(Mongo.java:823)
at com.mongodb.MongoIterableImpl.execute(MongoIterableImpl.java:130)
at com.mongodb.MongoIterableImpl.iterator(MongoIterableImpl.java:77)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2442)
... 118 more
可能是什么问题?
答案 0 :(得分:0)
您是否需要将副本集作为URI的一部分传递?我们必须在uri结束时这样做
?replicaSet=setInQuestion