springboot配置多个mongodb数据源

springboot配置多个mongodb数据源

最近做个资源整合的小项目,在本地获取资源数据,再保存至远程服务器的mongodb。后来想在本地也保存一份,就需要使用2个mongodb的数据源了。
工程如下:
springboot配置多个mongodb数据源_第1张图片

新建一个配置父类AbstractMongoConfig:

package cn.***.configuration;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public abstract class AbstractMongoConfig{
    protected String host;
    protected int port;
    protected String username;
    protected String password;
    protected String database;
    public MongoDbFactory mongoDbFactory(){
        ServerAddress serverAddress = new ServerAddress(host, port);
        List credentialsList = new ArrayList();
        credentialsList.add(MongoCredential.createCredential(username, database, password.toCharArray()));
        return new SimpleMongoDbFactory(new MongoClient(serverAddress, credentialsList),database);
    }
    public abstract MongoTemplate getMongoTemplate();
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getDatabase() {
        return database;
    }
    public void setDatabase(String database) {
        this.database = database;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

新建第一个mongodb数据源配置类:

package cn.***.configuration;

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 com.mongodb.MongoClient;

@Configuration
@ConfigurationProperties(prefix="spring.data.mongodb.primary")
public class PrimaryMongoConfig extends AbstractMongoConfig{

    /**
     * 本地mongodb不需要密码,用这个
     */
    public MongoDbFactory mongoDbFactory(){
        return new SimpleMongoDbFactory(new MongoClient(host, port), database);
    }

    @Primary
    @Bean(name="primaryMongoTemplate")
    @Override
    public MongoTemplate getMongoTemplate() {
        return new MongoTemplate(mongoDbFactory());
    }
}

新建第二个配置类:

package cn.***.configuration;

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;

@Configuration
@ConfigurationProperties(prefix="spring.data.mongodb.secondary")
public class SecondaryMongoConfig extends AbstractMongoConfig{

    @Bean(name="secondaryMongoTemplate")
    @Override
    public MongoTemplate getMongoTemplate() {
        return new MongoTemplate(mongoDbFactory());
    }
}

配置文件:application.properties

#mongodb configuration local windows
spring.data.mongodb.primary.database=mytestdb
spring.data.mongodb.primary.host=127.0.0.1
spring.data.mongodb.primary.port=27017
#mongodb configuration remote
spring.data.mongodb.secondary.database=mytestdb
spring.data.mongodb.secondary.host=**********
spring.data.mongodb.secondary.username=********
spring.data.mongodb.secondary.password=********
spring.data.mongodb.secondary.port=27017

使用:

package .......
import ......
@Repository
public class RecordDaoImpl implements RecordDao{
    @Resource(name="primaryMongoTemplate")
    private MongoTemplate mongoTemplate_local;
    @Resource(name="secondaryMongoTemplate")
    private MongoTemplate mongoTemplate_remote;

    @Override
    public boolean add(Record record){
        try{
            mongoTemplate_local.insert(record);
            mongoTemplate_remote.insert(record);
            return true;
        }catch(DuplicateKeyException e){
            return false;
        }
    }
}

你可能感兴趣的:(java)