Dynamic datasource, multiple datasource of spring boot/ distribution transaction; spring boot 多数据源,动态数据源,分布式事务
Dynamic datasource, multiple datasource of spring boot/ distribution transaction; spring boot 多数据源,动态数据源,分布式事务
该项目使用了一个可写数据源和多个只读数据源,为了减少数据库压力,使用轮循的方式选择只读数据源;考虑到在一个 Service 中同时会有读和写的操作,所以本应用使用 AOP 切面通过 DAO 层的方法名切换只读数据源;但这种方式要求数据源主从一致,并且应当避免在同一个 Service 方法中写入后立即查询,如果必须在执行写入操作后立即读取,应当在 Service 方法上添加
@Transactional注解以保证使用主数据源
需要注意的是,使用 DAO 层切面后不应该在 Service 类层面上加
@Transactional注解,而应该添加在方法上,这也是 Spring 推荐的做法
动态切换数据源依赖
configuration包下的4个类来实现,分别是:
- DataSourceRoutingDataSource.java
- DataSourceConfigurer.java
- DynamicDataSourceContextHolder.java
- DynamicDataSourceAspect.java
dependencies {
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-aop')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}product_master, product_slave_alpha, product_slave_beta, product_slave_gammaproduct,并插入不同数据……package cn.com.hellowood.dynamicdatasource.common;
public enum DataSourceKey {
master,
slaveAlpha,
slaveBeta,
slaveGamma
}
该类继承自
AbstractRoutingDataSource类,在访问数据库时会调用该类的determineCurrentLookupKey()方法获取数据库实例的 key
package cn.com.hellowood.dynamicdatasource.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
protected Object determineCurrentLookupKey() {
logger.info("Current DataSource is [{}]", DynamicDataSourceContextHolder.getDataSourceKey());
return DynamicDataSourceContextHolder.getDataSourceKey();
}
}
数据源配置类,在该类中生成多个数据源实例并将其注入到
ApplicationContext中
…该类为数据源上下文配置,用于切换数据源
…动态数据源切换的切面,切 DAO 层,通过 DAO 层方法名判断使用哪个数据源,实现数据源切换 关于切面的 Order 可以不设,因为
@Transactional是最低的,取决于其他切面的设置,并且在org.springframework.core.annotation.AnnotationAwareOrderComparator会重新排序
……package cn.com.hellowood.dynamicdatasource.service;
import cn.com.hellowood.dynamicdatasource.mapper.ProductDao;
import cn.com.hellowood.dynamicdatasource.model.Product;
import cn.com.hellowood.dynamicdatasource.error.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductDao productDao;
public Product select(long productId) throws ServiceException {
Product product = productDao.select(productId);
if (product == null) {
throw new ServiceException("Product:" + productId + " not found");
}
return product;
}
@Transactional(rollbackFor = DataAccessException.class)
public Product update(long productId, Product newProduct) throws ServiceException {
if (productDao.update(newProduct) 启动项目,此时访问 `/product/1` 会返回 `product_master` 数据库中 `product` 表中的所有数据,多次访问 `/product` 会分别返回 `product_slave_alpha`、`product_slave_beta`、`product_slave_gamma` 数据库中 `product` 表中的数据,同时也可以在看到切换数据源的 log,说明动态切换数据源是有效的
---------------
## 注意
> 在该应用中因为使用了 DAO 层的切面切换数据源,所以 `@Transactional` 注解不能加在类上,只能用于方法;有 `@Trasactional`注解的方法无法切换数据源
## Stargazers over timeNo open issues yet, or sync has not completed.