9a1c0988c46434449b00714d843cb769
SpringBoot整合MongoDB多数据源

依赖

// build.gradle

implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

配置文件

# application.yml

spring:
  data:
    mongodb:
      primary:
        uri: mongodb://localhost:27017/db1
      secondary:
        uri: mongodb://localhost:27017/db2

主数据库配置

// PrimaryMongoConfig.java

package com.fengwenyi.springboot_mongo_multi_source.config;

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * MongoDB Primary Config
 * @author Erwin Feng
 * @since 2019-07-01 17:12
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.fengwenyi.springboot_mongo_multi_source.primary",
        mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.data.mongodb.primary")
    public MongoProperties primaryMongoProperties() {
        return new MongoProperties();
    }

    @Primary
    @Bean(name = "primaryMongoTemplate")
    public MongoTemplate primaryMongoTemplate() throws Exception {
        return new MongoTemplate(primaryFactory(primaryMongoProperties()));
    }

    @Bean
    @Primary
    public MongoDbFactory primaryFactory(MongoProperties mongoProperties) throws Exception {
        return new SimpleMongoDbFactory(new MongoClientURI(primaryMongoProperties().getUri()));
    }
}

副数据库配置

// SecondaryMongoConfig.java

package com.fengwenyi.springboot_mongo_multi_source.config;

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * MongoDB Secondary Config
 * @author Erwin Feng
 * @since 2019-07-01 17:12
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.fengwenyi.springboot_mongo_multi_source.secondary",
        mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {

    @Bean
    @ConfigurationProperties(prefix="spring.data.mongodb.secondary")
    public MongoProperties secondaryMongoProperties() {
        return new MongoProperties();
    }

    @Bean(name = "secondaryMongoTemplate")
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(secondaryMongoProperties()));
    }

    @Bean
    public MongoDbFactory secondaryFactory(MongoProperties mongoProperties) throws Exception {
        return new SimpleMongoDbFactory(new MongoClientURI(secondaryMongoProperties().getUri()));
    }
}

用户实体,副

// User.java

package com.fengwenyi.springboot_mongo_multi_source.secondary.entity;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.time.Instant;

/**
 * 用户
 * @author Erwin Feng
 * @since 2019-07-01 17:15
 */
@Data
@Accessors(chain = true)
@Document(collection = "t_user")
public class User implements Serializable {

    private static final long serialVersionUID = -7229906944062898852L;

    /** ID */
    @Id
    private String id;

    /** 用户名 */
    private String username;

    /** 年龄 */
    private Integer age;

    /** 注册时间 */
    private Instant registerTime;
}

用户查询仓库,副

// UserRepository.java

package com.fengwenyi.springboot_mongo_multi_source.secondary.repository;

import com.fengwenyi.springboot_mongo_multi_source.secondary.entity.User;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
 * 用户
 * @author Erwin Feng
 * @since 2019-07-01 17:18
 */
public interface UserRepository extends MongoRepository<User, String> {

    /**
     * 通过用户名查询
     * @param username 用户名
     * @return
     */
    List<User> findAllByUsername(String username);

}

登录日志实体,主

```java
// LoginLog.java

package com.fengwenyi.springboot_mongo_multi_source.primary.entity;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;

import java.io.Serializable;
import java.time.Instant;

/**

  • 登录日志

  • @author Erwin Feng

  • @since 2019-07-01 17:18
    */
    @Data
    @Accessors(chain = true)
    public class LoginLog implements Serializable {

    private static final long serialVersionUID = -6694661682102504919L;

    /** ID */
    @Id
    private String id;

top Created with Sketch.