add
This commit is contained in:
@ -51,38 +51,42 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
||||
"/**/*.html", "/**/*.css", "/**/*.js",
|
||||
// Knife4j
|
||||
"/swagger-resources", "/v3/api-docs/**", "/favicon.ico",
|
||||
// Druid 可视化web监控
|
||||
"/druid/**",
|
||||
// File
|
||||
BaseConstant.RESOURCE_PATTERN);
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
log.debug("Start run DynamicDataSourceFilter, Uri: {}", request.getRequestURI());
|
||||
String tenantId = request.getHeader(TenantConstant.HEADER_KEY_TENANT);
|
||||
// 主系统 or 登录入口获取租户列表
|
||||
if (TenantConstant.DEFAULT_TENANT_ID.equals(tenantId) || TenantConstant.LOGIN_TENANT_LIST_URI.equals(request.getRequestURI())) {
|
||||
dynamicDataSourceConfig.setDefaultSetting();
|
||||
filterChain.doFilter(request, response);
|
||||
after();
|
||||
return;
|
||||
}
|
||||
|
||||
// 不需要租户ID的接口
|
||||
for (String uri : NOT_NEED_TENANT_URIS) {
|
||||
if (UriUtil.match(uri, request.getRequestURI())) {
|
||||
try {
|
||||
log.debug("Start run DynamicDataSourceFilter, Uri: {}", request.getRequestURI());
|
||||
String tenantId = request.getHeader(TenantConstant.HEADER_KEY_TENANT);
|
||||
// 主系统 or 登录入口获取租户列表
|
||||
if (TenantConstant.DEFAULT_TENANT_ID.equals(tenantId) || TenantConstant.LOGIN_TENANT_LIST_URI.equals(request.getRequestURI())) {
|
||||
dynamicDataSourceConfig.setDefaultSetting();
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SysTenant sysTenant = sysTenantService.selectFromCache(tenantId);
|
||||
// 检查租户是否允许访问
|
||||
if (checkTenantIsNotAllow(response, sysTenant)) {
|
||||
return;
|
||||
// 不需要租户ID的接口
|
||||
for (String uri : NOT_NEED_TENANT_URIS) {
|
||||
if (UriUtil.match(uri, request.getRequestURI())) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SysTenant sysTenant = sysTenantService.selectFromCache(tenantId);
|
||||
// 检查租户是否允许访问
|
||||
if (checkTenantIsNotAllow(response, sysTenant)) {
|
||||
return;
|
||||
}
|
||||
// 设置租户信息
|
||||
before(sysTenant);
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
after();
|
||||
}
|
||||
// 设置租户信息
|
||||
before(sysTenant);
|
||||
filterChain.doFilter(request, response);
|
||||
after();
|
||||
}
|
||||
|
||||
private void before(SysTenant sysTenant) {
|
||||
@ -143,7 +147,6 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
||||
// 更新租户状态为已过期
|
||||
dynamicDataSourceConfig.setDefaultSetting();
|
||||
sysTenantService.updateStatus(sysTenant.getTenantId(), TenantStatusEnum.EXPIRE.getStatus());
|
||||
after();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -25,7 +25,7 @@ import java.sql.SQLException;
|
||||
@Service
|
||||
public class MysqlInitTablesStrategy implements InitTablesStrategy {
|
||||
|
||||
public static final String CREATE_MYSQL_DB_SQL = "DROP DATABASE IF EXISTS `{}`;CREATE DATABASE `{}` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';";
|
||||
public static final String CREATE_MYSQL_DB_SQL = "CREATE DATABASE `{}` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';";
|
||||
|
||||
@Override
|
||||
public TenantInitVo create(Connection conn, String schema) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.qiaoba.module.tenant.init.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.qiaoba.common.base.code.DatasourceErrorCode;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.database.utils.DbUtil;
|
||||
import com.qiaoba.module.tenant.entity.vo.TenantInitVo;
|
||||
import com.qiaoba.module.tenant.init.InitTablesStrategy;
|
||||
@ -10,6 +12,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 初始化表策略-Mysql
|
||||
@ -21,11 +24,22 @@ import java.sql.Connection;
|
||||
@Service
|
||||
public class PgSqlInitTablesStrategy implements InitTablesStrategy {
|
||||
|
||||
public static final String CREATE_MYSQL_DB_SQL = "DROP DATABASE IF EXISTS `{}`;CREATE DATABASE `{}` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';";
|
||||
private static final String CREATE_PGSQL_SCHEMA_SQL = "CREATE SCHEMA \"{}\";";
|
||||
|
||||
@Override
|
||||
public TenantInitVo create(Connection conn, String schema) {
|
||||
return null;
|
||||
try {
|
||||
// 新建模式
|
||||
DbUtil.runSql(conn, StrUtil.format(CREATE_PGSQL_SCHEMA_SQL, schema));
|
||||
// 切换模式
|
||||
DbUtil.setSchema(DataBaseEnum.POSTGRE_SQL.getType(), conn, schema);
|
||||
// 执行SQL
|
||||
return create(conn);
|
||||
} catch (SQLException e) {
|
||||
return new TenantInitVo(DatasourceErrorCode.CREATE_SCHEMA_ERROR.getCode(), e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -97,7 +97,7 @@ public class SysTenantDatasourceServiceImpl implements SysTenantDatasourceServic
|
||||
int result = updateById(datasource);
|
||||
// 刷新PrimaryDatasourceMap
|
||||
if (result > BaseConstant.HANDLE_ERROR) {
|
||||
|
||||
// todo
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.qiaoba.module.tenant.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.qiaoba.common.base.code.DatasourceErrorCode;
|
||||
@ -56,13 +57,27 @@ public class SysTenantInitServiceImpl implements SysTenantInitService {
|
||||
if (Objects.isNull(datasource)) {
|
||||
return new TenantInitCheckVo(sysTenant.getCompanyName(), sysTenant.getMode(), DatasourceErrorCode.NOT_FIND.getCode(), DatasourceErrorCode.NOT_FIND.getMsg());
|
||||
}
|
||||
// 5. 有数据源, 检查连接性
|
||||
JdbcUtil.CheckResult connectResult = JdbcUtil.check(DataBaseEnum.getDriver(datasource.getType()),
|
||||
DataBaseEnum.getUrl(datasource.getType(), datasource.getIp(), datasource.getPort(), datasource.getDbName(), datasource.getSchemaName()),
|
||||
datasource.getUsername(), datasource.getPassword());
|
||||
if (!connectResult.isResult()) {
|
||||
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = JdbcUtil.connection(DataBaseEnum.getDriver(datasource.getType()),
|
||||
DataBaseEnum.getUrl(datasource.getType(), datasource.getIp(), datasource.getPort(), datasource.getDbName(), datasource.getSchemaName()),
|
||||
datasource.getUsername(), datasource.getPassword());
|
||||
|
||||
// 检查 SCHEMA 是否存在
|
||||
if (StrUtil.isNotBlank(datasource.getSchemaName())) {
|
||||
Boolean schemaExist = DbUtil.checkSchema(datasource.getType(), connection, datasource.getSchemaName());
|
||||
if (!schemaExist) {
|
||||
// 6. 数据源无法连接, 返回
|
||||
return new TenantInitCheckVo(sysTenant.getCompanyName(), sysTenant.getMode(), datasource,
|
||||
DatasourceErrorCode.SCHEMA_NOT_EXIST_ERROR.getCode(), StrUtil.format(DatasourceErrorCode.SCHEMA_NOT_EXIST_ERROR.getMsg(), datasource.getSchemaName()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 6. 数据源无法连接, 返回
|
||||
return new TenantInitCheckVo(sysTenant.getCompanyName(), sysTenant.getMode(), datasource, DatasourceErrorCode.CONNECT_ERROR.getCode(), connectResult.getMsg());
|
||||
return new TenantInitCheckVo(sysTenant.getCompanyName(), sysTenant.getMode(), datasource, DatasourceErrorCode.CONNECT_ERROR.getCode(), e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(connection);
|
||||
}
|
||||
|
||||
// 7. 全部正常
|
||||
@ -86,7 +101,8 @@ public class SysTenantInitServiceImpl implements SysTenantInitService {
|
||||
try {
|
||||
// 获取主库Connection 和 Schema
|
||||
DruidDataSource dataSource = (DruidDataSource) PrimaryDatasourceContext.getDefault();
|
||||
connection = dataSource.getConnection();
|
||||
// 创建新的连接
|
||||
connection = JdbcUtil.getConnection(dataSource.getDriverClassName(), dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
|
||||
return InitTablesStrategyFactory.getStrategy(TenantDbTypeContext.getDefault()).create(connection, TenantSchema.getSchema(tenantId));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -112,19 +128,16 @@ public class SysTenantInitServiceImpl implements SysTenantInitService {
|
||||
if (TenantModeEnum.COLUMN.getMode().equals(sysTenant.getMode())) {
|
||||
// 获取默认租户的主数据源
|
||||
DataSource dataSource = (DataSource) PrimaryDatasourceContext.getDefault();
|
||||
// 获取默认租户的主数据库类型
|
||||
String dbType = TenantDbTypeContext.getDefault();
|
||||
// 初始化
|
||||
return init(dataSource.getConnection(), tenantId);
|
||||
}
|
||||
// SCHEMA
|
||||
else if (TenantModeEnum.SCHEMA.getMode().equals(sysTenant.getMode())) {
|
||||
// 获取默认租户的主数据源
|
||||
DataSource dataSource = (DataSource) PrimaryDatasourceContext.getDefault();
|
||||
// 获取默认租户的主数据库类型
|
||||
// 获取主库Connection 和 Schema
|
||||
DruidDataSource dataSource = (DruidDataSource) PrimaryDatasourceContext.getDefault();
|
||||
// 创建新的连接
|
||||
Connection connection = JdbcUtil.getConnection(dataSource.getDriverClassName(), dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
|
||||
String dbType = TenantDbTypeContext.getDefault();
|
||||
// 切换 SCHEMA
|
||||
Connection connection = dataSource.getConnection();
|
||||
DbUtil.setSchema(dbType, connection, TenantSchema.getSchema(tenantId));
|
||||
// 初始化
|
||||
return init(connection, tenantId);
|
||||
|
Reference in New Issue
Block a user