8月23 配置mongodb连接池 | docker 操作

Posted 七彩的人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8月23 配置mongodb连接池 | docker 操作相关的知识,希望对你有一定的参考价值。

 

一、配置mongodb连接池

属性类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import lombok.Getter;
import lombok.Setter;

@Component
@PropertySource(value = "classpath:application-dev.properties")
@ConfigurationProperties(prefix = "mongo")
@Getter
@Setter
public class MongoSettingsProperties {
    
    private String host;
    private int port;
    private String database;
    private Integer minConnectionsPerHost = 0;
    private Integer maxConnectionsPerHost = 100;
    private Integer maxWaitTime = 120000;
    private Integer connectTimeout = 10000;
    private String username;
    private String password;
    private String authenticationDatabase;
}

 

配置文件

mongo.host=42.159.xx.xx
mongo.port=27018
mongo.database=loreal-mdm
mongo.username=
mongo.password=
mongo.options.min-connections-per-host=20
mongo.options.max-connections-per-host=20
mongo.options.max-wait-time=120000
mongo.options.connect-timeout=10000
mongo.authentication-database: admin

 

连接池 bean配置

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

import cn.com.connext.commenservice.bean.MongoSettingsProperties;

@Configuration
public class MongoConfig {

    
    //覆盖默认的MongoDbFacotry
    @Bean
    @Autowired
    public MongoDbFactory mongoDbFactory(MongoSettingsProperties properties) {
        //客户端配置(连接数,副本集群验证)
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(properties.getMaxConnectionsPerHost());
        builder.minConnectionsPerHost(properties.getMinConnectionsPerHost());
        builder.maxWaitTime(properties.getMaxWaitTime());
        builder.connectTimeout(properties.getConnectTimeout());
        MongoClientOptions mongoClientOptions = builder.build();
        List<ServerAddress> serverAddresses = new ArrayList<>();
        // MongoDB地址列表
        ServerAddress serverAddress = new ServerAddress(properties.getHost(), properties.getPort());
        serverAddresses.add(serverAddress);
        // 连接认证
        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(properties.getUsername(),
                properties.getAuthenticationDatabase() != null ? properties.getAuthenticationDatabase() : properties.getDatabase(),
                properties.getPassword().toCharArray());
        mongoCredentialList.add(mongoCredential);
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses,mongoClientOptions);
        //new MongoClient(serverAddresses, mongoCredential, mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, properties.getDatabase());
        return mongoDbFactory;
    }

}

 

 

参考:

https://blog.csdn.net/daibang2182/article/details/80585004

http://www.xiaoqiangge.com/aritcle/1522033666922.html

 

二、docker 操作

找到mongodb 的配置文件,创建用户名密码

进入docker
docker exec -it <容器名>bash

whereis mongo

mongo: /usr/bin/mongo

[email protected]:/var/local# cd /usr/bin

进入mongo
./mongo 127.0.0.1:27017
db.createUser({user:"lorealmdm",pwd:"[email protected]",roles:[{role:"readWriteAnyDatabase",db:"admin"}]})

退出
docker restart 容器ID或容器名 :不管容器是否启动,直接重启容器 

ctrl+d 退出docker交互式

docker 中不能用vim编辑文件

更新来源

apt-get update

安装vim

apt-get install -y vim

参考:

https://blog.csdn.net/xinxu_wang/article/details/52441807

https://blog.csdn.net/fofabu2/article/details/78983741

关于角色:

https://blog.csdn.net/jianlong727/article/details/53889990

 

以上是关于8月23 配置mongodb连接池 | docker 操作的主要内容,如果未能解决你的问题,请参考以下文章

node.js如何配置mongodb连接池?

为 JBoss 中的连接池配置 MongoDB 数据源

java操作mongodb(连接池)(转)

SpringBoot 整合 MongoDB 连接池配置详解

mongodb如何管理连接的?有必要实现连接池吗

c3p0数据库连接池 原创: Java之行 Java之行 5月8日 连接池概述 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程