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

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

@@ -6,7 +6,6 @@ import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.common.constant.enums.EnumJobStatus;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.controller.BaseCurdController;
@@ -38,23 +37,14 @@ public class SysJobController extends BaseCurdController<SysJobService, SysJob>
@GetMapping("/start")
@SaCheckPermission("/api/v1/sysJob/save")
public Result<Void> start(BigInteger id) {
SysJob sysJob = service.getById(id);
sysJob.setStatus(EnumJobStatus.RUNNING.getCode());
service.addJob(sysJob);
service.updateById(sysJob);
service.startJob(id);
return Result.ok();
}
@GetMapping("/stop")
@SaCheckPermission("/api/v1/sysJob/save")
public Result<Void> stop(BigInteger id) {
SysJob sysJob = new SysJob();
sysJob.setId(id);
sysJob.setStatus(EnumJobStatus.STOP.getCode());
ArrayList<Serializable> ids = new ArrayList<>();
ids.add(id);
service.deleteJob(ids);
service.updateById(sysJob);
service.stopJob(id);
return Result.ok();
}
@@ -88,4 +78,4 @@ public class SysJobController extends BaseCurdController<SysJobService, SysJob>
service.deleteJob(ids);
return super.onRemoveBefore(ids);
}
}
}

View File

@@ -16,6 +16,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -156,4 +157,14 @@ public class SysAccountController extends BaseCurdController<SysAccountService,
service.updateById(update);
return Result.ok();
}
}
@Override
@PostMapping("save")
public Result<?> save(@JsonBody SysAccount entity) {
try {
return super.save(entity);
} catch (DuplicateKeyException e) {
return Result.fail(1, "用户名已存在");
}
}
}

View File

@@ -10,12 +10,14 @@ import tech.easyflow.common.web.jsonbody.JsonBody;
import tech.easyflow.system.entity.SysOption;
import tech.easyflow.system.service.SysOptionService;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -39,7 +41,10 @@ public class SysOptionController extends BaseController {
if (keys == null || keys.length == 0) {
return Result.ok(data);
}
List<SysOption> list = service.list(QueryWrapper.create().in(SysOption::getKey, (Object[]) keys));
BigInteger tenantId = SaTokenUtil.getLoginAccount().getTenantId();
List<SysOption> list = service.list(QueryWrapper.create()
.eq(SysOption::getTenantId, tenantId)
.in(SysOption::getKey, (Object[]) keys));
for (SysOption sysOption : list) {
data.put(sysOption.getKey(), sysOption.getValue());
}
@@ -62,12 +67,21 @@ public class SysOptionController extends BaseController {
if (key == null || key.isEmpty()) {
throw new BusinessException("key is empty");
}
sysOption.setTenantId(SaTokenUtil.getLoginAccount().getTenantId());
SysOption record = service.getByOptionKey(key);
if (record == null) {
service.save(sysOption);
} else {
BigInteger tenantId = SaTokenUtil.getLoginAccount().getTenantId();
sysOption.setTenantId(tenantId);
try {
SysOption record = service.getByOptionKey(key, tenantId);
if (record == null) {
service.save(sysOption);
} else {
QueryWrapper w = QueryWrapper.create();
w.eq(SysOption::getTenantId, tenantId);
w.eq(SysOption::getKey, key);
service.update(sysOption, w);
}
} catch (DuplicateKeyException e) {
QueryWrapper w = QueryWrapper.create();
w.eq(SysOption::getTenantId, tenantId);
w.eq(SysOption::getKey, key);
service.update(sysOption, w);
}
@@ -79,6 +93,7 @@ public class SysOptionController extends BaseController {
if (key == null || key.isEmpty()) {
throw new BusinessException("key is empty");
}
return Result.ok(service.getByOptionKey(key));
BigInteger tenantId = SaTokenUtil.getLoginAccount().getTenantId();
return Result.ok(service.getByOptionKey(key, tenantId));
}
}
}

View File

@@ -6,8 +6,11 @@ import cn.dev33.satoken.annotation.SaIgnore;
import com.alicp.jetcache.Cache;
import com.mybatisflex.core.keygen.impl.SnowFlakeIDKeyGenerator;
import com.mybatisflex.core.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -42,6 +45,8 @@ import java.util.Map;
@UsePermission(moduleName = "/api/v1/bot")
public class UcBotController extends BaseCurdController<BotService, Bot> {
private static final Logger log = LoggerFactory.getLogger(UcBotController.class);
private final ModelService modelService;
private final BotWorkflowService botWorkflowService;
private final BotDocumentCollectionService botDocumentCollectionService;
@@ -160,7 +165,12 @@ public class UcBotController extends BaseCurdController<BotService, Bot> {
conversation.setBotId(botId);
conversation.setAccountId(SaTokenUtil.getLoginAccount().getId());
commonFiled(conversation, SaTokenUtil.getLoginAccount().getId(), SaTokenUtil.getLoginAccount().getTenantId(), SaTokenUtil.getLoginAccount().getDeptId());
conversationMessageService.save(conversation);
try {
conversationMessageService.save(conversation);
} catch (DuplicateKeyException e) {
// 并发重试场景下允许重复创建请求,唯一主键冲突按已创建处理。
log.debug("conversation already exists, conversationId={}", conversationId, e);
}
}
return botService.startChat(botId, prompt, conversationId, messages, chatCheckResult, attachments);

View File

@@ -3,8 +3,10 @@ package tech.easyflow.usercenter.controller.ai;
import cn.hutool.core.collection.CollectionUtil;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.dao.DuplicateKeyException;
import tech.easyflow.ai.entity.Bot;
import tech.easyflow.ai.entity.BotRecentlyUsed;
import tech.easyflow.ai.service.BotRecentlyUsedService;
@@ -14,13 +16,16 @@ import tech.easyflow.common.domain.Result;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.controller.BaseCurdController;
import tech.easyflow.common.web.jsonbody.JsonBody;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -81,4 +86,53 @@ public class UcBotRecentlyUsedController extends BaseCurdController<BotRecentlyU
entity.setCreatedBy(SaTokenUtil.getLoginAccount().getId());
return super.onSaveOrUpdateBefore(entity, isSave);
}
}
@PostMapping("save")
@Override
public Result<?> save(@JsonBody BotRecentlyUsed entity) {
if (entity == null || entity.getBotId() == null) {
return Result.fail("botId不能为空");
}
LoginAccount account = SaTokenUtil.getLoginAccount();
Date now = new Date();
QueryWrapper queryWrapper = QueryWrapper.create()
.eq(BotRecentlyUsed::getCreatedBy, account.getId())
.eq(BotRecentlyUsed::getBotId, entity.getBotId());
BotRecentlyUsed exist = service.getOne(queryWrapper);
if (exist != null) {
BotRecentlyUsed update = new BotRecentlyUsed();
update.setId(exist.getId());
update.setCreated(now);
update.setSortNo(entity.getSortNo() == null ? exist.getSortNo() : entity.getSortNo());
service.updateById(update);
return buildSaveResult(exist.getId());
}
entity.setCreated(now);
entity.setCreatedBy(account.getId());
if (entity.getSortNo() == null) {
entity.setSortNo(0);
}
try {
service.save(entity);
return buildSaveResult(entity.getId());
} catch (DuplicateKeyException e) {
BotRecentlyUsed saved = service.getOne(queryWrapper);
if (saved != null) {
BotRecentlyUsed update = new BotRecentlyUsed();
update.setId(saved.getId());
update.setCreated(now);
service.updateById(update);
return buildSaveResult(saved.getId());
}
throw e;
}
}
private Result<?> buildSaveResult(BigInteger id) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("id", id);
return Result.ok(resultMap);
}
}