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

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,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);
}
}