update 优化 自动判断数据库类型

This commit is contained in:
疯狂的狮子Li 2024-03-27 14:26:12 +08:00
parent 473e1de682
commit 40496b4e23
39 changed files with 42 additions and 104 deletions

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.template.datasource.access.config;
import com.aizuda.easy.retry.common.core.enums.NodeTypeEnum;
import com.aizuda.easy.retry.common.core.enums.NotifySceneEnum;
import com.aizuda.easy.retry.common.core.util.EnvironmentUtils;
import com.aizuda.easy.retry.server.model.dto.ConfigDTO;
import com.aizuda.easy.retry.template.datasource.access.ConfigAccess;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
@ -12,6 +13,7 @@ import com.aizuda.easy.retry.template.datasource.persistence.mapper.SceneConfigM
import com.aizuda.easy.retry.template.datasource.persistence.po.GroupConfig;
import com.aizuda.easy.retry.template.datasource.persistence.po.NotifyConfig;
import com.aizuda.easy.retry.template.datasource.persistence.po.SceneConfig;
import com.aizuda.easy.retry.template.datasource.utils.DbUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
@ -45,8 +47,7 @@ public abstract class AbstractConfigAccess<T> implements ConfigAccess<T> {
DbTypeEnum.SQLSERVER.getDb());
protected DbTypeEnum getDbType() {
String dbType = environment.getProperty("easy-retry.db-type");
return DbTypeEnum.modeOf(dbType);
return DbUtils.getDbType();
}
protected List<NotifyConfig> getByGroupIdAndNotifyScene(String groupName, Integer notifyScene, String namespaceId) {

View File

@ -1,7 +1,9 @@
package com.aizuda.easy.retry.template.datasource.access.task;
import com.aizuda.easy.retry.common.core.util.EnvironmentUtils;
import com.aizuda.easy.retry.template.datasource.access.TaskAccess;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
import com.aizuda.easy.retry.template.datasource.utils.DbUtils;
import com.aizuda.easy.retry.template.datasource.utils.RequestDataHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -29,8 +31,7 @@ public abstract class AbstractTaskAccess<T> implements TaskAccess<T> {
DbTypeEnum.SQLSERVER.getDb());
protected DbTypeEnum getDbType() {
String dbType = environment.getProperty("easy-retry.db-type");
return DbTypeEnum.modeOf(dbType);
return DbUtils.getDbType();
}
/**

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.template.datasource.config;
import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
import com.aizuda.easy.retry.template.datasource.utils.DbUtils;
import com.aizuda.easy.retry.template.datasource.utils.RequestDataHelper;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
@ -36,8 +37,7 @@ public class EasyRetryTemplateAutoConfiguration {
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, Environment environment, MybatisPlusInterceptor mybatisPlusInterceptor, MybatisPlusProperties mybatisPlusProperties) throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
String dbType = environment.getProperty("easy-retry.db-type");
DbTypeEnum dbTypeEnum = DbTypeEnum.modeOf(dbType);
DbTypeEnum dbTypeEnum = DbUtils.getDbType();
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MessageFormat.format("classpath*:/{0}/mapper/*.xml", dbTypeEnum.getDb())));
factoryBean.setPlugins(mybatisPlusInterceptor);
factoryBean.setTypeAliasesPackage(mybatisPlusProperties.getTypeAliasesPackage());
@ -58,8 +58,7 @@ public class EasyRetryTemplateAutoConfiguration {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
String tablePrefix = Optional.ofNullable(environment.getProperty("mybatis-plus.global-config.db-config.table-prefix")).orElse(StrUtil.EMPTY);
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor(tablePrefix));
String dbType = environment.getProperty("easy-retry.db-type");
DbTypeEnum dbTypeEnum = DbTypeEnum.modeOf(dbType);
DbTypeEnum dbTypeEnum = DbUtils.getDbType();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbTypeEnum.getMpDbType()));
return interceptor;

View File

@ -19,7 +19,7 @@ import java.util.Objects;
public enum DbTypeEnum {
MYSQL("mysql", "MySql数据库", DbType.MYSQL),
MARIADB("mariadb", "MariaDB数据库", DbType.MARIADB),
POSTGRES("postgres", "Postgres数据库", DbType.POSTGRE_SQL),
POSTGRES("postgresql", "Postgres数据库", DbType.POSTGRE_SQL),
ORACLE("oracle", "Oracle数据库", DbType.ORACLE_12C),
SQLSERVER("sqlserver", "SQLServer数据库", DbType.SQL_SERVER);
@ -29,7 +29,7 @@ public enum DbTypeEnum {
public static DbTypeEnum modeOf(String db) {
for (DbTypeEnum value : DbTypeEnum.values()) {
if (Objects.equals(value.getDb(), db)) {
if (db.contains(value.getDb())) {
return value;
}
}

View File

@ -0,0 +1,21 @@
package com.aizuda.easy.retry.template.datasource.utils;
import com.aizuda.easy.retry.common.core.context.SpringContext;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
import org.springframework.core.env.Environment;
/**
* 数据库工具
*
* @author: 疯狂的狮子Li
* @date : 2024-03-27 14:17
*/
public class DbUtils {
public static DbTypeEnum getDbType() {
Environment environment = SpringContext.getBean(Environment.class);
String url = environment.getProperty("spring.datasource.url");
return DbTypeEnum.modeOf(url);
}
}

View File

@ -1,16 +0,0 @@
package com.aizuda.easy.retry.mariadb.datasource.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author: www.byteblogs.com
* @date : 2023-08-04 12:37
*/
@Configuration
@ComponentScan("com.aizuda.easy.retry.mariadb.datasource.*")
@ConditionalOnProperty(prefix = "easy-retry", name = "db-type", havingValue = "mariadb")
public class EasyRetryMariadbAutoConfiguration {
}

View File

@ -1 +0,0 @@
com.aizuda.easy.retry.mariadb.datasource.config.EasyRetryMariadbAutoConfiguration

View File

@ -1,16 +0,0 @@
package com.aizuda.easy.retry.mysql.datasource.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author: www.byteblogs.com
* @date : 2023-08-04 12:37
*/
@Configuration
@ComponentScan("com.aizuda.easy.retry.mysql.datasource")
@ConditionalOnProperty(prefix = "easy-retry", name = "db-type", havingValue = "mysql")
public class EasyRetryMysqlAutoConfiguration {
}

View File

@ -1 +0,0 @@
com.aizuda.easy.retry.mysql.datasource.config.EasyRetryMysqlAutoConfiguration

View File

@ -1,16 +0,0 @@
package com.aizuda.easy.retry.oracle.datasource.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author: www.byteblogs.com
* @date : 2023-08-04 12:37
*/
@Configuration
@ComponentScan("com.aizuda.easy.retry.oracle.datasource.*")
@ConditionalOnProperty(prefix = "easy-retry", name = "db-type", havingValue = "oracle")
public class EasyRetryOracleAutoConfiguration {
}

View File

@ -1 +0,0 @@
com.aizuda.easy.retry.oracle.datasource.config.EasyRetryOracleAutoConfiguration

View File

@ -1,17 +0,0 @@
package com.aizuda.easy.retry.postgres.datasource.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @author: www.byteblogs.com
* @date : 2023-08-04 12:37
*/
@Configuration
@ComponentScan("com.aizuda.easy.retry.postgres.datasource.*")
@ConditionalOnProperty(prefix = "easy-retry", name = "db-type", havingValue = "postgres")
public class EasyRetryPostgresAutoConfiguration {
}

View File

@ -1 +0,0 @@
com.aizuda.easy.retry.postgres.datasource.config.EasyRetryPostgresAutoConfiguration

View File

@ -1,16 +0,0 @@
package com.aizuda.easy.retry.sqlserver.datasource.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author: www.byteblogs.com
* @date : 2024-03-19 22:05
*/
@Configuration
@ComponentScan("com.aizuda.easy.retry.sqlserver.datasource.*")
@ConditionalOnProperty(prefix = "easy-retry", name = "db-type", havingValue = "sqlserver")
public class EasyRetrySqlServerAutoConfiguration {
}

View File

@ -75,7 +75,10 @@ public class SystemProperties {
/**
* 数据库类型
*
* @deprecated 废弃 新版本通过数据源url自动判断
*/
@Deprecated
private DbTypeEnum dbType = DbTypeEnum.MYSQL;
/**

View File

@ -1,9 +1,8 @@
package com.aizuda.easy.retry.server.common.lock.persistence;
import com.aizuda.easy.retry.common.core.context.SpringContext;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.exception.EasyRetryServerException;
import com.aizuda.easy.retry.server.common.lock.LockProvider;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
import com.aizuda.easy.retry.template.datasource.utils.DbUtils;
import com.google.common.collect.Lists;
import java.util.List;
@ -22,9 +21,9 @@ public final class LockStorageFactory {
}
public static LockStorage getLockStorage() {
SystemProperties systemProperties = SpringContext.getBeanByType(SystemProperties.class);
DbTypeEnum db = DbUtils.getDbType();
return LOCK_STORAGES.stream()
.filter(lockProvider -> lockProvider.supports(systemProperties.getDbType().getDb()))
.filter(lockProvider -> lockProvider.supports(db.getDb()))
.findFirst().orElseThrow(() -> new EasyRetryServerException("未找到合适锁处理器"));
}

View File

@ -6,7 +6,6 @@ import com.aizuda.easy.retry.common.core.model.Result;
import com.aizuda.easy.retry.common.core.util.JsonUtil;
import com.aizuda.easy.retry.common.core.util.NetUtil;
import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.dto.DistributeInstance;
import com.aizuda.easy.retry.server.common.dto.ServerNodeExtAttrs;
import com.aizuda.easy.retry.server.common.enums.DashboardLineEnum;
@ -34,6 +33,7 @@ import com.aizuda.easy.retry.template.datasource.persistence.mapper.JobSummaryMa
import com.aizuda.easy.retry.template.datasource.persistence.mapper.RetrySummaryMapper;
import com.aizuda.easy.retry.template.datasource.persistence.mapper.ServerNodeMapper;
import com.aizuda.easy.retry.template.datasource.persistence.po.ServerNode;
import com.aizuda.easy.retry.template.datasource.utils.DbUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
@ -65,7 +65,6 @@ public class DashBoardServiceImpl implements DashBoardService {
private static final String DASHBOARD_CONSUMER_BUCKET = "/dashboard/consumer/bucket";
private final SystemProperties systemProperties;
private final ServerNodeMapper serverNodeMapper;
private final RestTemplate restTemplate;
private final JobSummaryMapper jobSummaryMapper;
@ -131,7 +130,7 @@ public class DashBoardServiceImpl implements DashBoardService {
// 重试任务列表
Page<Object> pager = new Page<>(baseQueryVO.getPage(), baseQueryVO.getSize());
// 针对SQL Server的分页COUNT, 自定义statement ID
if (DbTypeEnum.SQLSERVER.equals(systemProperties.getDbType())) {
if (DbTypeEnum.SQLSERVER == DbUtils.getDbType()) {
pager.setCountId("sqlServer_jobTaskList_Count");
}
IPage<DashboardRetryLineResponseDO.Task> IPage = retrySummaryMapper.retryTaskList(namespaceId, groupNames, pager);
@ -167,7 +166,7 @@ public class DashBoardServiceImpl implements DashBoardService {
// 重试任务列表
Page<Object> pager = new Page<>(baseQueryVO.getPage(), baseQueryVO.getSize());
// 针对SQL Server的分页COUNT, 自定义statement ID
if (DbTypeEnum.SQLSERVER.equals(systemProperties.getDbType())) {
if (DbTypeEnum.SQLSERVER == DbUtils.getDbType()) {
pager.setCountId("sqlServer_jobTaskList_Count");
}
// 任务类型