一、安装mongodb4.0.3(××)
1.1、官方安装文档
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
1.2、tar.gz包下载地址
https://www.mongodb.com/download-center/community?jmp=docs
1.3、复制集官方配置
https://docs.mongodb.com/manual/administration/replica-set-member-configuration/
二、springproject版本依赖(×)
spring:5.1.1.release 以上
springdatamongodb:2.1.1.release 以上
三、配置mongotransactionmanager(springboot使用方式)
springboot 集成非常简单,配置mongotransastionmanager,目前spring-boot-starter-data-mongodb 版本较低不支持,因此暂未验证,springboot配置复制集参考springboot相关文档
1
2
3
4
5
6
7
8
9
10
|
@configuration static class config extends abstractmongoconfiguration { @bean mongotransactionmanager transactionmanager(mongodbfactory dbfactory) { return new mongotransactionmanager(dbfactory); } // ... } |
四、配置mongotransactionmanager(普通spring项目)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<context:property-placeholder location= "classpath:mongo.properties" ignore-unresolvable= "true" file-encoding= "utf-8" /> <!-- 重点配置 1 .安全配置:credentials= "${mongo.user}:${mongo.password}@${mongo.database}" 2 .单机配置:host= "${mongo.host}" port= "${mongo.port}" 3 .副本集配置:replica-set= "${mongo.replset}" --> <mongo:mongo-client id= "mongoclient" replica-set= "${mongo.replset}" > <mongo:client-options min-connections-per-host= "${mongo.minconnectionsperhost}" write-concern= "safe" read-preference= "nearest" 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}" max-connection-idle-time= "${mongo.maxconnectionidletime}" max-connection-life-time= "${mongo.maxconnectionlifetime}" heartbeat-socket-timeout= "${mongo.heartbeatsockettimeout}" heartbeat-connect-timeout= "${mongo.heartbeatconnecttimeout}" min-heartbeat-frequency= "${mongo.minheartbeatfrequency}" heartbeat-frequency= "${mongo.heartbeatfrequency}" /> </mongo:mongo-client> <mongo:db-factory id= "mongodbfactory" dbname= "${mongo.database}" mongo-ref= "mongoclient" /> <bean id= "mappingcontext" class = "org.springframework.data.mongodb.core.mapping.mongomappingcontext" /> <bean id= "defaultmongotypemapper" class = "org.springframework.data.mongodb.core.convert.defaultmongotypemapper" > <constructor-arg name= "typekey" > < null /> </constructor-arg> </bean> <bean id= "mappingmongoconverter" class = "org.springframework.data.mongodb.core.convert.mappingmongoconverter" > <constructor-arg name= "mongodbfactory" ref= "mongodbfactory" /> <constructor-arg name= "mappingcontext" ref= "mappingcontext" /> <property name= "typemapper" ref= "defaultmongotypemapper" /> </bean> <bean id= "mongotemplate" class = "org.springframework.data.mongodb.core.mongotemplate" > <constructor-arg name= "mongodbfactory" ref= "mongodbfactory" /> <constructor-arg name= "mongoconverter" ref= "mappingmongoconverter" /> </bean> <!-- 重点配置 --> <bean id= "mongotransactionmanager" class = "org.springframework.data.mongodb.mongotransactionmanager" > <constructor-arg name= "dbfactory" ref= "mongodbfactory" /> </bean> <tx:annotation-driven transaction-manager= "mongotransactionmanager" proxy-target- class = "true" /> |
五、使用事务(×)
1
2
3
4
5
6
7
8
9
10
11
12
|
@service public class stateservice { @autowired private mongotemplate teplate; @transactional void somebusinessfunction(step step) { template.insert(step); log.debug( 1 / 0 ); }; }); |
六、mongo.properties 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
mongo.host= 192.168 . 1.66 mongo.port= 27017 mongo.replset= 192.168 . 1.66 : 2000 , 192.168 . 1.66 : 2001 , 192.168 . 1.66 : 2002 mongo.minconnectionsperhost= 10 mongo.threadsallowedtoblockforconnectionmultiplier= 4 mongo.connecttimeout= 30000 mongo.maxwaittime= 3000 mongo.socketkeepalive= false mongo.sockettimeout= 30000 mongo.maxconnectionidletime= 0 mongo.maxconnectionlifetime= 0 mongo.heartbeatsockettimeout= 20000 mongo.heartbeatconnecttimeout= 10000 mongo.minheartbeatfrequency= 3000 mongo.heartbeatfrequency= 5000 mongo.database=labsta |
七、最后是重点(×××××)
7.1、必须使用复制集,单节点不支持,mongodb4.2 会支持分片集事务,复制集安装请自行研究
7.2、使用事务之前(@transaction),数据库和文档必须提前创建,如何解决呢?且看下篇
7.3、spring版本和springdatamongodb版本必须遵循 @springproject版本依赖
7.4、注意,此处的复制集不是4.0版本之前的主从复制集群,4.0之后已废弃主从模式
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/2faf0e6b6627