小强哥博客

小强哥,小强哥博客,技术大咖

MongDB副本集(二),SpringBoot链接Mongo副本集

前两天同的一个线上程序连接mongo突然出现了如下错误,

org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[{address=127.0.0.1:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.3 ms, state=CONNECTED}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[{address=127.0.0.1:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.3 ms, state=CONNECTED}]
	at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:77)
	at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2135)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:481)
	at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1101)
	at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1034)
	at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:981)
	at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:969)
	at com.eju.ess.MongoTest.test1(MongoTest.java:43)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[{address=127.0.0.1:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.3 ms, state=CONNECTED}]
	at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:377)
	at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
	at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:411)
	at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133)
	at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60)
	at com.mongodb.Mongo.execute(Mongo.java:845)
	at com.mongodb.Mongo$2.execute(Mongo.java:828)
	at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:342)
	at com.mongodb.DBCollection.insert(DBCollection.java:337)
	at com.mongodb.DBCollection.insert(DBCollection.java:328)
	at com.mongodb.DBCollection.insert(DBCollection.java:298)
	at com.mongodb.DBCollection.insert(DBCollection.java:264)
	at com.mongodb.DBCollection.insert(DBCollection.java:186)
	at com.mongodb.DBCollection.save(DBCollection.java:407)
	at com.mongodb.DBCollection.save(DBCollection.java:382)
	at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:1106)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:479)
	... 34 more

其中有一句话说的是,

{address=127.0.0.1:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.3 ms, state=CONNECTED}

意就是127.0.0.1:27017这台server是一个SECONDARY,mongo驱动不能向SECONDARY节点写入数据,怀疑是mongo发生了primary和secondary切换。

后来登录mongo服务器发现确实是发生了primary和secondary切换。

然后回到程序发现同事链接mongo的代码如下写法,

spring.data.mongodb.uri=mongodb://127.0.0.1:27017/quezon

我擦,只链接了一个mongo,碰巧,当时上线的时候该节点恰好是primary节点,现发生了primary和secondary,因此就报错了,调整后的mongo链接字符串如下,

spring.data.mongodb.uri=mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/quezon?replcaSet=spock

其中replcaSet指定的副本集名称,重启,ok了。

参考文章:

http://www.runoob.com/mongodb/mongodb-connections.html

http://xiaoqiangge.com/aritcle/1506320282052.html