文档章节

崛起于Springboot2.X之Mongodb多数据源处理(35)

木九天
 木九天
发布于 08/16 11:31
字数 675
阅读 48
收藏 0

多数据源:4个mongodb库!

目录结构图:

1、添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2、application.properties

spring.data.mongodb.first.database=node1
spring.data.mongodb.first.uri=localhost:27017

spring.data.mongodb.second.database=node2
spring.data.mongodb.second.uri=localhost:27017

spring.data.mongodb.third.database=node3
spring.data.mongodb.third.uri=localhost:27017

spring.data.mongodb.fourth.database=node4
spring.data.mongodb.fourth.uri=localhost:27017

3、配置类,一个主类,四个各自的mongodb配置类

@Configuration
public class MultipleMongoProperties {

    @Bean(name="firstMongoProperties")
    @Primary
    @ConfigurationProperties(prefix="spring.data.mongodb.first")
    public MongoProperties firstMongoProperties() {
        System.out.println("-------------------- statisMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="secondMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.second")
    public MongoProperties secondMongoProperties() {
        System.out.println("-------------------- listMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="thirdMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.third")
    public MongoProperties thirdMongoProperties() {
        System.out.println("-------------------- thirdMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="fourthMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.fourth")
    public MongoProperties fourthMongoProperties() {
        System.out.println("-------------------- fourthMongoProperties init ---------------------");
        return new MongoProperties();
    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.first", mongoTemplateRef = "firstMongo")
public class FirstMongoTemplate {

    @Autowired
    @Qualifier("firstMongoProperties")
    private MongoProperties mongoProperties;

    @Primary
    @Bean(name = "firstMongo")
    public MongoTemplate firstMongoTemplate() throws Exception {
        return new MongoTemplate(firstFactory(this.mongoProperties));
    }

    @Bean
    @Primary
    public MongoDbFactory firstFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.second", mongoTemplateRef = "secondMongo")
public class SecondMongoTemplate {
    @Autowired
    @Qualifier("secondMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "secondMongo")
    public MongoTemplate secondTemplate() throws Exception {
        return new MongoTemplate(secondFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory secondFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.third", mongoTemplateRef = "thirdMongo")
public class ThirdMongoTemplate {

    @Autowired
    @Qualifier("thirdMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "thirdMongo")
    public MongoTemplate thirdTemplate() throws Exception {
        return new MongoTemplate(thirdFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory thirdFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.fourth", mongoTemplateRef = "fourthMongo")
public class FourthMongoTemplate {

    @Autowired
    @Qualifier("fourthMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "fourthMongo")
    public MongoTemplate fourthTemplate() throws Exception {
        return new MongoTemplate(fourthFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory fourthFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }

}

4、dao层

    看我刚刚的目录结构图:在dao下面创建四个first、second、third、fourth文件夹,对应下面的四个mongodb数据库repository

public interface FirstRepository extends MongoRepository<People,String> {
}
public interface SecondRepository extends MongoRepository<People,String> {
}
public interface ThirdRepository extends MongoRepository<People,String> {
}
public interface FourthRepository extends MongoRepository<People,String> {
}

5、entity层

也是如上的操作,他们没有在同一个文件夹下面

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "first_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "second_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "third_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "fourth_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}

6、controller层

@Controller
@RequestMapping(value = "/mongodb")
public class TestController {

    @Autowired
    FirstRepository firstRepository;

    @Autowired
    SecondRepository secondRepository;

    @Autowired
    ThirdRepository thirdRepository;

    @Autowired
    FourthRepository fourthRepository;

    @GetMapping(value = "/test1")
    public void test1(){
        firstRepository.save(new com.dtb.mongodb.entity.first.People("1","第一个"));
    }

    @GetMapping(value = "/test2")
    public void test2(){
        secondRepository.save(new People("2","添加第二个数据"));
    }


    @GetMapping(value = "/test3")
    public void test3(){
        thirdRepository.save(new com.dtb.mongodb.entity.third.People("1","第三个"));
    }

    @GetMapping(value = "/test4")
    public void test4(){
        fourthRepository.save(new com.dtb.mongodb.entity.fourth.People("1","第四个"));
    }
}

7、添加成功!

 

 

© 著作权归作者所有

共有 人打赏支持
木九天

木九天

粉丝 79
博文 179
码字总数 123401
作品 0
海淀
程序员
崛起于Springboot2.X之多节点mongodb实现区块链交易记录(36)

序言:根据前两篇崛起于Springboot2.X之区块链单节点mysql实现交易记录和崛起于Springboot2.X之Mongodb多数据源处理,进行整合,实现区块链多节点mongodb实现交易记录方案。直接上代码了,不...

木九天
08/23
0
0
MongoDB之Hadoop驱动介绍

MongoDB之Hadoop驱动介绍 ------------------------ 1. 一些概念 Hadoop 是一套Apache开源的分布式计算框架,其中包括了分布式文件系统DFS与分布式计算模型MapReduce,而MongoDB是一个面向文...

李长春
2011/09/02
0
1
Spring boot中mongodb的使用

mongodb的增删改查 Spring Boot对各种流行的数据源都进行了封装,当然也包括了mongodb,下面给大家介绍如何在spring boot中使用mongodb: 1、pom包配置 pom包里面添加spring-boot-starter-dat...

glen_xu
06/11
0
0
MongoDB 4.0 RC 版本强势登陆

MongoDB 因其灵活的文档模型、可扩展分布式设计广受开发者喜爱,在此基础上,MongoDB 4.0 推出了更强大的功能支持,目前4.0第一个RC版本已经发布,本文将介绍 MongoDB 4.0 核心的一些新特性。...

张友东
05/30
0
0
Yum安装MongoDB及数据库管理

MongoDB简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB旨在为WEB应用提供可扩展的高性能数据...

cchenyz
07/13
0
0

没有更多内容

加载失败,请刷新页面

加载更多

记录一次idea断点打不上的解决办法和猜测原因

写完代码,需要在本地调试一下,却发现断点打不上,正常打上断点应该是红点加勾,如下图: 但是今天打断点却出现圈+斜杠,类似下面这种: 出现这种情况大概有两种可能,一种是断点打在了错误...

edwardGe
11分钟前
0
0
使用PHP辅助快速制作一套自己的手写字体实践

一、背景 笔者以前在网上看到有民间高手制作字体的相关事迹,觉得把自己的手写字用键盘敲出来是一件很有意思的事情,所以一直有时间想制作一套自己的手写体,前几天在网上搜索了一下制作字体...

Mr_zebra
12分钟前
0
0
Python 中实现装饰器 @functools.wraps 的作用

废话不说,先看两端代码: 1、使用了@functools.wraps import timeimport functoolsdef timeit(func): @functools.wraps(func) def wrapper(): start = time.clock()...

fang_faye
15分钟前
0
0
spark任务出现Lost executor报错的几点解决方案

1、spark.executor.extraJavaOptions="-XX:MaxPermSize=2028m" 2、spark.akka.frameSize=1003、增加executor内存executor默认的永久代内存是64K,可以看到永久代使用率长时间为99%,通过...

KYO4321
23分钟前
0
0
全文检索Solr集成HanLP中文分词

以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在Github上,...

左手的倒影
23分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部