最近在作SaaS應用,數據庫採用了單實例多schema的架構(詳見參考資料1),每一個租戶有一個獨立的schema,同時整個數據源有一個共享的schema,所以須要解決動態增刪、切換數據源的問題。前端
在網上搜了不少文章後,不少都是講主從數據源配置,或都是在應用啓動前已經肯定好數據源配置的,甚少講在不停機的狀況如何動態加載數據源,因此寫下這篇文章,以供參考。java
當一個請求進來的時候,判斷當前用戶所屬租戶,並根據租戶信息切換至相應數據源,而後進行後續的業務操做。spring
@EqualsAndHashCode(callSuper = false) @Data @FieldDefaults(level = AccessLevel.PRIVATE) public class TenantConfigEntity { /** * 租戶id **/ Integer tenantId; /** * 租戶名稱 **/ String tenantName; /** * 租戶名稱key **/ String tenantKey; /** * 數據庫url **/ String dbUrl; /** * 數據庫用戶名 **/ String dbUser; /** * 數據庫密碼 **/ String dbPassword; /** * 數據庫public_key **/ String dbPublicKey; }
public class DataSourceUtil { private static final String DATA_SOURCE_BEAN_KEY_SUFFIX = "_data_source"; private static final String JDBC_URL_ARGS = "?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&zeroDateTimeBehavior=convertToNull"; private static final String CONNECTION_PROPERTIES = "config.decrypt=true;config.decrypt.key="; /** * 拼接數據源的spring bean key */ public static String getDataSourceBeanKey(String tenantKey) { if (!StringUtils.hasText(tenantKey)) { return null; } return tenantKey + DATA_SOURCE_BEAN_KEY_SUFFIX; } /** * 拼接完整的JDBC URL */ public static String getJDBCUrl(String baseUrl) { if (!StringUtils.hasText(baseUrl)) { return null; } return baseUrl + JDBC_URL_ARGS; } /** * 拼接完整的Druid鏈接屬性 */ public static String getConnectionProperties(String publicKey) { if (!StringUtils.hasText(publicKey)) { return null; } return CONNECTION_PROPERTIES + publicKey; } }
使用ThreadLocal保存當前線程的數據源key name,並實現set、get、clear方法;sql
public class DataSourceContextHolder { private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<>(); public static void setDataSourceKey(String tenantKey) { dataSourceKey.set(tenantKey); } public static String getDataSourceKey() { return dataSourceKey.get(); } public static void clearDataSourceKey() { dataSourceKey.remove(); } }
繼承AbstractRoutingDataSource(建議閱讀其源碼,瞭解動態切換數據源的過程),實現動態選擇數據源;數據庫
public class DynamicDataSource extends AbstractRoutingDataSource { @Autowired private ApplicationContext applicationContext; @Lazy @Autowired private DynamicDataSourceSummoner summoner; @Lazy @Autowired private TenantConfigDAO tenantConfigDAO; @Override protected String determineCurrentLookupKey() { String tenantKey = DataSourceContextHolder.getDataSourceKey(); return DataSourceUtil.getDataSourceBeanKey(tenantKey); } @Override protected DataSource determineTargetDataSource() { String tenantKey = DataSourceContextHolder.getDataSourceKey(); String beanKey = DataSourceUtil.getDataSourceBeanKey(tenantKey); if (!StringUtils.hasText(tenantKey) || applicationContext.containsBean(beanKey)) { return super.determineTargetDataSource(); } if (tenantConfigDAO.exist(tenantKey)) { summoner.registerDynamicDataSources(); } return super.determineTargetDataSource(); } }
從數據庫加載數據源信息,並動態組裝和註冊spring bean,安全
@Slf4j @Component public class DynamicDataSourceSummoner implements ApplicationListener<ContextRefreshedEvent> { // 跟spring-data-source.xml的默認數據源id保持一致 private static final String DEFAULT_DATA_SOURCE_BEAN_KEY = "defaultDataSource"; @Autowired private ConfigurableApplicationContext applicationContext; @Autowired private DynamicDataSource dynamicDataSource; @Autowired private TenantConfigDAO tenantConfigDAO; private static boolean loaded = false; /** * Spring加載完成後執行 */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 防止重複執行 if (!loaded) { loaded = true; try { registerDynamicDataSources(); } catch (Exception e) { log.error("數據源初始化失敗, Exception:", e); } } } /** * 從數據庫讀取租戶的DB配置,並動態注入Spring容器 */ public void registerDynamicDataSources() { // 獲取全部租戶的DB配置 List<TenantConfigEntity> tenantConfigEntities = tenantConfigDAO.listAll(); if (CollectionUtils.isEmpty(tenantConfigEntities)) { throw new IllegalStateException("應用程序初始化失敗,請先配置數據源"); } // 把數據源bean註冊到容器中 addDataSourceBeans(tenantConfigEntities); } /** * 根據DataSource建立bean並註冊到容器中 */ private void addDataSourceBeans(List<TenantConfigEntity> tenantConfigEntities) { Map<Object, Object> targetDataSources = Maps.newLinkedHashMap(); DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); for (TenantConfigEntity entity : tenantConfigEntities) { String beanKey = DataSourceUtil.getDataSourceBeanKey(entity.getTenantKey()); // 若是該數據源已經在spring裏面註冊過,則不從新註冊 if (applicationContext.containsBean(beanKey)) { DruidDataSource existsDataSource = applicationContext.getBean(beanKey, DruidDataSource.class); if (isSameDataSource(existsDataSource, entity)) { continue; } } // 組裝bean AbstractBeanDefinition beanDefinition = getBeanDefinition(entity, beanKey); // 註冊bean beanFactory.registerBeanDefinition(beanKey, beanDefinition); // 放入map中,注意必定是剛纔建立bean對象 targetDataSources.put(beanKey, applicationContext.getBean(beanKey)); } // 將建立的map對象set到 targetDataSources; dynamicDataSource.setTargetDataSources(targetDataSources); // 必須執行此操做,纔會從新初始化AbstractRoutingDataSource 中的 resolvedDataSources,也只有這樣,動態切換纔會起效 dynamicDataSource.afterPropertiesSet(); } /** * 組裝數據源spring bean */ private AbstractBeanDefinition getBeanDefinition(TenantConfigEntity entity, String beanKey) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DruidDataSource.class); builder.getBeanDefinition().setAttribute("id", beanKey); // 其餘配置繼承defaultDataSource builder.setParentName(DEFAULT_DATA_SOURCE_BEAN_KEY); builder.setInitMethodName("init"); builder.setDestroyMethodName("close"); builder.addPropertyValue("name", beanKey); builder.addPropertyValue("url", DataSourceUtil.getJDBCUrl(entity.getDbUrl())); builder.addPropertyValue("username", entity.getDbUser()); builder.addPropertyValue("password", entity.getDbPassword()); builder.addPropertyValue("connectionProperties", DataSourceUtil.getConnectionProperties(entity.getDbPublicKey())); return builder.getBeanDefinition(); } /** * 判斷Spring容器裏面的DataSource與數據庫的DataSource信息是否一致 * 備註:這裏沒有判斷public_key,由於另外三個信息基本能夠肯定惟一了 */ private boolean isSameDataSource(DruidDataSource existsDataSource, TenantConfigEntity entity) { boolean sameUrl = Objects.equals(existsDataSource.getUrl(), DataSourceUtil.getJDBCUrl(entity.getDbUrl())); if (!sameUrl) { return false; } boolean sameUser = Objects.equals(existsDataSource.getUsername(), entity.getDbUser()); if (!sameUser) { return false; } try { String decryptPassword = ConfigTools.decrypt(entity.getDbPublicKey(), entity.getDbPassword()); return Objects.equals(existsDataSource.getPassword(), decryptPassword); } catch (Exception e) { log.error("數據源密碼校驗失敗,Exception:{}", e); return false; } } }
<!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:data.properties" ignore-unresolvable="true"/> <!-- 公共(默認)數據源 --> <bean id="defaultDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="url" value="${ds.jdbcUrl}" /> <property name="username" value="${ds.user}" /> <property name="password" value="${ds.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="5" /> <property name="minIdle" value="2" /> <property name="maxActive" value="10" /> <!-- 配置獲取鏈接等待超時的時間,單位是毫秒 --> <property name="maxWait" value="1000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="5000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="240000" /> <property name="validationQuery" value="SELECT 1" /> <!--單位:秒,檢測鏈接是否有效的超時時間--> <property name="validationQueryTimeout" value="60" /> <!--建議配置爲true,不影響性能,而且保證安全性。申請鏈接的時候檢測,若是空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測鏈接是否有效--> <property name="testWhileIdle" value="true" /> <!--申請鏈接時執行validationQuery檢測鏈接是否有效,作了這個配置會下降性能。--> <property name="testOnBorrow" value="true" /> <!--歸還鏈接時執行validationQuery檢測鏈接是否有效,作了這個配置會下降性能。--> <property name="testOnReturn" value="false" /> <!--Config Filter--> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${ds.publickey}" /> </bean> <!-- 事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="multipleDataSource"/> </bean> <!--多數據源--> <bean id="multipleDataSource" class="a.b.c.DynamicDataSource"> <property name="defaultTargetDataSource" ref="defaultDataSource"/> <property name="targetDataSources"> <map> <entry key="defaultDataSource" value-ref="defaultDataSource"/> </map> </property> </bean> <!-- 註解事務管理器 --> <!--這裏的order值必須大於DynamicDataSourceAspectAdvice的order值--> <tx:annotation-driven transaction-manager="txManager" order="2"/> <!-- 建立SqlSessionFactory,同時指定數據源 --> <bean id="mainSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource"/> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的DAO --> <bean id="mainSqlMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="mainSqlSessionFactory"/> <property name="basePackage" value="a.b.c.*.dao"/> </bean> <bean id="defaultSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="defaultDataSource"/> </bean> <bean id="defaultSqlMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="defaultSqlSessionFactory"/> <property name="basePackage" value="a.b.c.base.dal.dao"/> </bean> <!-- 其餘配置省略 -->
利用AOP自動切換數據源,僅供參考;mybatis
@Slf4j @Aspect @Component @Order(1) // 請注意:這裏order必定要小於tx:annotation-driven的order,即先執行DynamicDataSourceAspectAdvice切面,再執行事務切面,才能獲取到最終的數據源 @EnableAspectJAutoProxy(proxyTargetClass = true) public class DynamicDataSourceAspectAdvice { @Around("execution(* a.b.c.*.controller.*.*(..))") public Object doAround(ProceedingJoinPoint jp) throws Throwable { ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = sra.getRequest(); HttpServletResponse response = sra.getResponse(); String tenantKey = request.getHeader("tenant"); // 前端必須傳入tenant header, 不然返回400 if (!StringUtils.hasText(tenantKey)) { WebUtils.toHttp(response).sendError(HttpServletResponse.SC_BAD_REQUEST); return null; } log.info("當前租戶key:{}", tenantKey); DataSourceContextHolder.setDataSourceKey(tenantKey); Object result = jp.proceed(); DataSourceContextHolder.clearDataSourceKey(); return result; } }
另外,部分信息(例如:租戶配置、省市縣等共用信息)是存放在公共schema裏面的,也就是說一個請求裏面有可能包括查詢當前租戶數據源和公共數據源,建議爲公共數據源單首創建一個sqlSessionFactory
和sqlMapper
(分別對應xml配置裏的defaultSqlSessionFactory
和defaultSqlMapper
),或乾脆把公共數據源的數據單獨作成一個微服務,之後水平擴展也方便。架構