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());
     builder.socketKeepAlive(true);
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

ConnectionTimeOut和SocketTimeOut的区别:

一次完整的请求包括三个阶段:1、建立连接 2、数据传输 3、断开连接

如果与服务器(这里指数据库)请求建立连接的时间超过ConnectionTimeOut,就会抛 ConnectionTimeOutException,即服务器连接超时,没有在规定的时间内建立连接。

如果与服务器连接成功,就开始数据传输了。

如果服务器处理数据用时过长,超过了SocketTimeOut,就会抛出SocketTimeOutExceptin,即服务器响应超时,服务器没有在规定的时间内返回给客户端数据

二、docker 操作

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

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

whereis mongo

mongo: /usr/bin/mongo

root@8725453ff785:/var/local# cd /usr/bin

进入mongo
./mongo 127.0.0.1:27017
db.createUser({user:"lorealmdm",pwd:"mdm1@qawsx",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

原文地址:https://www.cnblogs.com/lyon91/p/9525355.html