spring4.0 配置mongo3.0集群

匆匆过客 提交于 2019-12-07 12:17:37

#在mongo3.0集群,使用副本集群方式.

pom.xml 加引mongo 3.0 ,spring 版本是4.以上 我这里使用提4.2.1.RELEASE

     <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的驱动
  • 这里给提供一个mongo2.0spring配置.
    <?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后,这种配置方式,就不支持,官户提给出的解决方法

     <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启动授权后.就会提供没有权限.

##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>

  • mongodb.properties 配置文件.
    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.authentication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!