refactor(extension/tenant): 多租户组件适配动态隔离级别 (#8)

This commit is contained in:
小熊
2024-12-20 10:44:09 +08:00
committed by GitHub
parent 7c3f15a6f6
commit c089df66cf
20 changed files with 307 additions and 153 deletions

View File

@@ -16,11 +16,11 @@
package top.continew.starter.extension.tenant.autoconfigure;
import cn.hutool.core.convert.Convert;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import top.continew.starter.extension.tenant.context.TenantContext;
import top.continew.starter.extension.tenant.config.TenantProvider;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
/**
@@ -29,20 +29,25 @@ import top.continew.starter.extension.tenant.context.TenantContextHolder;
* @author Charles7c
* @since 2.7.0
*/
public class TenantInterceptor implements HandlerInterceptor {
public class TenantInterceptor implements HandlerInterceptor, Ordered {
private final TenantProperties tenantProperties;
private final TenantProvider tenantProvider;
public TenantInterceptor(TenantProperties tenantProperties) {
public TenantInterceptor(TenantProperties tenantProperties, TenantProvider tenantProvider) {
this.tenantProperties = tenantProperties;
this.tenantProvider = tenantProvider;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String tenantId = request.getHeader(tenantProperties.getTenantIdHeader());
TenantContext tenantContext = new TenantContext();
tenantContext.setTenantId(Convert.toLong(tenantId));
TenantContextHolder.setContext(tenantContext);
TenantContextHolder.setContext(tenantProvider.getByTenantId(tenantId, true));
return true;
}
@Override
public int getOrder() {
return Integer.MIN_VALUE;
}
}

View File

@@ -18,7 +18,6 @@ package top.continew.starter.extension.tenant.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
import java.util.List;
@@ -36,11 +35,6 @@ public class TenantProperties {
*/
private boolean enabled = true;
/**
* 租户隔离级别
*/
private TenantIsolationLevel isolationLevel = TenantIsolationLevel.LINE;
/**
* 租户 ID 列名
*/
@@ -69,14 +63,6 @@ public class TenantProperties {
this.enabled = enabled;
}
public TenantIsolationLevel getIsolationLevel() {
return isolationLevel;
}
public void setIsolationLevel(TenantIsolationLevel isolationLevel) {
this.isolationLevel = isolationLevel;
}
public String getTenantIdColumn() {
return tenantIdColumn;
}

View File

@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.extension.tenant.config.TenantProvider;
/**
* 多租户 Web MVC 自动配置
@@ -35,13 +36,15 @@ import top.continew.starter.core.constant.PropertiesConstants;
public class TenantWebMvcAutoConfiguration implements WebMvcConfigurer {
private final TenantProperties tenantProperties;
private final TenantProvider tenantProvider;
public TenantWebMvcAutoConfiguration(TenantProperties tenantProperties) {
public TenantWebMvcAutoConfiguration(TenantProperties tenantProperties, TenantProvider tenantProvider) {
this.tenantProperties = tenantProperties;
this.tenantProvider = tenantProvider;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TenantInterceptor(tenantProperties));
registry.addInterceptor(new TenantInterceptor(tenantProperties, tenantProvider));
}
}

View File

@@ -16,19 +16,22 @@
package top.continew.starter.extension.tenant.config;
import top.continew.starter.extension.tenant.context.TenantContext;
/**
* 租户数据提供者
* 租户数据提供者
*
* @author Charles7c
* @since 2.7.0
*/
public interface TenantDataSourceProvider {
public interface TenantProvider {
/**
* 根据租户 ID 获取数据源配置
* 根据租户ID获取租户数据
*
* @param tenantId 租户 ID
* @param verify 是否验证租户有效性
* @return 数据源配置
*/
TenantDataSource getByTenantId(String tenantId);
TenantContext getByTenantId(String tenantId, boolean verify);
}

View File

@@ -16,6 +16,9 @@
package top.continew.starter.extension.tenant.context;
import top.continew.starter.extension.tenant.config.TenantDataSource;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* 租户上下文
*
@@ -29,6 +32,16 @@ public class TenantContext {
*/
private Long tenantId;
/**
* 隔离级别
*/
private TenantIsolationLevel isolationLevel;
/**
* 数据源信息
*/
private TenantDataSource dataSource;
public Long getTenantId() {
return tenantId;
}
@@ -36,4 +49,20 @@ public class TenantContext {
public void setTenantId(Long tenantId) {
this.tenantId = tenantId;
}
public TenantIsolationLevel getIsolationLevel() {
return isolationLevel;
}
public void setIsolationLevel(TenantIsolationLevel isolationLevel) {
this.isolationLevel = isolationLevel;
}
public TenantDataSource getDataSource() {
return dataSource;
}
public void setDataSource(TenantDataSource dataSource) {
this.dataSource = dataSource;
}
}

View File

@@ -17,6 +17,7 @@
package top.continew.starter.extension.tenant.context;
import com.alibaba.ttl.TransmittableThreadLocal;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
import java.util.Optional;
@@ -66,4 +67,13 @@ public class TenantContextHolder {
public static Long getTenantId() {
return Optional.ofNullable(getContext()).map(TenantContext::getTenantId).orElse(null);
}
/**
* 获取隔离级别
*/
public static TenantIsolationLevel getIsolationLevel() {
return Optional.ofNullable(getContext())
.map(TenantContext::getIsolationLevel)
.orElse(TenantIsolationLevel.LINE);
}
}

View File

@@ -31,9 +31,8 @@ public interface TenantDataSourceHandler {
/**
* 切换数据源
*
* @param dataSourceName 数据源名称
*/
void changeDataSource(String dataSourceName);
void changeDataSource(TenantDataSource tenantDataSource);
/**
* 是否存在指定数据源

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.extension.tenant.handler;
/**
* @description: 租户处理器
* @author: 小熊
* @create: 2024-12-18 19:37
*/
public interface TenantHandler {
/**
* 在指定租户中执行方法
*/
void executeInTenant(Long tenantId, Runnable runnable);
}

View File

@@ -23,7 +23,6 @@
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<optional>true</optional>
</dependency>
<!-- 核心模块 -->

View File

@@ -30,7 +30,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.core.ResolvableType;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.extension.tenant.config.TenantDataSourceProvider;
import top.continew.starter.extension.tenant.config.TenantProvider;
import top.continew.starter.extension.tenant.handler.*;
/**
@@ -49,14 +49,8 @@ public class TenantAutoConfiguration {
private TenantAutoConfiguration() {
}
/**
* 租户隔离级别:行级
*/
@AutoConfiguration
@ConditionalOnProperty(name = PropertiesConstants.TENANT + ".isolation-level", havingValue = "line", matchIfMissing = true)
public static class Line {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'Tenant-Line' completed initialization.");
log.debug("[ContiNew Starter] - Auto Configuration 'Tenant' completed initialization.");
}
/**
@@ -76,17 +70,6 @@ public class TenantAutoConfiguration {
public TenantLineHandler tenantLineHandler(TenantProperties properties) {
return new DefaultTenantLineHandler(properties);
}
}
/**
* 租户隔离级别:数据源级
*/
@AutoConfiguration
@ConditionalOnProperty(name = PropertiesConstants.TENANT + ".isolation-level", havingValue = "datasource")
public static class DataSource {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'Tenant-DataSource' completed initialization.");
}
/**
* 租户数据源级隔离通知
@@ -111,10 +94,9 @@ public class TenantAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public TenantDataSourceHandler tenantDataSourceHandler(TenantDataSourceProvider tenantDataSourceProvider,
DynamicRoutingDataSource dynamicRoutingDataSource,
public TenantDataSourceHandler tenantDataSourceHandler(javax.sql.DataSource dataSource,
DefaultDataSourceCreator dataSourceCreator) {
return new DefaultTenantDataSourceHandler(tenantDataSourceProvider, dynamicRoutingDataSource, dataSourceCreator);
return new DefaultTenantDataSourceHandler((DynamicRoutingDataSource)dataSource, dataSourceCreator);
}
/**
@@ -122,12 +104,21 @@ public class TenantAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public TenantDataSourceProvider tenantDataSourceProvider() {
public TenantProvider tenantDataSourceProvider() {
if (log.isErrorEnabled()) {
log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType
.forClass(TenantDataSourceProvider.class));
.forClass(TenantProvider.class));
}
throw new NoSuchBeanDefinitionException(TenantDataSourceProvider.class);
throw new NoSuchBeanDefinitionException(TenantProvider.class);
}
/**
* 租户处理器
*/
@Bean
@ConditionalOnMissingBean
public TenantHandler tenantHandler(TenantDataSourceHandler tenantDataSourceHandler, TenantProvider tenantProvider) {
return new DefaultTenantHandler(tenantDataSourceHandler, tenantProvider);
}
}

View File

@@ -24,7 +24,6 @@ import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.continew.starter.extension.tenant.config.TenantDataSource;
import top.continew.starter.extension.tenant.config.TenantDataSourceProvider;
import javax.sql.DataSource;
@@ -39,24 +38,18 @@ public class DefaultTenantDataSourceHandler implements TenantDataSourceHandler {
private static final Logger log = LoggerFactory.getLogger(DefaultTenantDataSourceHandler.class);
private final DynamicRoutingDataSource dynamicRoutingDataSource;
private final DefaultDataSourceCreator dataSourceCreator;
private final TenantDataSourceProvider tenantDataSourceProvider;
public DefaultTenantDataSourceHandler(TenantDataSourceProvider tenantDataSourceProvider,
DynamicRoutingDataSource dynamicRoutingDataSource,
public DefaultTenantDataSourceHandler(DynamicRoutingDataSource dynamicRoutingDataSource,
DefaultDataSourceCreator dataSourceCreator) {
this.tenantDataSourceProvider = tenantDataSourceProvider;
this.dynamicRoutingDataSource = dynamicRoutingDataSource;
this.dataSourceCreator = dataSourceCreator;
}
@Override
public void changeDataSource(String dataSourceName) {
public void changeDataSource(TenantDataSource tenantDataSource) {
if (tenantDataSource != null) {
String dataSourceName = tenantDataSource.getPoolName();
if (!this.containsDataSource(dataSourceName)) {
TenantDataSource tenantDataSource = tenantDataSourceProvider.getByTenantId(dataSourceName);
if (null == tenantDataSource) {
throw new IllegalArgumentException("Data source [%s] configuration not found"
.formatted(dataSourceName));
}
DataSource datasource = this.createDataSource(tenantDataSource);
dynamicRoutingDataSource.addDataSource(dataSourceName, datasource);
log.info("Load data source: {}", dataSourceName);
@@ -64,6 +57,7 @@ public class DefaultTenantDataSourceHandler implements TenantDataSourceHandler {
DynamicDataSourceContextHolder.push(dataSourceName);
log.info("Change data source: {}", dataSourceName);
}
}
@Override
public boolean containsDataSource(String dataSourceName) {

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.extension.tenant.handler;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import top.continew.starter.extension.tenant.config.TenantProvider;
import top.continew.starter.extension.tenant.context.TenantContext;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* @description: 租户处理器
* @author: 小熊
* @create: 2024-12-18 19:43
*/
public class DefaultTenantHandler implements TenantHandler {
private final TenantDataSourceHandler dataSourceHandler;
private final TenantProvider tenantProvider;
public DefaultTenantHandler(TenantDataSourceHandler dataSourceHandler, TenantProvider tenantProvider) {
this.dataSourceHandler = dataSourceHandler;
this.tenantProvider = tenantProvider;
}
@Override
public void executeInTenant(Long tenantId, Runnable runnable) {
boolean enabled = SpringUtil.getProperty("continew-starter.tenant.enabled", Boolean.class, false);
if (enabled) {
TenantContext tenantHandler = tenantProvider.getByTenantId(tenantId.toString(), false);
// 保存当前的租户上下文
TenantContext originalContext = TenantContextHolder.getContext();
// 切换数据源
boolean isPush = false;
try {
// 设置新的租户上下文
TenantContextHolder.setContext(tenantHandler);
if (TenantIsolationLevel.DATASOURCE.equals(tenantHandler.getIsolationLevel())) {
//切换数据源
dataSourceHandler.changeDataSource(tenantHandler.getDataSource());
isPush = true;
}
runnable.run();
} finally {
// 恢复原始的租户上下文
if (originalContext != null) {
TenantContextHolder.setContext(originalContext);
} else {
TenantContextHolder.clearContext();
}
if (isPush) {
DynamicDataSourceContextHolder.poll();
}
}
}
}
}

View File

@@ -22,6 +22,7 @@ import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* 默认租户行级隔离处理器
@@ -54,7 +55,8 @@ public class DefaultTenantLineHandler implements TenantLineHandler {
@Override
public boolean ignoreTable(String tableName) {
Long tenantId = TenantContextHolder.getTenantId();
if (null != tenantId && tenantId.equals(tenantProperties.getSuperTenantId())) {
if ((null != tenantId && tenantId.equals(tenantProperties
.getSuperTenantId())) || TenantIsolationLevel.DATASOURCE.equals(TenantContextHolder.getIsolationLevel())) {
return true;
}
return CollUtil.contains(tenantProperties.getIgnoreTables(), tableName);

View File

@@ -65,7 +65,7 @@ public class TenantDataSourceAdvisor extends AbstractPointcutAdvisor implements
*/
private Pointcut buildPointcut() {
AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
cut.setExpression("!@annotation(top.continew.starter.extension.tenant.annotation.TenantDataSourceIgnore)");
cut.setExpression("execution(* *..controller..*(..))");
return new ComposablePointcut((Pointcut)cut);
}
}

View File

@@ -20,6 +20,7 @@ import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* 租户数据源级隔离拦截器
@@ -37,15 +38,11 @@ public class TenantDataSourceInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Long tenantId = TenantContextHolder.getTenantId();
if (null == tenantId) {
return invocation.proceed();
}
if (TenantContextHolder.getIsolationLevel() == TenantIsolationLevel.DATASOURCE) {
// 切换数据源
boolean isPush = false;
try {
String dataSourceName = tenantId.toString();
tenantDataSourceHandler.changeDataSource(dataSourceName);
tenantDataSourceHandler.changeDataSource(TenantContextHolder.getContext().getDataSource());
isPush = true;
return invocation.proceed();
} finally {
@@ -54,4 +51,7 @@ public class TenantDataSourceInterceptor implements MethodInterceptor {
}
}
}
return invocation.proceed();
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.log.aop.aspect;
import com.alibaba.ttl.TransmittableThreadLocal;
@@ -48,7 +64,7 @@ public class ConsoleLogAspect {
// 打印请求日志
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
Instant startTime = Instant.now();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
@@ -65,16 +81,16 @@ public class ConsoleLogAspect {
// 打印请求耗时
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
Instant endTime = Instant.now();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
Duration timeTaken = Duration.between(timeTtl.get(), endTime);
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(),
response != null ? response.getStatus() : "N/A",
timeTaken.toMillis());
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response != null
? response.getStatus()
: "N/A", timeTaken.toMillis());
}
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.log.aop.aspect;
import cn.hutool.core.text.CharSequenceUtil;
@@ -58,7 +74,7 @@ public class LogAspect {
@Before(value = "pointcutService()")
public void doBefore() {
Instant startTime = Instant.now();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
LogRecord.Started startedLogRecord = LogRecord.start(startTime, new RecordableServletHttpRequest(request));
@@ -66,7 +82,6 @@ public class LogAspect {
}
}
/**
* 处理请求后执行 - 正常返回
*
@@ -98,7 +113,7 @@ public class LogAspect {
private void handleAfterCompletion(JoinPoint joinPoint, Exception ex) {
try {
Instant endTime = Instant.now();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
@@ -110,7 +125,7 @@ public class LogAspect {
}
// 获取方法和类注解信息
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
Class<?> targetClass = joinPoint.getTarget().getClass();
@@ -122,9 +137,7 @@ public class LogAspect {
// 完成日志记录
LogRecord finishedLogRecord = startedLogRecord
.finish(endTime,
new RecordableServletHttpResponse(response, response.getStatus()),
includeSet);
.finish(endTime, new RecordableServletHttpResponse(response, response.getStatus()), includeSet);
// 记录异常
if (ex != null) {
finishedLogRecord.getResponse().setStatus(1);

View File

@@ -57,7 +57,7 @@ public class LogAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public LogAspect logAspect() {
return new LogAspect(logDao(),logProperties);
return new LogAspect(logDao(), logProperties);
}
/**

View File

@@ -49,7 +49,6 @@ public class LogProperties {
*/
private Set<Include> includes = Include.defaultIncludes();
public boolean isEnabled() {
return enabled;
}

View File

@@ -175,6 +175,7 @@ public class LogRecord {
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}