调整框架及订单模块
This commit is contained in:
@@ -8,7 +8,6 @@ import com.tashow.cloud.redis.config.TashowCacheProperties;
|
||||
import com.tashow.cloud.systemapi.api.tenant.TenantApi;
|
||||
import com.tashow.cloud.tenant.core.aop.TenantIgnoreAspect;
|
||||
import com.tashow.cloud.tenant.core.db.TenantDatabaseInterceptor;
|
||||
import com.tashow.cloud.tenant.core.job.TenantJobAspect;
|
||||
import com.tashow.cloud.tenant.core.mq.rabbitmq.TenantRabbitMQInitializer;
|
||||
import com.tashow.cloud.tenant.core.redis.TenantRedisCacheManager;
|
||||
import com.tashow.cloud.tenant.core.security.TenantSecurityWebFilter;
|
||||
@@ -86,14 +85,6 @@ public class TenantAutoConfiguration {
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
// ========== Job ==========
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = "com.xxl.job.core.handler.annotation.XxlJob")
|
||||
public TenantJobAspect tenantJobAspect(TenantFrameworkService tenantFrameworkService) {
|
||||
return new TenantJobAspect(tenantFrameworkService);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate")
|
||||
|
||||
@@ -2,14 +2,13 @@ package com.tashow.cloud.tenant.config;
|
||||
|
||||
import com.tashow.cloud.systemapi.api.tenant.TenantApi;
|
||||
import com.tashow.cloud.tenant.core.rpc.TenantRequestInterceptor;
|
||||
import com.tashow.cloud.tenant.core.rpc.TenantRequestInterceptor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(prefix = "tashow.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
|
||||
@ConditionalOnProperty(prefix = "tashow.tenant", value = "enable", matchIfMissing = true)
|
||||
@EnableFeignClients(clients = TenantApi.class) // 主要是引入相关的 API 服务
|
||||
public class TenantRpcAutoConfiguration {
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.tashow.cloud.tenant.core.job;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 多租户 Job 注解
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TenantJob {
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package com.tashow.cloud.tenant.core.job;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.tashow.cloud.common.util.json.JsonUtils;
|
||||
import com.tashow.cloud.tenant.core.service.TenantFrameworkService;
|
||||
import com.tashow.cloud.tenant.core.util.TenantUtils;
|
||||
import com.xxl.job.core.context.XxlJobContext;
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 多租户 JobHandler AOP
|
||||
* 任务执行时,会按照租户逐个执行 Job 的逻辑
|
||||
*
|
||||
* 注意,需要保证 JobHandler 的幂等性。因为 Job 因为某个租户执行失败重试时,之前执行成功的租户也会再次执行。
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Aspect
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class TenantJobAspect {
|
||||
|
||||
private final TenantFrameworkService tenantFrameworkService;
|
||||
|
||||
@Around("@annotation(tenantJob)")
|
||||
public void around(ProceedingJoinPoint joinPoint, TenantJob tenantJob) {
|
||||
// 获得租户列表
|
||||
List<Long> tenantIds = tenantFrameworkService.getTenantIds();
|
||||
if (CollUtil.isEmpty(tenantIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 逐个租户,执行 Job
|
||||
Map<Long, String> results = new ConcurrentHashMap<>();
|
||||
AtomicBoolean success = new AtomicBoolean(true); // 标记,是否存在失败的情况
|
||||
XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); // XXL-Job 上下文
|
||||
tenantIds.parallelStream().forEach(tenantId -> {
|
||||
// TODO 芋艿:先通过 parallel 实现并行;1)多个租户,是一条执行日志;2)异常的情况
|
||||
TenantUtils.execute(tenantId, () -> {
|
||||
try {
|
||||
XxlJobContext.setXxlJobContext(xxlJobContext);
|
||||
// 执行 Job
|
||||
Object result = joinPoint.proceed();
|
||||
results.put(tenantId, StrUtil.toStringOrEmpty(result));
|
||||
} catch (Throwable e) {
|
||||
results.put(tenantId, ExceptionUtil.getRootCauseMessage(e));
|
||||
success.set(false);
|
||||
// 打印异常
|
||||
XxlJobHelper.log(StrUtil.format("[多租户({}) 执行任务({}),发生异常:{}]",
|
||||
tenantId, joinPoint.getSignature(), ExceptionUtils.getStackTrace(e)));
|
||||
}
|
||||
});
|
||||
});
|
||||
// 记录执行结果
|
||||
if (success.get()) {
|
||||
XxlJobHelper.handleSuccess(JsonUtils.toJsonString(results));
|
||||
} else {
|
||||
XxlJobHelper.handleFail(JsonUtils.toJsonString(results));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user