springboot(2.0.2.RELEASE) mongo多数据源配置

添加maven依赖

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

application.yml配置

spring:
  data:
    mongodb:
      mobile:
        host: 192.168.1.179
        port: 27019
        database: databasename
        username: username
        password: xxx
        authentication-database: admin
      md5:
        host: 192.168.1.179
        port: 27020
        database: databasename
        username: username
        password: xxxx
        authentication-database: admin

抽象config配置

package com.huiyu.ftp.config.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

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

/**
 * @author chenpeng
 * @date 2018/7/30
 */
public abstract class AbstractMongoConfig {
    /**
     * host
     */
    private String host;
    /**
     * 端口号 27017
     */
    private Integer port;
    /**
     * 认证用户名
     */
    private String username;
    /**
     * 认证密码
     */
    private String password;
    /**
     * 库名
     */
    private String database;
    /**
     * 认证库名,一般是admin
     */
    private String authenticationDatabase;

    protected MongoDbFactory getMongoDbFactory() {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return new SimpleMongoDbFactory(new MongoClient(host, port), database);
        }

        //这里暂时只不考虑集群操作
        List<ServerAddress> seeds = new ArrayList<>();
        ServerAddress serverAddress = new ServerAddress(host, port);
        seeds.add(serverAddress);


        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        mongoCredentialList.add(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray()));
        return new SimpleMongoDbFactory(new MongoClient(seeds, mongoCredentialList), database);
    }


    /**
     * 继承类必须实现这个接口
     *
     * @return mongoTemplate实例
     */
    abstract public MongoTemplate getMongoTemplate();

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getAuthenticationDatabase() {
        return authenticationDatabase;
    }

    public void setAuthenticationDatabase(String authenticationDatabase) {
        this.authenticationDatabase = authenticationDatabase;
    }
}

为每个数据源写一个唯一的template

public class MobileMongoTemplate extends MongoTemplate {
    public MobileMongoTemplate(MongoClient mongoClient, String databaseName) {
        super(mongoClient, databaseName);
    }

    public MobileMongoTemplate(MongoDbFactory mongoDbFactory) {
        super(mongoDbFactory);
    }

    public MobileMongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {
        super(mongoDbFactory, mongoConverter);
    }
}

数据源配置

package com.huiyu.ftp.config.mongo;

import com.huiyu.ftp.config.mongo.templates.MobileMongoTemplate;
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.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * @author chenpeng
 * @date 2018/7/30
 */
@Configuration
@EnableMongoRepositories(mongoTemplateRef = "mobileMongoTemplate")
@ConfigurationProperties(prefix = "spring.data.mongodb.mobile")
public class MobileMongoTemplateConfig extends AbstractMongoConfig {
    @Override
    @Primary
    @Bean(name = "mobileMongoTemplate")
    public MobileMongoTemplate getMongoTemplate() {
        MongoDbFactory mongoDbFactory = this.getMongoDbFactory();
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return new MobileMongoTemplate(mongoDbFactory, mappingConverter);
    }
}

其他数据源按照这样配置就好了

测试时,需要用到某个数据源只需要按照下面这样引用即可

 @Resource
 private MobileMongoTemplate mobileMongoTemplate;

去除自动连接 localhost:27012

在入口文件进行配置即可

package com.huiyu.ftp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author chenpeng
 */
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class FtpManagerWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(FtpManagerWebApplication.class, args);
    }
}

results matching ""

    No results matching ""