Spring Boot配置和使用兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)步驟
Spring Boot配置和使用兩個(gè)數(shù)據(jù)源
技術(shù)背景
在實(shí)際的開發(fā)場(chǎng)景中,一個(gè)Spring Boot應(yīng)用可能需要連接多個(gè)數(shù)據(jù)庫,比如主從數(shù)據(jù)庫、不同業(yè)務(wù)模塊使用不同數(shù)據(jù)庫等。Spring Boot本身支持多數(shù)據(jù)源的配置,通過合理配置可以實(shí)現(xiàn)對(duì)多個(gè)數(shù)據(jù)源的管理和使用。
實(shí)現(xiàn)步驟
1. 配置數(shù)據(jù)源信息
在application.properties或application.yml中添加兩個(gè)數(shù)據(jù)源的配置信息。以application.properties為例:
# 第一個(gè)數(shù)據(jù)庫配置 spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver # 第二個(gè)數(shù)據(jù)庫配置 spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
2. 創(chuàng)建數(shù)據(jù)源Bean
在配置類中創(chuàng)建兩個(gè)數(shù)據(jù)源的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. 配置事務(wù)管理器(可選)
如果需要對(duì)兩個(gè)數(shù)據(jù)源進(jìn)行事務(wù)管理,可以配置兩個(gè)事務(wù)管理器:
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. 使用不同的數(shù)據(jù)源
在需要使用數(shù)據(jù)源的地方,通過@Qualifier注解指定使用哪個(gè)數(shù)據(jù)源:
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() {
// 使用主數(shù)據(jù)源執(zhí)行操作
primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
// 使用從數(shù)據(jù)源執(zhí)行操作
secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
}
}核心代碼
數(shù)據(jù)源配置
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();
}
}事務(wù)管理器配置
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;
}
}服務(wù)層使用數(shù)據(jù)源
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() {
// 使用主數(shù)據(jù)源執(zhí)行操作
primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
// 使用從數(shù)據(jù)源執(zhí)行操作
secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
}
}最佳實(shí)踐
- 標(biāo)記主數(shù)據(jù)源:使用
@Primary注解標(biāo)記一個(gè)主數(shù)據(jù)源,這樣在自動(dòng)注入時(shí)Spring會(huì)優(yōu)先使用該數(shù)據(jù)源。 - 事務(wù)管理:對(duì)于需要同時(shí)操作兩個(gè)數(shù)據(jù)源的場(chǎng)景,使用
ChainedTransactionManager進(jìn)行事務(wù)管理。 - 代碼隔離:將不同數(shù)據(jù)源的配置和使用代碼進(jìn)行隔離,提高代碼的可維護(hù)性。
常見問題
1.jdbcUrl is required with driverClassName錯(cuò)誤
在Spring Boot 2.0之后,需要使用jdbc-url代替url。
2. 如何讓不同的JpaRepository使用不同的數(shù)據(jù)源
可以通過配置不同的EntityManagerFactory和TransactionManager,并在@EnableJpaRepositories注解中指定對(duì)應(yīng)的entityManagerFactoryRef和transactionManagerRef。
3. 分布式事務(wù)問題
如果需要在兩個(gè)數(shù)據(jù)源之間進(jìn)行分布式事務(wù)處理,可以考慮使用XA協(xié)議或分布式事務(wù)框架,如Atomikos。
到此這篇關(guān)于Spring Boot配置和使用兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Spring Boot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter的操作步驟
- 使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題
- 使用springboot+druid雙數(shù)據(jù)源動(dòng)態(tài)配置操作
- 教你使用springboot配置多數(shù)據(jù)源
- springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)
- SpringBoot使用Nacos動(dòng)態(tài)配置數(shù)據(jù)源的方法
- SpringBoot使用Druid數(shù)據(jù)源的配置方法
相關(guān)文章
SpringBoot+Vue前后端分離實(shí)現(xiàn)審核功能的示例
在實(shí)際開發(fā)中,審核功能是一個(gè)非常常用的功能,本文就來介紹一下使用SpringBoot+Vue前后端分離實(shí)現(xiàn)審核功能的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
java如何實(shí)現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個(gè)節(jié)點(diǎn)
文章講述了在Java中實(shí)現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個(gè)節(jié)點(diǎn)時(shí)需要注意的三個(gè)點(diǎn),包括刪除的節(jié)點(diǎn)包含子節(jié)點(diǎn)、刪除子節(jié)點(diǎn)和其他子節(jié)點(diǎn)刪除的節(jié)點(diǎn)不包含子節(jié)點(diǎn)、以及該父節(jié)點(diǎn)變成葉子節(jié)點(diǎn),此外,文章還提到這兩件事包含在同一件事務(wù)中2024-12-12
解讀SpringBoot中addCorsMappings配置跨域與攔截器互斥問題的原因
這篇文章主要介紹了解讀SpringBoot中addCorsMappings配置跨域與攔截器互斥問題的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring MVC+MyBatis+MySQL實(shí)現(xiàn)分頁功能實(shí)例
分頁功能是我們?nèi)粘i_發(fā)中經(jīng)常會(huì)遇到的,下面這篇文章主要給大家介紹了Spring MVC+MyBatis+MySQL實(shí)現(xiàn)分頁功能的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06

