网上多是基于XML文件,本文使用基于配置类的方式使用动态数据源。
多数据源原理
Spring作为项目的应用容器,也对多数据源提供了很好的支持,当我们的持久化框架需要数据库连接时,我们需要做到动态的切换数据源,这些Spring的AbstractRoutingDataSource都给我们留了拓展的空间,可以先来看看抽象类AbstractRoutingDataSource在获取数据库连接时做了什么。
1//从配置文件读取到的DataSources的Map 2private Map<Object, DataSource> resolvedDataSources; 3 4//默认数据源 5private DataSource resolvedDefaultDataSource; 6 7public Connection getConnection() throws SQLException { 8 return determineTargetDataSource().getConnection(); 9} 10 11public Connection getConnection(String username, String password) throws SQLException { 12 return determineTargetDataSource().getConnection(username, password); 13} 14 15protected DataSource determineTargetDataSource() { 16 Assert.notNull(this.resolvedDataSources, "DataSource router not initialized"); 17 Object lookupKey = determineCurrentLookupKey(); 18 DataSource dataSource = this.resolvedDataSources.get(lookupKey); 19 if (dataSource == null && (this.lenientFallback || lookupKey == null)) { 20 dataSource = this.resolvedDefaultDataSource; 21 } 22 if (dataSource == null) { 23 throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]"); 24 } 25 return dataSource; 26} 27 28protected abstract Object determineCurrentLookupKey();
可以看到AbstractRoutingDataSource在决定目标数据源的时候,会先调用determineCurrentLookupKey()方法得到一个key,我们通过这个key从配置好的resolvedDataSources(Map结构)拿到这次调用对应的数据源,而determineCurrentLookupKey()开放出来让我们实现。
简单实现AbstractRoutingDataSource
基于上述分析,继承抽象类AbstractRoutingDataSource,并实现determineCurrentLookupKey()方法。
1public class MyDataSource extends AbstractRoutingDataSource { 2 3 private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<String>(); 4 5 public static void setDataSourceKey(String dataSource) { 6 dataSourceKey.set(dataSource); 7 } 8 9 protected Object determineCurrentLookupKey() { 10 String dsName = dataSourceKey.get(); 11 //这里需要注意的时,每次我们返回当前数据源的值得时候都需要移除ThreadLocal的值, 12 //这是为了避免同一线程上一次方法调用对之后调用的影响 13 dataSourceKey.remove(); 14 return dsName; 15 } 16 17} 18
实际项目中与mybatis的结合
- 配置AbstractRoutingDataSource
- 使用AbstractRoutingDataSource作为SqlSessionFactory的数据源
- 在使用具体的dao层的相关方法前,设置指定的datasource,动态切换数据源
- 执行dao方法的时候就会使用该数据源执行。 ### 配置datasource 说明:默认使用druid连接池。
在application.yml中配置Spring数据库连接相关属性。
Springboot 默认会自动加载classpath下的
application.yml和application.properties文件。
问题:当两个文件同时使用,会同时加载吗?出现冲突会优先选择哪个文件的?
两种文件同时使用时都会加载,如果两文件出现相同的属性名,则application.properties中的会为最终值(测试过。)。
多数据源配置:
1datasource: 2 type: com.alibaba.druid.pool.DruidDataSource.class 3 write: 4 name: pushopt 5 url: jdbc:mysql://127.0.0.1:3306/pushopt 6 username: root 7 password: hgfgood 8 driver-class-name: com.mysql.jdbc.Driver 9 max-active: 20 10 initial-size: 1 11 max-wait: 6000 12 pool-prepared-statements: true 13 max-open-prepared-statements: 20 14 read1: 15 name: test 16 url: jdbc:mysql://127.0.0.1:3306/test 17 username: root 18 password: hgfgood 19 driver-class-name: com.mysql.jdbc.Driver 20 max-active: 20 21 initial-size: 1 22 max-wait: 6000 23 pool-prepared-statements: true 24 max-open-prepared-statements: 20
生成DataSource Bean:
1package com.meituan.service.web.opt.config; 2 3import com.meituan.service.web.opt.enums.DataSourceType; 4import org.apache.ibatis.session.SqlSessionFactory; 5import org.mybatis.spring.SqlSessionFactoryBean; 6import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 7import org.springframework.boot.context.properties.ConfigurationProperties; 8import org.springframework.context.annotation.Bean; 9import org.springframework.context.annotation.Configuration; 10import org.springframework.context.annotation.DependsOn; 11import org.springframework.context.annotation.Primary; 12import org.springframework.core.io.ClassPathResource; 13import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 16import javax.sql.DataSource; 17import java.util.HashMap; 18import java.util.Map; 19 20/** 21 * Created by hgf on 16/7/29. 22 */ 23@Configuration 24public class DataSourceConfiguration { 25 26 private Class<? extends DataSource> datasourceType = com.alibaba.druid.pool.DruidDataSource.class; 27 28 @Bean(name = "writeDataSource") 29 @ConfigurationProperties(prefix = "datasource.write") 30 public DataSource writeDataSource() { 31 return DataSourceBuilder.create().type(datasourceType).build(); 32 } 33 34 @Bean(name = "readDataSource1") 35 @ConfigurationProperties(prefix = "datasource.read1") 36 public DataSource readDataSource1() { 37 return DataSourceBuilder.create().type(datasourceType).build(); 38 } 39 40 /** 41 * 有多少个数据源就要配置多少个bean 42 * 43 * @return 44 */ 45 @Bean 46 @Primary 47 @DependsOn({"writeDataSource", "readDataSource1"}) 48 public DynamicDataSource dynamicDataSource() { 49 DynamicDataSource proxy = new DynamicDataSource(); 50 51 //表示可用的数据源,包括写和读数据源 52 Map<Object, Object> targetDataSources = new HashMap<Object, Object>(); 53 // 写 54 targetDataSources.put(DataSourceType.WRITE.getType(), writeDataSource()); 55 56 //如果有多个DataSource,需要设置多个 57 targetDataSources.put(DataSourceType.READ.getType(), readDataSource1()); 58 59 //设置默认的数据源为写数据源 60 proxy.setDefaultTargetDataSource(writeDataSource()); 61 proxy.setTargetDataSources(targetDataSources); 62 63 return proxy; 64 } 65 66 @Bean 67 public SqlSessionFactory sqlSessionFactorys() throws Exception { 68 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 69 sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); 70 sqlSessionFactoryBean.setDataSource(dynamicDataSource()); 71 sqlSessionFactoryBean.setTypeAliasesPackage("com.meituan.service.web.opt.model"); 72 73 PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver(); 74 sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources("classpath:/mapper/*.xml")); 75 76 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject(); 77 return sqlSessionFactory; 78 } 79 80 @Bean 81 DataSourceTransactionManager dataSourceTransactionManager() { 82 DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dynamicDataSource()); 83 return dataSourceTransactionManager; 84 } 85 86} 87
注:此处有两个坑,其一是配置DataSource的时候有坑,在实例化
AbstractRoutingDataSource的时候不要在属性上设置@Autowired注入,直接使用属性注入或者调用Bean的配置函数否则会产生循环依赖。
正确使用方法:1 @Bean(name = "dynamicDataSource") 2 public AbstractRoutingDataSource dynamicDataSource(@Qualifier("writeDataSource") DataSource writeDataSource, @Qualifier("readDataSource1") DataSource readDataSource1) { 3 //self defined AbstractRoutingDataSource 4 DynamicDataSource proxy = new DynamicDataSource(); 5 ... 6 return proxy; 7 }错误方法:
1 @Autowired 2 @Qualifier("writeDataSource") 3 DataSource writeDataSource; 4 @Autowired 5 @Qualifier("readDataSource1") 6 DataSource readDataSource1; 7 @Bean(name = "dynamicDataSource") 8 public AbstractRoutingDataSource dynamicDataSource() { 9 DynamicDataSource proxy = new DynamicDataSource(); 10 ... 11 return proxy; 12 }错误异常:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'mybatisConfiguration': Unsatisfied dependency expressed through field 'writeDataSource':
Error creating bean with name 'writeDataSource' defined in class path resource [com/meituan/service/web/opt/config/DataSourceConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dynamicDataSource' defined in class path resource [com/meituan/service/web/opt/config/MybatisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource]: Circular reference involving containing bean 'mybatisConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'dynamicDataSource' threw exception; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'writeDataSource' defined in class path resource [com/meituan/service/web/opt/config/DataSourceConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dynamicDataSource' defined in class path resource [com/meituan/service/web/opt/config/MybatisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource]: Circular reference involving containing bean 'mybatisConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'dynamicDataSource' threw exception; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null主要意思就是:
mybatisConfigurationbean需要先注入writeDataSourceBean,该Bean依赖dataSourceInitializer,而dataSourceInitializer依赖dynamicDataSource,此时dynamicDataSource由于依赖writeDataSource而没有初始化,所以依赖注入writeDataSource此时没有正确的生成bean,而是null。所以造成初始化Bean失败。其二就是需要自定义Bean加载顺序。由于DataSource使用
DataSourceBuilder创建,该类依赖datasource实例,所以容易产生循环依赖,特别是在先加载DynamicDataSource,的同时加载writeDataSource时。解决方法:使用Spring提供的@DependsOn注解,注解DynamicDataSource。当加载DynamicDataSource,会等待加载writeDataSource,等writeDataSource加载完成后,再加载DynamicDataSource。就不会出现DynamicDataSource->DatasourceInitlizer->writeDataSource->DataSourceInitlizer循环依赖了。注: 当spring容器中有多个datasource时,使用
[@Primary](https://my.oschina.net/primary)决定当有同类别的beans时,如何选择注入那个类。
多数据源集成mybatis
生成AbstractRoutingDataSource的Bean后,使用该Bean配置SqlSessionFactory,就能使动态数据源生效。
注:如果手动配置
SqlSessionFactoryBean,那么Spring boot默认会从Ioc容器中选择一个(一般是最先生成的Datasource Bean)DataSource注入到默认的自动加载的SqlSessionFactory中,此时动态数据源不能生效。
自定义SqlSessionFactory,使用SqlSessionFactoryBean来生成SqlSessionFactory:
1 @Bean 2 public SqlSessionFactory sqlSessionFactorys(AbstractRoutingDataSource dynamicDataSource) throws Exception { 3 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 4 //设置mybatis的配置文件路径 5 sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); 6 //设置数据源为动态数据源 7 sqlSessionFactoryBean.setDataSource(dynamicDataSource); 8 //设置类型前缀包名,在mapper文件中就不用使用详细的包名了,直接使用类名。 9 sqlSessionFactoryBean.setTypeAliasesPackage("com.meituan.service.web.opt.model"); 10 11 //配置路径匹配器,获取匹配的文件 12 PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver(); 13 sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources("classpath:/mapper/*.xml")); 14 15 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject(); 16 return sqlSessionFactory; 17 }