我想用副本集名称初始化MongoDB集群连接池,以便在我的主服务器出现故障并且集群中的其他计算机变为主服务器时,它应该可以工作,如下所示:
MongoClientOptions options = MongoClientOptions.builder()
.requiredReplicaSetName("ReplicaSetName").
build();
ServerAddress serverAddress1 = new ServerAddress("192.168.5.5");
ServerAddress serverAddress2 = new ServerAddress("192.168.5.6"); List<ServerAddress> seeds = new ArrayList<>();
seeds.add(serverAddress1);seeds.add(serverAddress2);
MongoClient mongoClient = new MongoClient(seeds,options);
上面的代码完美无缺,但我想在Spring context.xml中做同样的事情。
目前我的应用程序上下文如下所示:
<bean id="mongoServerAddr1" class="com.mongodb.ServerAddress">
<constructor-arg name="host" value="${MONGO_CLUSTER1_HOST1}" />
<constructor-arg name="port" value="${MONGO_CLUSTER1_PORT1}" />
</bean>
<bean id="mongoServerAddr2" class="com.mongodb.ServerAddress">
<constructor-arg name="host" value="${MONGO_CLUSTER1_HOST2}" />
<constructor-arg name="port" value="${MONGO_CLUSTER1_PORT2}" />
</bean>
<util:list id="mongoHostList" value-type="com.mongodb.ServerAddress">
<ref bean="mongoServerAddr1" />
<ref bean="mongoServerAddr2" />
</util:list>
<bean id="mongoCredentialIDCluster1" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
<constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER1}" />
<constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE1}" />
<constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS1}" />
</bean>
<bean id="mongoCredentialIDCluster2" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
<constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER2}" />
<constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE2}" />
<constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS2}" />
</bean>
<util:list id="mongoCredentialList" value-type="com.mongodb.MongoCredential">
<ref bean="mongoCredentialIDCluster1" />
<ref bean="mongoCredentialIDCluster2" />
</util:list>
<bean id="mongoClient" class="com.mongodb.MongoClient">
<constructor-arg name="seeds" ref="mongoHostList" />
<constructor-arg name="credentialsList" ref="mongoCredentialList" />
</bean>
<bean id="mongoDao" class="com.dao.MongoDaoImpl">
<constructor-arg ref="mongoClient" />
</bean>
我怎样才能做到这一点?
答案 0 :(得分:1)
我用MongoClientURI类解决了这个问题。 配置详情:
label: for( initialization ; condition ; modification ){
for( initialization ; condition ; modification ){
if(condition){
break label;
}
}
}
这是我的应用程序上下文配置:
MONGO_URI=mongodb://user:password@192.168.50.167:27017,192.168.50.169:27017/?authSource=admin&replicaSet=ReplicaSetName
答案 1 :(得分:0)
可以找到一个可能的解决方案here:
Spring Docs中的示例是:
<强> 代码 强>
<beans>
<mongo:mongo-client host="localhost" port="27017">
<mongo:client-options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500}"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo-client>
</beans>
副本集XML示例
<mongo:mongo-client id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/>
希望这有帮助。