Spring Boot中增强对MongoDB的配置(连接池等)

DD的博客全面升级,阅读体验更佳(尤其是系列教程),后续不再通过这里发布新文章,而是改到 www.didispace.com 发布啦,奔走相告!点击直达~

之前转载了一篇关于《如何在Spring Boot中是配置MongoDB的连接数》的文章,相信关注我博客的朋友们肯定也看过了。这篇文章的作者主要重写了MongoDbFactory的构建来完成,整体实现的过程还是比较复杂的。本文就来具体说说如何更简单的来实现对MongoDB的更多配置定制。

spring-boot-starter-mongodb-plus

几周之前,我就在spring4all的github上创建了这个项目,主要就是想扩展一下官方spring boot starter对mongodb的支持,提供更多配置属性,比如:连接数的配置等。

先来看看如果使用这个扩展,是否要比之前那样自己定制要方便的多:

如何使用

  1. 在使用了spring-boot-starter-data-mongodb的项目中,增加以下依赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>mongodb-plus-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
  1. 在应用主类上增加@EnableMongoPlus注解,比如:
@EnableMongoPlus
@SpringBootApplication
public class Application {

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

}

可用配置参数

可配置参数如下:

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

上述配置值均为默认值

后记

如果您觉得该项目对您有用,欢迎给予Star支持:https://github.com/SpringForAll/spring-boot-starter-mongodb-plus/

同时也欢迎关注我的:

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy