[Mongo]MongoDB连接池推荐配置

MongoDB集群用于非事务文档存储场景,连接参数请勿配置 write-concern 为 majority(会大大降低集群的可用性及性能),该参数保持默认即可

禁止直接连Primary/Secondary节点,存在单点隐患,建议连接replica set server list,其具备自动failover、识别Primay/Secondary能力


推荐连接示例

MongoClient mongoClient = new MongoClient(
 Arrays.asList(
  new ServerAddress("gr27002i-0.jxq.db.XXX.com", 27002),
  new ServerAddress("gr27002i-1.jxq.db.XXX.com", 27002),
  new ServerAddress("gr27002i-2.jxq.db.XXX.com", 27002)
 )
);

 

配置参考(一):购物车MongoDB连接配置

spring-config-mongo.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
       default-autowire="byName">
    <mongo:mongo id="offlineOneMongo" replica-set="${db.mongodb.shopcart.2.replicaset}" write-concern="SAFE">
        <mongo:options
                connections-per-host="${mongo.offline.connectionsPerHost}"
                threads-allowed-to-block-for-connection-multiplier="${mongo.offline.threadsAllowedToBlockForConnectionMultiplier}"
                connect-timeout="${mongo.offline.connectTimeout}"
                max-wait-time="${mongo.offline.maxWaitTime}"
                auto-connect-retry="${mongo.offline.autoConnectRetry}"
                socket-keep-alive="${mongo.offline.socketKeepAlive}"
                socket-timeout="${mongo.offline.socketTimeout}"
                write-timeout="${mongo.offline.riteTimeout}"
                write-fsync="${mongo.offline.writeFsync}"
                max-auto-connect-retry-time="${mongo.offline.autoConnectRetryTime}"/>
    </mongo:mongo>
    <!-- 设置使用的数据库 -->
    <mongo:db-factory id="offlineOneMongoDbFactory" dbname="${mongo.offline.one.database}"
                      mongo-ref="offlineOneMongo" username="${db.mongodb.shopcart.2.user}" password="${db.mongodb.shopcart.2.password}" />
    <!-- 配置mongodb映射类型 -->
    <bean id="offlineOneMappingMongoConverter"
          class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
        <constructor-arg name="mongoDbFactory" ref="offlineOneMongoDbFactory" />
        <constructor-arg name="mappingContext" ref="mappingContext" />
        <property name="typeMapper" ref="defaultMongoTypeMapper" />
    </bean>
    <!-- mongodb的模板类 -->
    <bean id="offlineOneMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="offlineOneMongoDbFactory" />
        <constructor-arg name="mongoConverter" ref="offlineOneMappingMongoConverter" />
        <property name="readPreference" ref="primaryPreferredReadPreference"/>
    </bean>
</beans>

 

 

参数定义:

mongo.offline.connectionsPerHost=16
mongo.offline.threadsAllowedToBlockForConnectionMultiplier=4
mongo.offline.connectTimeout=1000
mongo.offline.maxWaitTime=3000
mongo.offline.autoConnectRetry=true
mongo.offline.socketKeepAlive=true
mongo.offline.socketTimeout=30000
mongo.offline.writeFsync=true
mongo.offline.autoConnectRetryTime=0
mongo.offline.maxConnectionIdleTime=60000
mongo.offline.minConnectionsPerHost=8
mongo.offline.description=
mongo.offline.maxConnectionLifeTime=0



参见:
http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/
https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
http://www.voidcn.com/article/p-wpgrmcpm-bnt.html
https://www.jianshu.com/p/5b1f5f640636




posted @ 2023-08-30 16:21  Angel挤一挤  阅读(503)  评论(0编辑  收藏  举报