#在mongo3.0集羣,使用副本集羣方式.html
pom.xml 加引mongo 3.0 ,spring 版本是4.以上 我這裏使用提4.2.1.RELEASEjava
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.8.0.RELEASE</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.1.0</version> </dependency>
若是不引用mongo-java-driver 其默加自動導入mongo2.0的驅動. mongo2的驅動
<?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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加載mongodb的屬性配置文件 --> <!-- 加載mongodb的屬性配置文件 --> <context:property-placeholder location="classpath:mongodb.properties" /> <mongo:mongo id="mongo" replica-set="${mongo.hostport}"> <mongo:options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}" write-number="1" write-timeout="1000" write-fsync="true"/> </mongo:mongo> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongo" username="${mongo.username}" password="${mongo.password}" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </bean> </beans>
在使用mongo3.0後,這種配置方式,就不支持,官戶提給出的解決方法spring
<mongo:mongo-client id="mongoClient" host="192.168.3.98" port="27017" credentials="用戶名:密碼@擁有權限的數據庫"> <mongo:mongo-client> 這種方式,也一樣會沒有權限,
開始覺得使用replica-set="127.0.0.1:27001,127.0.0.1:27002,127.0.0.1:27003" 這種方式能夠,這種方式在未使用mongo權限時,好像沒有什麼問題,mongo啓動受權後.就會提供沒有權限.mongodb
##spring mongo3 配置文件.數據庫
<?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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加載mongodb的屬性配置文件 --> <context:property-placeholder location="classpath:mongodb.properties" /> <!-- 定義mongo對象,對應的是mongodb官方jar包中的Mongo, --> <!-- MongoClient --> <bean id="mongoClient" class="com.mongodb.MongoClient"> <constructor-arg name="credentialsList"> <list> <!-- Credentials --> <bean class="com.mongodb.MongoCredential"> <constructor-arg name="mechanism" value = "#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}" /> <constructor-arg type="java.lang.String" name="userName" value="${mongo.username}" /> <constructor-arg type="java.lang.String" name="source" value="${mongo.dbname}" /> <constructor-arg type="char[]" name="password" value="${mongo.password}" /> </bean> </list> </constructor-arg> <constructor-arg name="seeds"> <list> <bean class="com.mongodb.ServerAddress"> <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters1.host}" /> <constructor-arg type="int" name="port" value="${mongo.sharded_clusters1.port}" /> </bean> <bean class="com.mongodb.ServerAddress"> <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters2.host}" /> <constructor-arg type="int" name="port" value="${mongo.sharded_clusters2.port}" /> </bean> <bean class="com.mongodb.ServerAddress"> <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters2.host}" /> <constructor-arg type="int" name="port" value="${mongo.sharded_clusters3.port}" /> </bean> </list> </constructor-arg> </bean> <!-- MongoDbFactory --> <bean id="simpleMongoDbFactoryID" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> <constructor-arg ref="mongoClient" /> <constructor-arg name="databaseName" value="${mongo.dbname}" /> </bean> <!-- MongoTemplate --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="simpleMongoDbFactoryID" /> </bean> </beans>
mongo.sharded_clusters1.host=127.0.0.1 mongo.sharded_clusters2.host=127.0.0.1 mongo.sharded_clusters3.host=127.0.0.1 mongo.sharded_clusters1.port=27051 mongo.sharded_clusters2.port=27051 mongo.sharded_clusters3.port=27051 mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=200 mongo.connectTimeout=10000 mongo.maxWaitTime=50000 mongo.autoConnectRetry=true mongo.socketKeepAlive=true mongo.socketTimeout=50000 mongo.slaveOk=true mongo.username=myuser mongo.password=123456 mongo.dbname=mydb mongo.maxConnectionLifeTime=0 mongo.heartbeatConnectTimeout=0 mongo.heartbeatSocketTimeout=0
mongo3.0 使用Spring須要使用4.0以上的版本,mongo3.集羣使用權限,
參考 http://stackoverflow.com/questions/28958789/connecting-to-mongodb-3-0-with-java-spring https://docs.mongodb.org/manual/core/authentication/ http://docs.spring.io/spring-data/data-mongo/docs/1.8.4.RELEASE/reference/html/#mongo.mongo-3.authenticationsocket