您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在springboot中對mongodb連接池進行配置,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
application.yml 配置
mongodb: address: localhost:27017 database: soms username: admin password: 123456 # 連接池配置 clientName: soms-task # 客戶端的標識,用于定位請求來源等 connectionTimeoutMs: 10000 # TCP連接超時,毫秒 readTimeoutMs: 15000 # TCP讀取超時,毫秒 poolMaxWaitTimeMs: 3000 #當連接池無可用連接時客戶端阻塞等待的時長,單位毫秒 connectionMaxIdleTimeMs: 60000 #TCP連接閑置時間,單位毫秒 connectionMaxLifeTimeMs: 120000 #TCP連接最多可以使用多久,單位毫秒 heartbeatFrequencyMs: 20000 #心跳檢測發送頻率,單位毫秒 minHeartbeatFrequencyMs: 8000 #最小的心跳檢測發送頻率,單位毫秒 heartbeatConnectionTimeoutMs: 10000 #心跳檢測TCP連接超時,單位毫秒 heartbeatReadTimeoutMs: 15000 #心跳檢測TCP連接讀取超時,單位毫秒 connectionsPerHost: 100 # 每個host的TCP連接數 minConnectionsPerHost: 5 #每個host的最小TCP連接數 #計算允許多少個線程阻塞等待可用TCP連接時的乘數,算法: threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost,當前配置允許10*20個線程阻塞 threadsAllowedToBlockForConnectionMultiplier: 10
用來獲取配置參數的類
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.util.List; @Data @Validated @Component @ConfigurationProperties(prefix = "mongodb") public class MongoClientOptionProperties { /** 基礎連接參數 */ private String database; private String username; private String password; @NotNull private List<String> address; /** 客戶端連接池參數 */ @NotNull @Size(min = 1) private String clientName; /** socket連接超時時間 */ @Min(value = 1) private int connectionTimeoutMs; /** socket讀取超時時間 */ @Min(value = 1) private int readTimeoutMs; /** 連接池獲取鏈接等待時間 */ @Min(value = 1) private int poolMaxWaitTimeMs; /** 連接閑置時間 */ @Min(value = 1) private int connectionMaxIdleTimeMs; /** 連接最多可以使用多久 */ @Min(value = 1) private int connectionMaxLifeTimeMs; /** 心跳檢測發送頻率 */ @Min(value = 2000) private int heartbeatFrequencyMs; /** 最小的心跳檢測發送頻率 */ @Min(value = 300) private int minHeartbeatFrequencyMs; /** 計算允許多少個線程阻塞等待時的乘數,算法:threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost */ @Min(value = 1) private int threadsAllowedToBlockForConnectionMultiplier; /** 心跳檢測連接超時時間 */ @Min(value = 200) private int heartbeatConnectionTimeoutMs; /** 心跳檢測讀取超時時間 */ @Min(value = 200) private int heartbeatReadTimeoutMs; /** 每個host最大連接數 */ @Min(value = 1) private int connectionsPerHost; /** 每個host的最小連接數 */ @Min(value = 1) private int minConnectionsPerHost; }
配置類
package com.alkin.soms.common.mongo; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; 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.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.convert.*; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import java.util.ArrayList; import java.util.List; @Configuration public class MongoConfig { private final Logger log = LoggerFactory.getLogger(MongoConfig.class); /** * 自定義mongo連接池 * * @param properties * @return */ @Bean @Autowired public MongoDbFactory mongoDbFactory(MongoClientOptionProperties properties) { //創建客戶端參數 MongoClientOptions options = mongoClientOptions(properties); //創建客戶端和Factory List<ServerAddress> serverAddresses = new ArrayList<>(); for (String address : properties.getAddress()) { String[] hostAndPort = address.split(":"); String host = hostAndPort[0]; int port = Integer.parseInt(hostAndPort[1]); ServerAddress serverAddress = new ServerAddress(host, port); serverAddresses.add(serverAddress); } String username = properties.getUsername(); String password = properties.getPassword(); String database = properties.getDatabase(); MongoClient mongoClient; if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) { //創建認證客戶端 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential( username, database, password.toCharArray()); mongoClient = new MongoClient(serverAddresses.get(0), mongoCredential, options); } else { //創建非認證客戶端 mongoClient = new MongoClient(serverAddresses, options); } SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, database); log.info("mongodb注入成功"); return mongoDbFactory; } @Bean(name = "mongoTemplate") @Autowired public MongoTemplate getMongoTemplate(MongoDbFactory mongoDbFactory) { return new MongoTemplate(mongoDbFactory); } /** * mongo客戶端參數配置 * * @return */ public MongoClientOptions mongoClientOptions(MongoClientOptionProperties properties) { return MongoClientOptions.builder() .connectTimeout(properties.getConnectionTimeoutMs()) .socketTimeout(properties.getReadTimeoutMs()).applicationName(properties.getClientName()) .heartbeatConnectTimeout(properties.getHeartbeatConnectionTimeoutMs()) .heartbeatSocketTimeout(properties.getHeartbeatReadTimeoutMs()) .heartbeatFrequency(properties.getHeartbeatFrequencyMs()) .minHeartbeatFrequency(properties.getMinHeartbeatFrequencyMs()) .maxConnectionIdleTime(properties.getConnectionMaxIdleTimeMs()) .maxConnectionLifeTime(properties.getConnectionMaxLifeTimeMs()) .maxWaitTime(properties.getPoolMaxWaitTimeMs()) .connectionsPerHost(properties.getConnectionsPerHost()) .threadsAllowedToBlockForConnectionMultiplier( properties.getThreadsAllowedToBlockForConnectionMultiplier()) .minConnectionsPerHost(properties.getMinConnectionsPerHost()).build(); } @Bean public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) { DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory); MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context); try { mappingConverter.setCustomConversions(beanFactory.getBean(MongoCustomConversions.class)); } catch (NoSuchBeanDefinitionException ignore) { } // Don't save _class to dao mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null)); return mappingConverter; } }
關于怎么在springboot中對mongodb連接池進行配置就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。