SpringBoot 1.5 JPA MongoDB 設置多數據源

SpringBoot 1.5 JPA MongoDB 設置多數據源

背景

某些特定場合,需要分庫查詢。
如:敏感數據分庫存儲,日誌數據分庫存儲等。

1.環境依賴

基於Springboot 1.5.6.RELEASE

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

2.配置文件

server:
  port: 8081
spring:
  data:
    mongodb:
      primary:
        uri: mongodb://root:123root@127.0.0.1:27017/ltz1
      secondary:
        uri: mongodb://root2:123root@127.0.0.1:27017/ltz2

3.Config配置

①.第一數據源配置(primary)

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
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;

/**
 * @author litong
 * @date 2020/1/19 14:40
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.ltz.demo.primary.dao",
		mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {

	@Bean
	@Primary
	@ConfigurationProperties(prefix = "spring.data.mongodb.primary")
	public MongoProperties primaryMongoProperties() {
		return new MongoProperties();
	}

	@Primary
	@Bean(name = "primaryMongoTemplate")
	public MongoTemplate primaryMongoTemplate() throws Exception {
		return new MongoTemplate(primaryFactory(primaryMongoProperties()));
	}

	@Bean
	@Primary
	public MongoDbFactory primaryFactory(MongoProperties mongoProperties) throws Exception {
		return new SimpleMongoDbFactory(new MongoClientURI(primaryMongoProperties().getUri()));
	}
}

①.第二數據源配置(secondary)

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
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;

/**
 * @author litong
 * @date 2020/1/19 14:40
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.ltz.demo.secondary.dao",
		mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {

	@Bean
	@ConfigurationProperties(prefix="spring.data.mongodb.secondary")
	public MongoProperties secondaryMongoProperties() {
		return new MongoProperties();
	}

	@Bean(name = "secondaryMongoTemplate")
	public MongoTemplate secondaryMongoTemplate() throws Exception {
		return new MongoTemplate(secondaryFactory(secondaryMongoProperties()));
	}

	@Bean
	public MongoDbFactory secondaryFactory(MongoProperties mongoProperties) throws Exception {
		return new SimpleMongoDbFactory(new MongoClientURI(secondaryMongoProperties().getUri()));
	}
}

3.特別注意:

①.此處的primarysecondary只是示例,可自行修改名稱,但是記得要一併修改Bean的名稱。

②.com.ltz.demo.primary.daocom.ltz.demo.secondary.dao兩個包名,也可自行修改,然後在包下建立對應的DAO(Repository)。即可實現在該數據源下的JPA操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章