相信看过上一篇文章的小伙伴已经知道了, 这章要讲的就是MongoDB主从配置。
在这边文章中,你将要学到的是在项目中配置主从数据库,并且兼容其他数据库哟。。这些都是博主项目中需要并且比较重要的知识哦~
好了,废话不多说,直接进主题。
1.pom依赖
?
1
2
3
4
|
< span style = "white-space:pre" > </ span >< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-data-mongodb</ artifactId >
</ dependency >
|
2.配置文件的编写
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
## master mongo
master:
mongodb:
host: localhost
port: 27017
database: db_ops
## slave1 mongo
slave1:
mongodb:
host: localhost
port: 27017
database: db_note
## zookeeper注册中心
|
3.配置文件的编写
1.配置父类AbstractMongoConfigure
?
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
|
public abstract class AbstractMongoConfigure {
private String host, database;
private int port;
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(host, port), database);
}
/*
* Factory method to create the MongoTemplate
*/
abstract public MongoTemplate getMongoTemplate() throws Exception;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
|
2.主数据库配置
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableMongoRepositories(basePackages = {"com.jx.ops.mapper.mongodb.ops"},mongoTemplateRef = "opsMongoTemplate")
@ComponentScan
@ConfigurationProperties(prefix = "ops.mongodb")
public class MongoMasterConfig extends AbstractMongoConfigure {
@Override
@Bean(name = "opsMongoTemplate")
@Primary //< span style = "color:#ff0000;" >重点哦</ span >
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
|
3.从数据库配置
?
1
2
3
4
5
6
7
8
9
10
11
12
|
@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableMongoRepositories(basePackages = {"com.jx.ops.mapper.mongodb.post"},mongoTemplateRef = "postMongoTemplate")
@ComponentScan
@ConfigurationProperties(prefix = "post.mongodb")
public class MongoPostConfig extends AbstractMongoConfigure {
@Override
@Bean(name = "postMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
|
到此,主从数据库也讲解完毕,如果有不懂或出bug的小伙伴可以留言我哟。。
以上这篇springboot配置多数据源的实例(MongoDB主从)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/m0_37625860/article/details/78913057
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 49
-
2025-05-29 46
-
2025-05-29 17
-
2025-05-29 49
-
2025-05-27 27
热门评论