阿里云mongodb库(3.2)(官方要求:请使用MongoDB 3.0以上版本的driver)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
省略。。。其它spring配置
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<!--<version>1.7.0.RELEASE</version> 需要spring4-->
<!--<version>1.6.4.RELEASE</version> 需要spring4-->
<version>1.4.2.RELEASE</version>
<exclusions><!-- 去就近的mongo-java-driver依赖,因为要支持mongodb 3.x-->
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.14.2</version>
</dependency>
<?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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Default bean name is 'mongo' -->
<!--<mongo:mongo host="${mongo.host}" port="${mongo.port}"/>
<mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo" username="${mongo.username}" password="${mongo.password}" />-->
<mongo:db-factory uri="${mongo.uri}"/> <!--这种方式可以配置多主机 -->
<!-- by default look for a Mongo object named 'mongo' - default name used for the converter is 'mappingConverter' -->
<bean id="readConverter" class="com.***.mongo.converters.UserRequestReader"/>
<mongo:mapping-converter base-package="com.***">
<mongo:custom-converters>
<mongo:converter ref="readConverter"/>
<mongo:converter>
<bean class="com.***.mongo.converters.UserRequestWriter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<!-- set the mapping converter to be used by the MongoTemplate -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
<!-- <bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/> -->
</beans>
追加:
1、上线时可能出现这样的异常
Caused by: com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of50 has been exceeded.
...
原因是spring-data-mongo 版本比较旧,默认传给driver的最大连接池数,只有10,乘上因子(默认5),最大只能支持10*5=50个线程并发请求。方法也很简单,自定义maxPoolSize参数即可,比如定义为200,这样单个java进程可以支持200*5=1000个线程并发请求mongodb
mongodb://{userName}:{password}@{host1:port1},{host2:port2}/{dbname}?replicaSet={replicaSetName}&maxPoolSize=200
2、假如按照阿里云文档(iptable或者rinetd)配置外网访问
mongo uri改成mongodb://{userName}:{password}@{host1:port1}/{dbname}?maxPoolSize=200,去掉replicaSet参数,可以避免客户端将服务端ip转成内网,导致访问不了。
配置外网可访问,要注意主备切换会导致代理配置需要相应改动,建议仅仅用于测试环境。