All Downloads are FREE. Search and download functionalities are using the official Maven repository.

love.keeping.starter.web.config.TenantConfiguration Maven / Gradle / Ivy

The newest version!
package love.keeping.starter.web.config;

import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import love.keeping.starter.web.utils.DataSourceUtil;
import love.keeping.starter.web.interceptors.TenantInterceptorImpl;
import love.keeping.starter.web.listeners.TenantListener.ClearTenantListener;
import love.keeping.starter.web.listeners.TenantListener.ReloadTenantListener;
import love.keeping.starter.web.listeners.TenantListener.SetTenantListener;
import love.keeping.starter.web.components.tenant.TenantInterceptor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "keeping.tenant", value = "enabled", matchIfMissing = false, havingValue = "true")
public class TenantConfiguration {

  @Autowired
  private DynamicDataSourceProperties dynamicDataSourceProperties;

  @Bean
  public TenantInterceptor tenantInterceptor() {
    return new TenantInterceptorImpl();
  }

  @Bean
  public ReloadTenantListener reloadTenantListener() {
    return new ReloadTenantListener();
  }

  @Bean
  public SetTenantListener setTenantListener() {
    return new SetTenantListener();
  }

  @Bean
  public ClearTenantListener clearTenantListener() {
    return new ClearTenantListener();
  }

  @Bean
  public AbstractJdbcDataSourceProvider tenantDataSourceProvider() {
    DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource()
        .get("master");
    return new AbstractJdbcDataSourceProvider(dataSourceProperty.getDriverClassName(),
        dataSourceProperty.getUrl(), dataSourceProperty.getUsername(),
        dataSourceProperty.getPassword()) {
      @Override
      protected Map executeStmt(Statement statement)
          throws SQLException {
        Map dataSourcePropertyMap = new HashMap<>();
        // 这里只加载启用的租户
        ResultSet rs = statement.executeQuery("select * from tenant where available = true");
        while (rs.next()) {
          String name = rs.getString("id");
          String username = rs.getString("jdbc_username");
          String password = rs.getString("jdbc_password");
          String url = rs.getString("jdbc_url");
          DataSourceProperty property = DataSourceUtil.createDataSourceProperty(dataSourceProperty,
              url, username, password);
          log.info("加载租户 {} 数据源 url {}", name, property.getUrl());
          dataSourcePropertyMap.put(name, property);
        }
        return dataSourcePropertyMap;
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy