In my project, I need to setup replica set for two mongo db instances. I did setup the mongo db in two different PCs to be primary and secondary and tested, it works fine.
I connected this to Spring Boot and it shows the replica set is working fine, the service also giving the expected output. But when I stop the primary mongo db instance, the service connects to the secondary for a moment and then starts complaining about primary being down. The service is also giving 500 Internal server error.
Below is the mongo uri setup in properties file
app.mongo.uri=mongodb://localadmin:test123@firstIp:27017,secondIp:27017/mydb?replicaSet=rs0&readPreference=secondary
This is the MongoAppConfig class that is overridden so that I can pass the username and password dynamically.
@Configuration
@EnableMongoRepositories
public class SampleMongoConfig extends AbstractMongoConfiguration {
@Value("${app.mongo.uri}")
private String mongoUri;
@Override
@Bean
public Mongo mongo() throws Exception {
//todo: username / pwd to be appended programatically.
MongoClientURI uri = new MongoClientURI(mongoUri, MongoClientOptions.builder().readPreference(ReadPreference.secondary()));
return new MongoClient(uri);
}
@Override
protected String getDatabaseName() {
return "mydb";
}
}
This is the error I'm seeing when the primary is brought down.
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
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:57)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 common frames omitted
INFO o.m.d.cluster - Monitor thread successfully connected to server with description ServerDescription{address=secondIp:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=657772587, setName='rs0', canonicalAddress=secondIp:27017, hosts=[firstIp:27017], passives=[secondIp:27017], arbiters=[], primary='null', tagSet=TagSet{[]}, electionId=null, setVersion=3, lastWriteDate=Wed May 23 04:05:29 GMT 2018, lastUpdateTimeNanos=1833371701259}
INFO o.m.d.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=firstIp:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}, ServerDescription{address=secondIp:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=657772587, setName='rs0', canonicalAddress=10.229.2.190:27017, hosts=[firstIp:27017], passives=[secondIp:27017], arbiters=[], primary='null', tagSet=TagSet{[]}, electionId=null, setVersion=3, lastWriteDate=Wed May 23 04:05:29 GMT 2018, lastUpdateTimeNanos=1833371701259}]}. Waiting for 30000 ms before timing out