xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,118 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill åº„骞 (smallchill@163.com)
 */
package org.springblade.core.tenant.dynamic;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import lombok.AllArgsConstructor;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.Set;
import static org.springblade.core.tenant.constant.TenantBaseConstant.*;
/**
 * ç§Ÿæˆ·æ•°æ®æºæ ¸å¿ƒå¤„理类
 *
 * @author Chill
 */
@AllArgsConstructor
public class TenantDataSourceHolder {
   private final DataSource dataSource;
   private final DataSourceCreator dataSourceCreator;
   private final JdbcTemplate jdbcTemplate;
   /**
    * æ•°æ®æºç¼“存处理
    *
    * @param tenantId ç§Ÿæˆ·ID
    */
   public void handleDataSource(String tenantId) {
      // èŽ·å–å‚¨å­˜çš„æ•°æ®æºé›†åˆ
      DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
      Set<String> keys = ds.getCurrentDataSources().keySet();
      // é…ç½®ä¸å­˜åœ¨åˆ™åŠ¨æ€æ·»åŠ æ•°æ®æºï¼Œä»¥æ‡’åŠ è½½çš„æ¨¡å¼è§£å†³åˆ†å¸ƒå¼åœºæ™¯çš„é…ç½®åŒæ­¥
      // ä¸ºäº†ä¿è¯æ•°æ®å®Œæ•´æ€§ï¼Œé…ç½®åŽç”Ÿæˆæ•°æ®æºç¼“存,后台便无法修改更换数据源,若一定要修改请迁移数据后重启服务或自行修改底层逻辑
      if (!keys.contains(tenantId)) {
         TenantDataSource tenantDataSource = getDataSource(tenantId);
         if (tenantDataSource != null) {
            // åˆ›å»ºæ•°æ®æºé…ç½®
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            // æ‹·è´æ•°æ®æºé…ç½®
            BeanUtils.copyProperties(tenantDataSource, dataSourceProperty);
            // å…³é—­æ‡’加载
            dataSourceProperty.setLazy(Boolean.FALSE);
            // åˆ›å»ºåŠ¨æ€æ•°æ®æº
            DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
            // æ·»åŠ æœ€æ–°æ•°æ®æº
            ds.addDataSource(tenantId, dataSource);
         }
      }
   }
   /**
    * åˆ¤æ–­ç§Ÿæˆ·æ˜¯å¦æœ‰æ•°æ®æºé…ç½®
    *
    * @param tenantId ç§Ÿæˆ·ID
    */
   private Boolean existDataSource(String tenantId) {
      // å°†ç§Ÿæˆ·æ˜¯å¦é…ç½®æ•°æ®æºè¿›è¡Œç¼“存,若重新配置会将此缓存清空并在下次请求的时候懒加载
      // è‹¥ç§Ÿæˆ·æ²¡æœ‰é…ç½®æ•°æ®æºåˆ™ä¼šè‡ªåŠ¨ä½¿ç”¨master数据源,此举是为了避免在没有数据库的时候频繁查询导致缓存击穿
      Boolean exist = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, Boolean.class, Boolean.FALSE);
      if (exist == null) {
         TenantDataSource tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_EXIST_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
         if (tenantDataSource != null && StringUtil.isNotBlank(tenantDataSource.getDatasourceId())) {
            exist = Boolean.TRUE;
         } else {
            exist = Boolean.FALSE;
         }
         CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, exist, Boolean.FALSE);
      }
      return exist;
   }
   /**
    * èŽ·å–å¯¹åº”çš„æ•°æ®æºé…ç½®
    *
    * @param tenantId ç§Ÿæˆ·ID
    */
   private TenantDataSource getDataSource(String tenantId) {
      // ä¸å­˜åœ¨ç§Ÿæˆ·æ•°æ®æºåˆ™è¿”回空,防止缓存击穿
      if (!existDataSource(tenantId)) {
         return null;
      }
      // èŽ·å–ç§Ÿæˆ·æ•°æ®æºä¿¡æ¯
      TenantDataSource tenantDataSource = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, TenantDataSource.class, Boolean.FALSE);
      if (tenantDataSource == null) {
         tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_SINGLE_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
         if (tenantDataSource != null && StringUtil.isNoneBlank(tenantDataSource.getTenantId(), tenantDataSource.getDriverClass(), tenantDataSource.getUrl(), tenantDataSource.getUsername(), tenantDataSource.getPassword())) {
            CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, tenantDataSource, Boolean.FALSE);
         } else {
            tenantDataSource = null;
         }
      }
      return tenantDataSource;
   }
}