欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot-mongodb的多数据源配置的方法步骤

程序员文章站 2024-02-24 15:35:34
在日常工作中,我们可能需要连接多个mongodb数据源,比如用户库user,日志库log。本章我们来记录连接多个数据源的步骤,以两个数据源为例,多个数据源类推。 1、po...

在日常工作中,我们可能需要连接多个mongodb数据源,比如用户库user,日志库log。本章我们来记录连接多个数据源的步骤,以两个数据源为例,多个数据源类推。

1、pom.xml中引入mongodb的依赖

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-data-mongodb</artifactid>
</dependency>
<dependency>
  <groupid>org.projectlombok</groupid>
  <artifactid>lombok</artifactid>
</dependency>
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-autoconfigure</artifactid>
  <version>release</version>
</dependency>

lombok - 是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。简单试了以下这个工具还挺好玩的,加上注解我们就不用手动写 getter\setter、构建方式类似的代码了。

2, yml配置:

mongodb:
 primary:
  host: 192.168.9.60
  port: 20000
  database: test
 secondary:
  host: 192.168.9.60
  port: 20000
  database: test1

3, 配置2个库的数据源:

@data
@configurationproperties(prefix = "mongodb")
public class multiplemongoproperties {

  private mongoproperties primary = new mongoproperties();
  private mongoproperties secondary = new mongoproperties();
}

3.1) 第一个库封装:

@configuration
@enablemongorepositories(basepackages = "com.neo.model.repository.primary",
    mongotemplateref = primarymongoconfig.mongo_template)
public class primarymongoconfig {

  protected static final string mongo_template = "primarymongotemplate";
}

3.2) 第二个库封装 :

@configuration
@enablemongorepositories(basepackages = "com.neo.model.repository.secondary",
    mongotemplateref = secondarymongoconfig.mongo_template)
public class secondarymongoconfig {

  protected static final string mongo_template = "secondarymongotemplate";
}

3.3) mongotemplate:

@configuration
public class multiplemongoconfig {

  @autowired
  private multiplemongoproperties mongoproperties;

  @primary
  @bean(name = primarymongoconfig.mongo_template)
  public mongotemplate primarymongotemplate() throws exception {
    return new mongotemplate(primaryfactory(this.mongoproperties.getprimary()));
  }

  @bean
  @qualifier(secondarymongoconfig.mongo_template)
  public mongotemplate secondarymongotemplate() throws exception {
    return new mongotemplate(secondaryfactory(this.mongoproperties.getsecondary()));
  }

  @bean
  @primary
  public mongodbfactory primaryfactory(mongoproperties mongo) throws exception {
    return new simplemongodbfactory(new mongoclient(mongo.gethost(), mongo.getport()),
        mongo.getdatabase());
  }

  @bean
  public mongodbfactory secondaryfactory(mongoproperties mongo) throws exception {
    return new simplemongodbfactory(new mongoclient(mongo.gethost(), mongo.getport()),
        mongo.getdatabase());
  }
}

4, 创建2个库对应的repository:

@data
@allargsconstructor
@noargsconstructor
@document(collection = "first_mongo")
public class primarymongoobject {

  @id
  private string id;

  private string value;

  @override
  public string tostring() {
    return "primarymongoobject{" + "id='" + id + '\'' + ", value='" + value + '\''
        + '}';
  }
}
public interface primaryrepository extends mongorepository<primarymongoobject, string> {
}

测试:

@runwith(springrunner.class)
@springboottest
public class mulidatabasetest {

  @autowired
  private primaryrepository primaryrepository;

  @autowired
  private secondaryrepository secondaryrepository;

  @test
  public void testsave() {

    system.out.println("************************************************************");
    system.out.println("测试开始");
    system.out.println("************************************************************");

    this.primaryrepository
        .save(new primarymongoobject(null, "第一个库的对象"));

    this.secondaryrepository
        .save(new secondarymongoobject(null, "第二个库的对象"));

    list<primarymongoobject> primaries = this.primaryrepository.findall();
    for (primarymongoobject primary : primaries) {
      system.out.println(primary.tostring());
    }

    list<secondarymongoobject> secondaries = this.secondaryrepository.findall();

    for (secondarymongoobject secondary : secondaries) {
      system.out.println(secondary.tostring());
    }

    system.out.println("************************************************************");
    system.out.println("测试完成");
    system.out.println("************************************************************");
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。