初始化Mongo服務器。帶權限與配置

package com.ouyeelbuy.manage.biz.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author robin.zhang
 * @date $
 * @description
 */
@Configuration
public class MongoConfig {
    @Value("${mongodb.collection}")
    private String collection;
    @Value("${mongo_host}")
    private String mongoHost;
    @Value("${mongo_database}")
    private String mongoDatabase;
    @Value("${mongo_max_connections}")
    private int mongoMaxConnections;
    @Value("${mongo_perConnection_threadWait_num}")
    private int mongoPerConnectionThreadWaitNum;
    @Value("${mongo_max_wait_time}")
    private int mongoMaxWaitTime;
    @Value("${mongo_connection_timeout}")
    private int mongoConnectionTimeout;
    @Value("${mongo_userName}")
    private String username;
    @Value("${mongo_passWord}")
    private String password;
    @Bean
    public MongoClient mongoClient() {
        MongoClient mongoClient = null;
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(mongoMaxConnections);//數據庫最大連接數
        builder.threadsAllowedToBlockForConnectionMultiplier(mongoPerConnectionThreadWaitNum);//每個連接線程等待數量
        builder.maxWaitTime(mongoMaxWaitTime);//最大等待時間
        builder.connectTimeout(mongoConnectionTimeout);//連接的超時時間
        MongoClientOptions myOptions = builder.build();
        try {
            List<ServerAddress> addrs = new ArrayList<>();
            for (String hostport : mongoHost.split(", *")) {
                if (StringUtils.isBlank(hostport)) {
                    continue;
                }
                hostport = hostport.trim();
                ServerAddress serverAddress = new ServerAddress(hostport.split(":")[0],Integer.valueOf(hostport.split(":")[1]));
                addrs.add(serverAddress);
            }
            MongoCredential credential = MongoCredential.createScramSha1Credential(username, mongoDatabase, password.toCharArray());
            List<MongoCredential> credentials = new ArrayList<>();
            credentials.add(credential);

            mongoClient = new MongoClient(addrs,credentials, myOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mongoClient;
    }

    @Bean
    public MongoDatabase mongoDatabase(MongoClient mongoClient) {
        MongoDatabase mongoDatabases = mongoClient.getDatabase(mongoDatabase);
        return mongoDatabases;
    }
    @Bean
    public MongoCollection<Document> mongoCollection(MongoDatabase mongoDatabase) {
        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collection);
        return mongoCollection;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章