小强哥博客

小强哥,小强哥博客,技术大咖

SpringBoot配置多个Mongo数据源

一业务项目一般都是配置一个数据源,偶尔也会配置多个,这里记录一下在SpringBoot1.5.x中如何配置多个mongo数据源。

首先添加Mongo依赖,如下,

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

接着分别创建2个Configuration,如下,

ChannelMongoConfiguration.java

@Configuration
@PropertySource("classpath:application-biz-channel-${spring.profiles.active}.properties")
public class ChannelMongoConfiguration {
	
	@Bean(name = "chanelMongoTemplate")
	public MongoTemplate getChanelMongoTemplate() {
		return new MongoTemplate(getChannelMongoDbFactory());
	}
	
	@Bean(name = "channelMongoDbFactory")
	public MongoDbFactory getChannelMongoDbFactory() {
		MongoClientURI mongoURI = new MongoClientURI(mongoUrl);
		try {
			SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoURI);
			return simpleMongoDbFactory;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Value("${channel.mongo.url}")
	private String mongoUrl;

}

VmsMongoConfiguration.java

@Configuration
@PropertySource("classpath:application-biz-vms-${spring.profiles.active}.properties")
public class VmsMongoConfiguration {
	
	@Bean(name = "vmsMongoTemplate")
	public MongoTemplate getVmsMongoTemplate() {
		return new MongoTemplate(getVmsMongoDbFactory());
	}
	
	@Bean(name = "vmsMongoDbFactory")
	public MongoDbFactory getVmsMongoDbFactory() {
		MongoClientURI mongoURI = new MongoClientURI(mongoUrl);
		try {
			SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoURI);
			return simpleMongoDbFactory;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Value("${vms.mongo.url}")
	private String mongoUrl;


}

两个配置基本上一样,只不过里面的BeanName配置的不一样。

配置完成以后,分别如下使用,

@Service
public class IChannelServiceImpl implements IChannelFace {

	@Autowired
	@Qualifier("chanelMongoTemplate")
	private MongoTemplate mongoTemplate;
	
	@Override
	public void signChannelWithToken(String token) {
		
		if(!"xxxx-xxxx-xxxx-xxxx".equals(token)) {
			throw new AppKeyNotFoundException(token);
		}
		
//		boolean bool = mongoTemplate.exists(Query.query(new Criteria().where("app_key").is(token)), ChannelDO.class);
//		if(!bool) {
//			throw new AppKeyNotFoundException(token);
//		}
	}
	
}

@Service
public class VoiceMessageService {
	
	@Autowired
	@Qualifier("vmsMongoTemplate")
	private MongoTemplate mongoTemplate;
}

通过@Qualifier关键字指定不同的BeanName从而注入不同的Bean。

错误

在启动过程中遇了如下错误,

Description:

Parameter 0 of method mappingMongoConverter in org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration required a single bean, but 2 were found:
	- vmsMongoDbFactory: defined by method 'getVmsMongoDbFactory' in class path resource [com/eju/ms/biz/vms/common/cfg/VmsMongoConfiguration.class]
	- channelMongoDbFactory: defined by method 'getChannelMongoDbFactory' in class path resource [com/eju/ms/biz/sign/common/cfg/ChannelMongoConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

问题原因是由于虽然我们配置了自己的2个mongo连接池,但是springboot自己还是会创建连接池,在创建的过程中发现了我们自定义的2个连接池,因此不知道使用哪一个,所以报错,解决方法如下,

@SpringBootApplication(exclude = MongoDataAutoConfiguration.class)
public class Startup {
	public static void main(String[] args) {
		SpringApplication.run(Startup.class, args);
	}
}

在SpringBootApplication增加exclude = MongoDataAutoConfiguration.class问题解决。

完。