在我的项目中,我需要为两个mongo db实例设置副本集。我确实在两台不同的PC中设置了mongo db作为主要和次要的并进行了测试,它运行正常。
我将它连接到Spring Boot,它显示副本集工作正常,该服务也提供了预期的输出。但是当我停止主mongo数据库实例时,服务会暂时连接到辅助服务器,然后开始抱怨主服务器已关闭。该服务还提供500内部服务器错误。
以下是属性文件中的mongo uri设置
app.mongo.uri=mongodb://localadmin:test123@firstIp:27017,secondIp:27017/mydb?replicaSet=rs0&readPreference=secondary
这是被覆盖的MongoAppConfig类,因此我可以动态传递用户名和密码。
@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";
}
}
这是我在初级版本关闭时看到的错误。
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