我有一个Spring启动服务器,我想将它连接到我的mongoDB副本集。 (集合中有3台服务器)
1.in我配置的application.properties:
spring.data.mongodb.uri=mongodb://user:password@172.1.1.55:27017/admin
但如果我尝试写入不是主要的服务器,我会收到错误。那么如何配置它,我的春季启动应用程序总是知道谁是主要的并写入主要?
我知道有一个选项可以写:
spring.data.mongodb.uri=mongodb://user:password@172.1.1.55:27017,172.1.1.2:27017,172.1.1.3:27017/admin
但它引发了一个例外:
2016-09-11 14:14:54.811 INFO 3128 --- [-31-61-35:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server ip-172-1-1-2:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_101]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_101]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_101]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_101]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_101]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_101]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_101]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_101]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.2.2.jar:na]
... 3 common frames omitted
非常感谢
答案 0 :(得分:2)
在另一个答案(How to configure spring-data-mongodb to use a replica set via properties)中,其中一个答案(来自@nwolfe)说:
中如何定义URI的方法...发现代码忽略了uri值,如果有的话 为
spring.data.mongodb.host
配置的值,spring.data.mongodb.port
,spring.data.mongodb.username
或spring.data.mongodb.password
。如果我将所有信息都放在uri中 (并删除了所有其他spring.data.mongodb。*值 属性文件),连接代码工作。
您可以设置spring.data.mongodb.uri属性来更改URL和 配置其他设置,例如副本集:
spring.data.mongodb.uri = mongodb的://用户:secret@mongo1.example.com:12345,mongo2.example.com:23456 /测试