项目当中有时候一个业务需要用到多个数据源,但是又不是微服务的形式,要在一个项目当中配置多个mongo数据源,如何配置呢?

1.springboot 资源文件(两个mongo地址):

spring.data.mongodb.primary.uri=mongodb://192.9.8.204:27017/reslib_server
spring.data.mongodb.secondary.uri=mongodb://192.9.8.204:27017/cloudfsdb

2.写配置类

package com.tyky.educloud.portal.config;

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;

import com.mongodb.MongoClientURI;


@Configuration  //Configuration class  
@ConfigurationProperties(prefix = "spring.data.mongodb.primary") 
@EnableMongoRepositories(basePackages="com.tyky.educloud.portal.repository",mongoTemplateRef="mongoTemplate")
public class PrimaryMongoConfig {
  private String uri;     
  
  @Primary      
  @Bean(name="mongoTemplate")
  public MongoTemplate getMongoTemplate() throws Exception {          
      return new MongoTemplate(mongoDbFactory());      
  }
  
  @Primary
  @Bean(name="mongoDbFactory")
  public MongoDbFactory mongoDbFactory() throws Exception {
      MongoClientURI mongoClientURI = new MongoClientURI(uri);      
      return new SimpleMongoDbFactory(mongoClientURI);
  }

  public String getUri() {
    return uri;
  }

  public void setUri(String uri) {
    this.uri = uri;
  }


}

其中

@EnableMongoRepositories(basePackages="com.tyky.educloud.portal.repository",mongoTemplateRef="mongoTemplate")

指定到你这个数据源对应的仓储类。


第二个数据源配置类:

package com.tyky.educloud.portal.config;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory;
import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy;
import org.springframework.data.mapping.model.FieldNamingStrategy;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.data.support.CachingIsNewStrategyFactory;
import org.springframework.data.support.IsNewStrategyFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;


@Configuration  //Configuration class  
@ConfigurationProperties(prefix = "spring.data.mongodb.secondary") 
public class SecondartyMongoConfig {
  private String uri;   
  
  @Bean(name="cloudfsTemplate")
  public MongoTemplate getMongoTemplate() throws Exception {          
      return new MongoTemplate(mongoDbFactory());      
  }
  
  @Bean(name="secondaryMongoDbFactory")
  public MongoDbFactory mongoDbFactory() throws Exception {
      MongoClientURI mongoClientURI = new MongoClientURI(uri);      
      return new SimpleMongoDbFactory(mongoClientURI);
  }
  
  @Bean(name="cloudfsgrid")
  public GridFsTemplate getGridFsTemplate() throws Exception {          
      return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());   
  }
 

  /**
   * Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration
   * class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending
   * {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is
   * overriden to implement alternate behaviour.
   * 
   * @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
   *         entities.
   */
  protected String getMappingBasePackage() {

      Package mappingBasePackage = getClass().getPackage();
      return mappingBasePackage == null ? null : mappingBasePackage.getName();
  }

  /**
   * Return {@link UserCredentials} to be used when connecting to the MongoDB instance or {@literal null} if none shall
   * be used.
   * 
   * @return
   * @deprecated since 1.7. {@link MongoClient} should hold authentication data within
   *             {@link MongoClient#getCredentialsList()}
   */
  @Deprecated
  protected UserCredentials getUserCredentials() {
      return null;
  }

  /**
   * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package.
   * 
   * @see #getMappingBasePackage()
   * @return
   * @throws ClassNotFoundException
   */
  @Bean
  public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {

      MongoMappingContext mappingContext = new MongoMappingContext();
      mappingContext.setInitialEntitySet(getInitialEntitySet());
      mappingContext.setSimpleTypeHolder(customConversions().getSimpleTypeHolder());
      mappingContext.setFieldNamingStrategy(fieldNamingStrategy());

      return mappingContext;
  }

  /**
   * Returns a {@link MappingContextIsNewStrategyFactory} wrapped into a {@link CachingIsNewStrategyFactory}.
   * 
   * @return
   * @throws ClassNotFoundException
   */
  @Bean
  public IsNewStrategyFactory isNewStrategyFactory() throws ClassNotFoundException {
      return new CachingIsNewStrategyFactory(new MappingContextIsNewStrategyFactory(mongoMappingContext()));
  }

  /**
   * Register custom {@link Converter}s in a {@link CustomConversions} object if required. These
   * {@link CustomConversions} will be registered with the {@link #mappingMongoConverter()} and
   * {@link #mongoMappingContext()}. Returns an empty {@link CustomConversions} instance by default.
   * 
   * @return must not be {@literal null}.
   */
  @Bean
  public CustomConversions customConversions() {
      return new CustomConversions(Collections.emptyList());
  }

  /**
   * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
   * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
   * 
   * @see #customConversions()
   * @see #mongoMappingContext()
   * @see #mongoDbFactory()
   * @return
   * @throws Exception
   */
  @Bean
  public MappingMongoConverter mappingMongoConverter() throws Exception {

      DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
      MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext());
      converter.setCustomConversions(customConversions());

      return converter;
  }

  /**
   * Scans the mapping base package for classes annotated with {@link Document}.
   * 
   * @see #getMappingBasePackage()
   * @return
   * @throws ClassNotFoundException
   */
  protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {

      String basePackage = getMappingBasePackage();
      Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();

      if (StringUtils.hasText(basePackage)) {
          ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
                  false);
          componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
          componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));

          for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
              initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
                      AbstractMongoConfiguration.class.getClassLoader()));
          }
      }

      return initialEntitySet;
  }

  /**
   * Configures whether to abbreviate field names for domain objects by configuring a
   * {@link CamelCaseAbbreviatingFieldNamingStrategy} on the {@link MongoMappingContext} instance created. For advanced
   * customization needs, consider overriding {@link #mappingMongoConverter()}.
   * 
   * @return
   */
  protected boolean abbreviateFieldNames() {
      return false;
  }

  /**
   * Configures a {@link FieldNamingStrategy} on the {@link MongoMappingContext} instance created.
   * 
   * @return
   * @since 1.5
   */
  protected FieldNamingStrategy fieldNamingStrategy() {
      return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy()
              : PropertyNameFieldNamingStrategy.INSTANCE;
  }

  public String getUri() {
    return uri;
  }

  public void setUri(String uri) {
    this.uri = uri;
  }
  
}

3.业务当中的使用:

	@Resource
	@Qualifier("mongoTemplate")
	private MongoOperations mongoOps;

	@Resource
	@Qualifier("cloudfsgrid")
	private GridFsOperations gridFsOps;


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐