欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot中使用MongoDB的連接池配置的方法

 更新時(shí)間:2018年03月06日 09:17:15   作者:翟永超  
本文介紹了Spring Boot中使用MongoDB的連接池配置的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

因?yàn)榻裉扉_(kāi)發(fā)遇到了性能問(wèn)題,可能與MongoDB的連接有關(guān),所以稍稍深入看了一下,正好搜到原來(lái)有人寫過(guò)這篇相關(guān)的內(nèi)容,所以轉(zhuǎn)載過(guò)來(lái)?;仡^有時(shí)間可以寫個(gè)擴(kuò)展到SpringForAll里,主體思路還是一樣的。感謝這位美女程序媛的文章!

說(shuō)明

Spring Boot中通過(guò)依賴 spring-boot-starter-data-mongodb ,來(lái)實(shí)現(xiàn) spring-data-mongodb 的自動(dòng)配置。

但是默認(rèn)情況下,Spring Boot 中,并沒(méi)有像使用MySQL或者Redis一樣,提供了連接池配置的功能。因此,我們需要自行重寫 MongoDbFactory ,實(shí)現(xiàn)MongoDB客戶端連接的參數(shù)配置擴(kuò)展。

需要說(shuō)明的是,MongoDB的客戶端本身就是一個(gè)連接池,因此,我們只需要配置客戶端即可。

配置文件

為了統(tǒng)一Spring Boot的配置,我們要將重寫的配置也配置到 application.yml 中,前綴為 spring.data.mongodb.custom 下(前綴可自己隨意配置):

spring:
 data:
 mongodb:
  custom:
  hosts:
   - 10.0.5.1
   - 10.0.5.1
  ports:
   - 27017
   - 27018
  replica-set: mgset-3590061
  username: jancee
  password: abc123
  database: jancee
  authentication-database: admin
  connections-per-host: 20
  min-connections-per-host: 20

該配置例子中,配置了副本集,其中包含了主機(jī) 10.0.5.1:27017 和 10.0.5.1:27018 ,其它配置與Spring Boot的標(biāo)準(zhǔn)配置類似,另外, connections-per-host 為客戶端的連接數(shù), in-connections-per-host 為客戶端最小連接數(shù)。

將配置包裝成類

為方便調(diào)用和可讀性,將上述配置包裝成一個(gè)配置實(shí)體類, MongoConfig.java 代碼如下:

package com.feidiao.jancee.fdiot.api.config.mongo;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.List;

@Component
@Validated
public class MongoSettingsProperties {

 @NotBlank
 private String database;

 @NotEmpty
 private List<String> hosts;

 @NotEmpty
 private List<Integer> ports;
 private String replicaSet;
 private String username;
 private String password;
 private String authenticationDatabase;
 private Integer minConnectionsPerHost = 10;
 private Integer connectionsPerHost = 2;
 public MongoSettingsProperties() {

 }

 public String getDatabase() {
  return database;
 }

 public void setDatabase(String database) {
  this.database = database;
 }

 public List<String> getHosts() {
  return hosts;
 }

 public void setHosts(List<String> hosts) {
  this.hosts = hosts;
 }

 public List<Integer> getPorts() {
  return ports;
 }

 public void setPorts(List<Integer> ports) {
  this.ports = ports;
 }

 public String getReplicaSet() {
  return replicaSet;
 }

 public void setReplicaSet(String replicaSet) {
  this.replicaSet = replicaSet;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getAuthenticationDatabase() {
  return authenticationDatabase;
 }

 public void setAuthenticationDatabase(String authenticationDatabase) {
  this.authenticationDatabase = authenticationDatabase;
 }

 public Integer getMinConnectionsPerHost() {
  return minConnectionsPerHost;
 }

 public void setMinConnectionsPerHost(Integer minConnectionsPerHost) {
  this.minConnectionsPerHost = minConnectionsPerHost;
 }

 public Integer getConnectionsPerHost() {
  return connectionsPerHost;
 }

 public void setConnectionsPerHost(Integer connectionsPerHost)  {
  this.connectionsPerHost = connectionsPerHost;
 }
}

覆蓋MongoDbFactory

接下來(lái),就是覆蓋Spring Boot原有的 MongoDbFactory Bean,新建文件 MongoConfig.java ,代碼如下:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 java.util.ArrayList;
import java.util.List;

@Configuration
public class MongoConfig {
 // 注入配置實(shí)體
 @Autowired
 private MongoSettingsProperties mongoSettingsProperties;
 @Bean
 @ConfigurationProperties(
   prefix = "spring.data.mongodb.custom")
 MongoSettingsProperties mongoSettingsProperties() {
  return new MongoSettingsProperties();
 }

 // 覆蓋默認(rèn)的MongoDbFactory
 @Bean
 MongoDbFactory mongoDbFactory() {
  //客戶端配置(連接數(shù)、副本集群驗(yàn)證)
  MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
  builder.connectionsPerHost(mongoSettingsProperties.getConnectionsPerHost());
  builder.minConnectionsPerHost(mongoSettingsProperties.getMinConnectionsPerHost());
  if (mongoSettingsProperties.getReplicaSet() != null) {
   builder.requiredReplicaSetName(mongoSettingsProperties.getReplicaSet());
  }
  MongoClientOptions mongoClientOptions = builder.build();

  // MongoDB地址列表
  List<ServerAddress> serverAddresses = new ArrayList<>();
  for (String host : mongoSettingsProperties.getHosts()) {
   Integer index = mongoSettingsProperties.getHosts().indexOf(host);
   Integer port = mongoSettingsProperties.getPorts().get(index);

   ServerAddress serverAddress = new ServerAddress(host, port);
   serverAddresses.add(serverAddress);
  }
  System.out.println("serverAddresses:" + serverAddresses.toString());

  // 連接認(rèn)證
  List<MongoCredential> mongoCredentialList = new ArrayList<>();
  if (mongoSettingsProperties.getUsername() != null) {
   mongoCredentialList.add(MongoCredential.createScramSha1Credential(
     mongoSettingsProperties.getUsername(),
     mongoSettingsProperties.getAuthenticationDatabase() != null ? mongoSettingsProperties.getAuthenticationDatabase() : mongoSettingsProperties.getDatabase(),
     mongoSettingsProperties.getPassword().toCharArray()));
  }
  System.out.println("mongoCredentialList:" + mongoCredentialList.toString());
  //創(chuàng)建客戶端和Factory
  MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions);
  MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoSettingsProperties.getDatabase());
  return mongoDbFactory;
 }
}

在這里,實(shí)現(xiàn)了MongoDB連接時(shí),前面配置的參數(shù)的設(shè)置,按照自己的實(shí)際情況,可以在 new SimpleMongoDbFactory 時(shí),增加修改自己需要的配置參數(shù)。

至此,就完成了全部配置,運(yùn)行測(cè)試即可。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)數(shù)據(jù)交互功能

    Java連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)數(shù)據(jù)交互功能

    在現(xiàn)代應(yīng)用中,數(shù)據(jù)庫(kù)是不可或缺的一部分,Java 作為一種廣泛使用的編程語(yǔ)言,提供了豐富的 API 來(lái)與各種數(shù)據(jù)庫(kù)進(jìn)行交互,本文將詳細(xì)介紹如何在 Java 中連接 MySQL 數(shù)據(jù)庫(kù),并實(shí)現(xiàn)基本的數(shù)據(jù)交互功能,需要的朋友可以參考下
    2024-10-10
  • Java中StringBuilder常用構(gòu)造方法解析

    Java中StringBuilder常用構(gòu)造方法解析

    這篇文章主要介紹了Java中StringBuilder常用構(gòu)造方法解析,StringBuilder是一個(gè)可標(biāo)的字符串類,我們可以吧它看成是一個(gè)容器這里的可變指的是StringBuilder對(duì)象中的內(nèi)容是可變的,需要的朋友可以參考下
    2024-01-01
  • IntelliJ Idea2017如何修改緩存文件的路徑

    IntelliJ Idea2017如何修改緩存文件的路徑

    這篇文章主要介紹了IntelliJ Idea2017如何修改緩存文件的路徑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java中EnumMap代替序數(shù)索引代碼詳解

    Java中EnumMap代替序數(shù)索引代碼詳解

    這篇文章主要介紹了Java中EnumMap代替序數(shù)索引代碼詳解,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Java中ArrayList和LinkedList的遍歷與性能分析

    Java中ArrayList和LinkedList的遍歷與性能分析

    這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測(cè)試對(duì)比,根據(jù)ArrayList和LinkedList的源碼實(shí)現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,有需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • Springboot項(xiàng)目的Mapper中增加一個(gè)新的sql語(yǔ)句

    Springboot項(xiàng)目的Mapper中增加一個(gè)新的sql語(yǔ)句

    本文主要介紹了Springboot項(xiàng)目的Mapper中增加一個(gè)新的sql語(yǔ)句,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • SpringBoot2 參數(shù)管理實(shí)踐之入?yún)⒊鰠⑴c校驗(yàn)的方式

    SpringBoot2 參數(shù)管理實(shí)踐之入?yún)⒊鰠⑴c校驗(yàn)的方式

    這篇文章主要介紹了SpringBoot2 參數(shù)管理實(shí)踐,入?yún)⒊鰠⑴c校驗(yàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-06-06
  • springboot接收日期類型參數(shù)的操作方法

    springboot接收日期類型參數(shù)的操作方法

    如果使用Get請(qǐng)求,直接使用對(duì)象接收,則可以使用@DateTimeFormat注解進(jìn)行格式化,本文重點(diǎn)給大家介紹springboot接收日期類型參數(shù)的方法,感興趣的朋友一起看看吧
    2024-02-02
  • 關(guān)于Spring?Cache?緩存攔截器(?CacheInterceptor)

    關(guān)于Spring?Cache?緩存攔截器(?CacheInterceptor)

    這篇文章主要介紹了關(guān)于Spring?Cache緩存攔截器(?CacheInterceptor),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法

    idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法

    這篇文章主要介紹了idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論