Enhanced mongodb configuration in spring boot (connection pool, etc.)

Time:2021-7-30

I reprinted an article aboutHow to configure the number of mongodb connections in spring bootI believe my friends who pay attention to my blog must have read it. The author of this article mainly rewritesMongoDbFactoryThe overall implementation process is still complex. This article will specifically talk about how to more simply customize the configuration of mongodb.

spring-boot-starter-mongodb-plus

A few weeks later, I created this project on GitHub of spring4all, mainly to expand the official spring boot starter’s support for mongodb and provide more configuration properties, such as the configuration of connection numbers.

First, let’s see if using this extension is much more convenient than customizing yourself as before:

How to use

1. In usespring-boot-starter-data-mongodbAdd the following dependencies to the project
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>mongodb-plus-spring-boot-starter</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>
2. Add on the application main class@EnableMongoPlusNotes, such as:
@EnableMongoPlus
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Available configuration parameters

The configurable parameters are as follows:

spring.data.mongodb.option.min-connection-per-host=0
spring.data.mongodb.option.max-connection-per-host=100
spring.data.mongodb.option.threads-allowed-to-block-for-connection-multiplier=5
spring.data.mongodb.option.server-selection-timeout=30000
spring.data.mongodb.option.max-wait-time=120000
spring.data.mongodb.option.max-connection-idle-time=0
spring.data.mongodb.option.max-connection-life-time=0
spring.data.mongodb.option.connect-timeout=10000
spring.data.mongodb.option.socket-timeout=0

spring.data.mongodb.option.socket-keep-alive=false
spring.data.mongodb.option.ssl-enabled=false
spring.data.mongodb.option.ssl-invalid-host-name-allowed=false
spring.data.mongodb.option.always-use-m-beans=false

spring.data.mongodb.option.heartbeat-socket-timeout=20000
spring.data.mongodb.option.heartbeat-connect-timeout=20000
spring.data.mongodb.option.min-heartbeat-frequency=500
spring.data.mongodb.option.heartbeat-frequency=10000
spring.data.mongodb.option.local-threshold=15

The above configuration values are the default values

Postscript

If you think this project is useful to you, you are welcome to give star support:https://github.com/SpringForA…

You are also welcome to pay attention to my:

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]