Spring引导弹簧数据mongodb副本集配置

时间:2016-03-10 20:54:52

标签: java mongodb spring-data-mongodb

我在连接到db时遇到问题,这是最近实现的副本集。

有两种连接方式,一种是通过URI,另一种是使用服务器地址和凭证列表创建mongo客户端

他们都没有工作,

     MongoClientURI uri = new MongoClientURI(getDatabaseURI()+getDatabaseName()+"?replicaSet=test-replset&readPreference=primary&authMechanism=SCRAM-SHA1");
     MongoClient client = new MongoClient(uri);

1 个答案:

答案 0 :(得分:0)

我找到了答案

    List<MongoCredential> credentialsList = new ArrayList<MongoCredential();
    credentialsList.add(MongoCredential.createCredential("USER",
            "DB_NAME", "PASSWORD".toCharArray()));


        client = new MongoClient("LIST<ServerAddress> YOURSERVERLIST" ,
                credentialsList,
                MongoClientOptions.builder()
                        .connectTimeout(TIME_OUT)
                        .readPreference(ReadPreference.secondaryPreferred())
                        .build()
        );