负载均衡深度改造,增加分布式锁,表唯一约束等

This commit is contained in:
2026-02-24 11:17:33 +08:00
parent 8d711dc3a2
commit 148a08a3f1
27 changed files with 891 additions and 182 deletions

View File

@@ -4,6 +4,7 @@ import com.mybatisflex.core.service.IService;
import tech.easyflow.job.entity.SysJob;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Collection;
/**
@@ -21,4 +22,8 @@ public interface SysJobService extends IService<SysJob> {
void addJob(SysJob job);
void deleteJob(Collection<Serializable> ids);
void startJob(BigInteger id);
void stopJob(BigInteger id);
}

View File

@@ -6,6 +6,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tech.easyflow.common.constant.enums.EnumMisfirePolicy;
import tech.easyflow.common.constant.enums.EnumJobStatus;
import tech.easyflow.common.cache.RedisLockExecutor;
import tech.easyflow.job.entity.SysJob;
import tech.easyflow.job.job.JobConstant;
import tech.easyflow.job.job.QuartzJob;
@@ -17,7 +19,9 @@ import tech.easyflow.job.util.JobUtil;
import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
/**
* 系统任务表 服务层实现。
@@ -28,11 +32,18 @@ import java.util.Collection;
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService {
private static final String JOB_LOCK_KEY_PREFIX = "easyflow:lock:job:";
private static final Duration LOCK_WAIT_TIMEOUT = Duration.ofSeconds(2);
private static final Duration LOCK_LEASE_TIMEOUT = Duration.ofSeconds(10);
protected Logger log = LoggerFactory.getLogger(SysJobServiceImpl.class);
@Resource
private Scheduler scheduler;
@Resource
private RedisLockExecutor redisLockExecutor;
@Override
public void test() {
System.out.println("java bean 动态执行");
@@ -92,4 +103,54 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implem
throw new RuntimeException(e);
}
}
@Override
public void startJob(BigInteger id) {
redisLockExecutor.executeWithLock(JOB_LOCK_KEY_PREFIX + id, LOCK_WAIT_TIMEOUT, LOCK_LEASE_TIMEOUT, () -> {
SysJob sysJob = this.getById(id);
if (sysJob == null) {
throw new IllegalStateException("任务不存在id=" + id);
}
try {
JobKey jobKey = JobUtil.getJobKey(sysJob);
if (!scheduler.checkExists(jobKey)) {
addJob(sysJob);
}
if (!Integer.valueOf(EnumJobStatus.RUNNING.getCode()).equals(sysJob.getStatus())) {
SysJob update = new SysJob();
update.setId(id);
update.setStatus(EnumJobStatus.RUNNING.getCode());
this.updateById(update);
}
} catch (SchedulerException e) {
log.error("启动任务失败id={}", id, e);
throw new RuntimeException(e);
}
});
}
@Override
public void stopJob(BigInteger id) {
redisLockExecutor.executeWithLock(JOB_LOCK_KEY_PREFIX + id, LOCK_WAIT_TIMEOUT, LOCK_LEASE_TIMEOUT, () -> {
SysJob sysJob = this.getById(id);
if (sysJob == null) {
throw new IllegalStateException("任务不存在id=" + id);
}
try {
JobKey jobKey = JobUtil.getJobKey(sysJob);
if (scheduler.checkExists(jobKey)) {
deleteJob(Collections.singletonList(id));
}
if (!Integer.valueOf(EnumJobStatus.STOP.getCode()).equals(sysJob.getStatus())) {
SysJob update = new SysJob();
update.setId(id);
update.setStatus(EnumJobStatus.STOP.getCode());
this.updateById(update);
}
} catch (SchedulerException e) {
log.error("停止任务失败id={}", id, e);
throw new RuntimeException(e);
}
});
}
}