标题:
Usage of 'UserCredentials' with 'MongoClient' is no longer supported(2)
[打印本页]
作者:
look_w
时间:
2019-5-16 19:09
标题:
Usage of 'UserCredentials' with 'MongoClient' is no longer supported(2)
解决方式
方式一monogo模式
使用简单的mongo模式配置如下:
<mongo:db-factory id="logMongoDbFactory"
host="${mongo.log.server}"
port="${mongo.log.port}"
dbname="${mongo.log.db}"
username="${mongo.log.user}"
password="${mongo.log.password}"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
方式二 mongoClient模式
mongoClient模式是比较新的配置方式,可以带更多的参数,在原配置基础上只需要调整一下账号密码的配置方式如下:
<mongo:mongo-client id="logMongoClient" host="${mongo.log.server}" port="${mongo.log.port}" credentials="${mongo.log.user}
{mongo.log.password}@${mongo.log.db}">
<!-- com.mongodb.MongoClientOptions -->
<mongo:client-options
connections-per-host="${mongo.log.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.log.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.log.connectTimeout}"
max-wait-time="${mongo.log.maxWaitTime}"
socket-keep-alive="${mongo.log.socketKeepAlive}"
socket-timeout="${mongo.log.socketTimeout}"
max-connection-life-time="${mongo.log.connectLifeTime}"/>
</mongo:mongo-client>
<mongo:db-factory id="logMongoDbFactory"
dbname="${mongo.log.db}"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
如果是副本集,则使用如下配置:
<mongo:mongo-client id="logMongoClient" replica-set="${mongo.log.host}
{mongo.log.port}" credentials="${mongo.log.user}
{mongo.log.password}@${mongo.log.db}">
<!-- com.mongodb.MongoClientOptions -->
<mongo:client-options
connections-per-host="${mongo.log.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.log.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.log.connectTimeout}"
max-wait-time="${mongo.log.maxWaitTime}"
socket-keep-alive="${mongo.log.socketKeepAlive}"
socket-timeout="${mongo.log.socketTimeout}"
max-connection-life-time="${mongo.log.connectLifeTime}"/>
</mongo:mongo-client>
<mongo:db-factory id="logMongoDbFactory"
dbname="${mongo.log.db}"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
注意事项
注意集群下连接的mongodb的host为主服务器的地址,而非从服务器的地址。
然后mongo:db-factory需要带 dbname参数,即使credentials="mongo.log.user:
mongo.log.user:{mongo.log.password}@${mongo.log.db}"中已经带了db的参数,但是这个是认证用的。
否则会报错无法通过认证。
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0