Preface
Spring dynamically configures multiple data sources, that is, segmenting data in large applications and using multiple database instances for management, which can effectively improve the horizontal scalability of the system. Such a solution will be different from the common single data instance solution, which requires the program to dynamically decide which database instance to store the data in and which database to extract the data from during runtime based on the current request and system status.
Spring2.x and later versions adopt the Proxy mode, which means we implement a virtual data source in the solution and use it to encapsulate the data source selection logic, so that the data source selection logic can be effectively transferred from separated from the Client. The Client provides the context required for selection (because this is what the Client knows), and the virtual DataSource implements the selection of the data source based on the context provided by the Client.
Implementation
The specific implementation is that the virtual DataSource only needs to inherit AbstractRoutingDataSource to implement determineCurrentLookupKey() in which the selection logic of the data source is encapsulated.
1. Dynamic configuration of multiple data sources
1. Data source name constant class:
/** * 动态配置多数据源 * 数据源的名称常量类 * @author LONGHUI_LUO * */ public class DataSourceConst { public static final String TEST="test"; public static final String USER="User"; }
2. Create a class to obtain and set the context environment, mainly responsible for changing the name of the context data source:
/** * 获得和设置上下文环境 主要负责改变上下文数据源的名称 * * @author LONGHUI_LUO * */ public class DataSourceContextHolder { private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境 // 设置数据源类型 public static void setDataSourceType(String dataSourceType) { contextHolder.set(dataSourceType); } // 获取数据源类型 public static String getDataSourceType() { return (String) contextHolder.get(); } // 清除数据源类型 public static void clearDataSourceType() { contextHolder.remove(); } }
3 . Create a dynamic data source class. Note that this class must inherit AbstractRoutingDataSource and implement the method determineCurrentLookupKey. This method returns an Object, usually a string:
/** * 建立动态数据源 * * @author LONGHUI_LUO * */ public class DynamicDataSource extends AbstractRoutingDataSource { protected Object determineCurrentLookupKey() { // 在进行DAO操作前,通过上下文环境变量,获得数据源的类型 return DataSourceContextHolder.getDataSourceType(); } }
4. Write spring configuration files to configure multiple data sources
<!-- 数据源相同的内容 --> <bean id="parentDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="username" value="sa" /> <property name="password" value="net2com" /> </bean> <!-- start以下配置各个数据源的特性 --> <bean parent="parentDataSource" id="testDataSource"> <propertynamepropertyname="url" value="jdbc:sqlserver://localhost:1433;databaseName=test" /> </bean> <bean parent="parentDataSource" id="UserDataSource"> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=User" /> </bean> <!-- end 配置各个数据源的特性 -->
5. Write spring configuration files to configure multiple data source mapping relationships
<bean class="com.xxxx.datasouce.DynamicDataSource" id="dataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="testDataSource" key="test"></entry> <entry value-ref="UserDataSource" key="User"></entry> </map> </property> <property name="defaultTargetDataSource" ref="testDataSource" ></property> </bean>
In this configuration, the first property attribute configures the target data source,
Dynamic switching is the data source
DataSourceContextHolder.setDataSourceType(DataSourceConst.TEST);