spring boot配置和使用两个数据源
技术背景
在实际的开发场景中,一个spring boot应用可能需要连接多个数据库,比如主从数据库、不同业务模块使用不同数据库等。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. 创建数据源bean
在配置类中创建两个数据源的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() { // 使用主数据源执行操作 primaryjdbctemplate.execute("select 1 from dual"); // 使用从数据源执行操作 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() { // 使用主数据源执行操作 primaryjdbctemplate.execute("select 1 from dual"); // 使用从数据源执行操作 secondaryjdbctemplate.execute("select 1 from dual"); } }
最佳实践
- 标记主数据源:使用
@primary
注解标记一个主数据源,这样在自动注入时spring会优先使用该数据源。 - 事务管理:对于需要同时操作两个数据源的场景,使用
chainedtransactionmanager
进行事务管理。 - 代码隔离:将不同数据源的配置和使用代码进行隔离,提高代码的可维护性。
常见问题
1.jdbcurl is required with driverclassname错误
在spring boot 2.0之后,需要使用jdbc-url
代替url
。
2. 如何让不同的jparepository使用不同的数据源
可以通过配置不同的entitymanagerfactory
和transactionmanager
,并在@enablejparepositories
注解中指定对应的entitymanagerfactoryref
和transactionmanagerref
。
3. 分布式事务问题
如果需要在两个数据源之间进行分布式事务处理,可以考虑使用xa协议或分布式事务框架,如atomikos。
到此这篇关于spring boot配置和使用两个数据源的实现步骤的文章就介绍到这了,更多相关spring boot内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论