Spring Boot配置和使用兩個數據源的實現步驟
Spring Boot配置和使用兩個數據源
技術背景
在實際的開發(fā)場景中,一個Spring Boot應用可能需要連接多個數據庫,比如主從數據庫、不同業(yè)務模塊使用不同數據庫等。Spring Boot本身支持多數據源的配置,通過合理配置可以實現對多個數據源的管理和使用。
實現步驟
1. 配置數據源信息
在application.properties或application.yml中添加兩個數據源的配置信息。以application.properties為例:
# 第一個數據庫配置 spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver # 第二個數據庫配置 spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
2. 創(chuàng)建數據源Bean
在配置類中創(chuàng)建兩個數據源的Bean:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}3. 配置事務管理器(可選)
如果需要對兩個數據源進行事務管理,可以配置兩個事務管理器:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class TransactionManagerConfig {
@Bean(name="tm1")
@Autowired
@Primary
DataSourceTransactionManager tm1(@Qualifier ("primaryDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
@Bean(name="tm2")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("secondaryDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
}4. 使用不同的數據源
在需要使用數據源的地方,通過@Qualifier注解指定使用哪個數據源:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class MyService {
private final JdbcTemplate primaryJdbcTemplate;
private final JdbcTemplate secondaryJdbcTemplate;
@Autowired
public MyService(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.primaryJdbcTemplate = new JdbcTemplate(primaryDataSource);
this.secondaryJdbcTemplate = new JdbcTemplate(secondaryDataSource);
}
public void doSomething() {
// 使用主數據源執(zhí)行操作
primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
// 使用從數據源執(zhí)行操作
secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
}
}核心代碼
數據源配置
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}事務管理器配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class TransactionManagerConfig {
@Bean(name="tm1")
@Autowired
@Primary
DataSourceTransactionManager tm1(@Qualifier ("primaryDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
@Bean(name="tm2")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("secondaryDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
}服務層使用數據源
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class MyService {
private final JdbcTemplate primaryJdbcTemplate;
private final JdbcTemplate secondaryJdbcTemplate;
@Autowired
public MyService(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.primaryJdbcTemplate = new JdbcTemplate(primaryDataSource);
this.secondaryJdbcTemplate = new JdbcTemplate(secondaryDataSource);
}
public void doSomething() {
// 使用主數據源執(zhí)行操作
primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
// 使用從數據源執(zhí)行操作
secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
}
}最佳實踐
- 標記主數據源:使用
@Primary注解標記一個主數據源,這樣在自動注入時Spring會優(yōu)先使用該數據源。 - 事務管理:對于需要同時操作兩個數據源的場景,使用
ChainedTransactionManager進行事務管理。 - 代碼隔離:將不同數據源的配置和使用代碼進行隔離,提高代碼的可維護性。
常見問題
1.jdbcUrl is required with driverClassName錯誤
在Spring Boot 2.0之后,需要使用jdbc-url代替url。
2. 如何讓不同的JpaRepository使用不同的數據源
可以通過配置不同的EntityManagerFactory和TransactionManager,并在@EnableJpaRepositories注解中指定對應的entityManagerFactoryRef和transactionManagerRef。
3. 分布式事務問題
如果需要在兩個數據源之間進行分布式事務處理,可以考慮使用XA協(xié)議或分布式事務框架,如Atomikos。
到此這篇關于Spring Boot配置和使用兩個數據源的實現步驟的文章就介紹到這了,更多相關Spring Boot內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解SpringBoot 快速整合Mybatis(去XML化+注解進階)
本篇文章主要介紹了詳解SpringBoot 快速整合Mybatis(去XML化+注解進階),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

