The multiple data sources springboot mongo

  1. Profiles
origin:
  mongodb:
    host: 192.168.124.111
    database:data1
    port: 27017

reptile:
  mongodb:
    host: 192.168.124.222
    database: data2
    port: 27017
  1. Configuration class
import com.mongodb.MongoClient;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

public abstract class AbstractMongoConfigure {

    private String host, database;
    private int port;
    /**
     * Method that creates MongoDbFactory
     * Common to both of the MongoDb connections
     */
    public MongoDbFactory mongoDbFactory() throws Exception {

        return new SimpleMongoDbFactory(new MongoClient(host, port), database);
    }


    /**
     * Factory method to create the MongoTemplate
     * @return
     * @throws Exception
     */
    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;
    }
}

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.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@EnableMongoRepositories(basePackages = {"你的repository的包路径"}, mongoTemplateRef = "originMongoTemplate")
@ConfigurationProperties(prefix = "origin.mongodb")//配置的前缀
public class OriginMongoConfigure extends AbstractMongoConfigure {
    @Override
    @Bean(name = "originMongoTemplate")
    @Primary  //一定要标识一个,不然启动会报错
    public MongoTemplate getMongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@EnableMongoRepositories(basePackages = {"你的repository的包路径"}, mongoTemplateRef = "reptileMongoTemplate")
@ConfigurationProperties(prefix = "reptile.mongodb") //配置的前缀
public class ReptileMongoConfigure extends AbstractMongoConfigure {
    @Override
    @Bean(name = "reptileMongoTemplate")
    public MongoTemplate getMongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}

  1. Po write and repository, this is no different with other, not unfold said.
  2. Using
    a. Repository can be used normally
    b. Use of mogoTemplate.
    @Autowired
    @Qualifier("reptileMongoTemplate")
    private MongoTemplate reptileMongoTemplate;

    @Autowired
    @Qualifier("originMongoTemplate")
    private MongoTemplate originMongoTemplate;
Published 202 original articles · won praise 6 · views 30000 +

Guess you like

Origin blog.csdn.net/fall_hat/article/details/103806037