user1628688
user1628688

Reputation: 85

How to set replica set in spring boot?

I Have a spring boot server and I want to connect it to my mongoDB replica set. (3 servers in the set)

1.in application.properties I configured:

spring.data.mongodb.uri=mongodb://user:[email protected]:27017/admin

but if I try to write to server that is not the primary I get error. so how can I configure it that my spring boot app always know who is the primary and write to the primary?

I know there is an option to write :

spring.data.mongodb.uri=mongodb://user:[email protected]:27017,172.1.1.2:27017,172.1.1.3:27017/admin

but it throws me an exception:

        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
  1. I always connect to the admin DB. but I need access to another db because my collection is there. how can I configure that the user will connect to all db? because this user will work with different dbs and different collections?

Thanks a lot

Upvotes: 4

Views: 11725

Answers (1)

alexbt
alexbt

Reputation: 17055

In this other answer (How to configure spring-data-mongodb to use a replica set via properties) , one of the answer (from @nwolfe) says this:

... found that the code was ignoring the uri value if there were any values configured for spring.data.mongodb.host, spring.data.mongodb.port, spring.data.mongodb.username or spring.data.mongodb.password. If I put all that information in the uri (and removed all the other spring.data.mongodb.* values from the property file), the connection code worked.

Here's how you can define the URIs, from the spring documentation:

You can set spring.data.mongodb.uri property to change the URL and configure additional settings such as the replica set:

spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo2.example.com:23456/test

Upvotes: 3

Related Questions