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

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

@@ -24,9 +24,13 @@
<groupId>tech.easyflow</groupId>
<artifactId>easyflow-common-satoken</artifactId>
</dependency>
<dependency>
<groupId>tech.easyflow</groupId>
<artifactId>easyflow-common-cache</artifactId>
</dependency>
<dependency>
<groupId>tech.easyflow</groupId>
<artifactId>easyflow-common-web</artifactId>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -11,6 +11,7 @@ import com.mybatisflex.core.row.Row;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.easyflow.common.cache.RedisLockExecutor;
import tech.easyflow.common.entity.DatacenterQuery;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.web.exceptions.BusinessException;
@@ -26,21 +27,35 @@ import tech.easyflow.datacenter.service.DatacenterTableService;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class DatacenterTableServiceImpl extends ServiceImpl<DatacenterTableMapper, DatacenterTable> implements DatacenterTableService {
private static final String DATACENTER_TABLE_LOCK_KEY_PREFIX = "easyflow:lock:datacenter:table:";
private static final String DATACENTER_TABLE_CREATE_LOCK_KEY_PREFIX = "easyflow:lock:datacenter:table:create:";
private static final Duration LOCK_WAIT_TIMEOUT = Duration.ofSeconds(2);
private static final Duration LOCK_LEASE_TIMEOUT = Duration.ofSeconds(15);
@Resource
private DbHandleManager dbHandleManager;
@Resource
private DatacenterTableFieldMapper fieldsMapper;
@Resource
private RedisLockExecutor redisLockExecutor;
@Override
@Transactional(rollbackFor = Exception.class)
public void saveTable(DatacenterTable entity, LoginAccount loginUser) {
String lockKey = buildTableLockKey(entity, loginUser);
redisLockExecutor.executeWithLock(lockKey, LOCK_WAIT_TIMEOUT, LOCK_LEASE_TIMEOUT, () -> {
doSaveTable(entity, loginUser);
});
}
private void doSaveTable(DatacenterTable entity, LoginAccount loginUser) {
DbHandleService dbHandler = dbHandleManager.getDbHandler();
List<DatacenterTableField> fields = entity.getFields();
@@ -117,12 +132,19 @@ public class DatacenterTableServiceImpl extends ServiceImpl<DatacenterTableMappe
@Override
@Transactional(rollbackFor = Exception.class)
public void removeTable(BigInteger tableId) {
DatacenterTable record = getById(tableId);
dbHandleManager.getDbHandler().deleteTable(record);
removeById(tableId);
QueryWrapper wrapper = QueryWrapper.create();
wrapper.eq(DatacenterTableField::getTableId, tableId);
fieldsMapper.deleteByQuery(wrapper);
redisLockExecutor.executeWithLock(
DATACENTER_TABLE_LOCK_KEY_PREFIX + tableId,
LOCK_WAIT_TIMEOUT,
LOCK_LEASE_TIMEOUT,
() -> {
DatacenterTable record = getById(tableId);
dbHandleManager.getDbHandler().deleteTable(record);
removeById(tableId);
QueryWrapper wrapper = QueryWrapper.create();
wrapper.eq(DatacenterTableField::getTableId, tableId);
fieldsMapper.deleteByQuery(wrapper);
}
);
}
@Override
@@ -247,6 +269,17 @@ public class DatacenterTableServiceImpl extends ServiceImpl<DatacenterTableMappe
return "tb_dynamic_" + tableName + "_" + id;
}
private String buildTableLockKey(DatacenterTable table, LoginAccount loginUser) {
if (table.getId() != null) {
return DATACENTER_TABLE_LOCK_KEY_PREFIX + table.getId();
}
String tenant = table.getTenantId() != null
? table.getTenantId().toString()
: (loginUser != null && loginUser.getTenantId() != null ? loginUser.getTenantId().toString() : "0");
String tableName = table.getTableName() == null ? "unknown" : table.getTableName();
return DATACENTER_TABLE_CREATE_LOCK_KEY_PREFIX + tenant + ":" + tableName;
}
/**
* 构建查询条件
*/