Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

How can I handle multiple server MongoDB with Morphia

I am using Morphia. And now I have two mongoDB server: 'mongodb://user:[email protected]:3800,server2.com:3717/testDb?replicaSet=mgset-502725001'

How can I connect both server by Morphia?

I can only connet one server right now:

@Bean(name = "mongoClient")
    public MongoClient mongoClient() {
        List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
        MongoCredential credentia = MongoCredential.createCredential(
                mongoDbUserName, mongoDbDatabase, mongoDbPassword.toCharArray());
        credentialsList.add(credentia);
        MongoClientOptions clientOptions = MongoClientOptions.builder()
                .maxConnectionIdleTime(6000 * 5)
                .maxConnectionLifeTime(0)
                .build();

        // only one server here
        ServerAddress addr = new ServerAddress("server1.com", 3800);
        return new MongoClient(addr, credentialsList, clientOptions);
    }
question from:https://stackoverflow.com/questions/65933204/how-can-i-handle-multiple-server-mongodb-with-morphia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems I can handle like this:

        MongoClientURI mongoClientURI = new MongoClientURI("mongodb://" + mongoDbUserName + ":" +
                mongoDbPassword + "@" + mongoDbHost + ":" + mongoDbPort + "/" + mongoDbDatabase);
        return new MongoClient(mongoClientURI);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...