applicationContext-AppConfig.xml 内容
spring-boot 配置:
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:*.properties" />
<!-- mongo依赖 -->
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo-client id="mongo" replica-set="${mongo.hostport}" credentials="${mongo.userPassDb}" >
<!-- 一些连接属性的设置 -->
<mongo:client-options connections-per-host="10"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" />
</mongo:mongo-client>
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory dbname="leopard" mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="leopard" />
</bean>
配置文件内容
application.properties
mongo.autoConnectRetry=true
mongo.connectTimeout=10000
mongo.connectionsPerHost=10
mongo.hostport=地址1\:端口,地址2\:端口?replicaSet\=mgset-4434465
mongo.maxWaitTime=1500
mongo.slaveOk=true
mongo.socketKeepAlive=true
mongo.socketTimeout=10000
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.userPassDb=用户名\:密码@数据库名
hostport : 可配置多个mongo地址实现负载,
其中 replicaSet 参数为复制集,作用:使用N个mongo地址,自动容错,自动恢复,高可用
userPassDb :mongo配置文件加入权限后,设置可访问的用户名和密码
常用 spring 配置:
<context:property-placeholder location="classpath:*.properties" />
<!-- mongo依赖 -->
<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="${mongo.userName}" />
<constructor-arg name="password" value="${mongo.password}" />
</bean>
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo id="mongo" replica-set="${mongo.hostport}">
<!-- 一些连接属性的设置 -->
<mongo:options connections-per-host="10"
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="0" write-fsync="true" />
</mongo:mongo>
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory dbname="leopard" mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="leopard" />
<constructor-arg ref="userCredentials" />
</bean>
配置文件内容
mongo.properties
mongo.hostport=地址1\:端口,地址2\:端口?replicaSet\=mgset-4434465
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=10000
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=10000
mongo.slaveOk=true
mongo.userName=用户名
mongo.password=密码
与spring-boot相比较,只有 用户名、密码、数据库名、xml文件中前缀名,的引用有所不同
在拉取的 docker-compose 文件中加入 “ command: [--auth] ” 如下,便可开启权限限制
version: '2'
services:
mongo:
# restart: always
image: mongo:3.2
command: [--auth]
ports:
- "37017:27017"
volumes:
- /data/db