Pre Merge pull request !58 from 疯狂的狮子Li/dev_3.2.0

This commit is contained in:
疯狂的狮子Li 2024-03-27 07:07:58 +00:00 committed by Gitee
commit 7b7a6d9532
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
46 changed files with 75 additions and 123 deletions

View File

@ -2,6 +2,9 @@ package com.aizuda.easy.retry.common.core.context;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
@ -15,23 +18,42 @@ import org.springframework.stereotype.Component;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class SpringContext implements ApplicationContextAware {
public class SpringContext implements BeanFactoryPostProcessor, ApplicationContextAware {
public static ApplicationContext CONTEXT;
private static ConfigurableListableBeanFactory FACTORY;
private static ApplicationContext CONTEXT;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
SpringContext.FACTORY = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContext.CONTEXT = applicationContext;
}
public static ListableBeanFactory getBeanFactory() {
final ListableBeanFactory factory = null == FACTORY ? CONTEXT : FACTORY;
if (null == factory) {
throw new RuntimeException("No ConfigurableListableBeanFactory or ApplicationContext injected, maybe not in the Spring environment?");
}
return factory;
}
public static ApplicationContext getContext() {
return CONTEXT;
}
public static <T> T getBeanByType(Class<T> clazz) {
return CONTEXT.getBean(clazz);
return getBeanFactory().getBean(clazz);
}
public static synchronized <T> T getBean(String name) {
try {
return (T) CONTEXT.getBean(name);
return (T) getBeanFactory().getBean(name);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", name, exception.getMessage());
return null;
@ -40,7 +62,7 @@ public class SpringContext implements ApplicationContextAware {
public static synchronized <T> T getBean(Class<T> requiredType) {
try {
return CONTEXT.getBean(requiredType);
return getBeanFactory().getBean(requiredType);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", requiredType.getName(), exception.getMessage());
return null;
@ -49,7 +71,7 @@ public class SpringContext implements ApplicationContextAware {
public static synchronized <T> T getBean(String name, Class<T> requiredType) {
try {
return CONTEXT.getBean(name, requiredType);
return getBeanFactory().getBean(name, requiredType);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", name, exception.getMessage());
return null;

View File

@ -69,7 +69,7 @@ public class LogUtils {
private static Boolean getLogStatus() {
try {
Environment environment = SpringContext.CONTEXT.getBean(Environment.class);
Environment environment = SpringContext.getBean(Environment.class);
return environment.getProperty("easy-retry.log.status", Boolean.class, Boolean.TRUE);
} catch (Exception ignored) {
}

View File

@ -12,6 +12,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;
@ -34,8 +35,6 @@ public abstract class AbstractConfigAccess<T> implements ConfigAccess<T> {
protected SceneConfigMapper sceneConfigMapper;
@Autowired
protected GroupConfigMapper groupConfigMapper;
@Autowired
protected Environment environment;
protected static final List<String> ALLOW_DB = Arrays.asList(
DbTypeEnum.MYSQL.getDb(),
@ -45,8 +44,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

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.template.datasource.access.task;
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;
@ -18,9 +19,6 @@ import java.util.List;
*/
public abstract class AbstractTaskAccess<T> implements TaskAccess<T> {
@Autowired
protected Environment environment;
protected static final List<String> ALLOW_DB = Arrays.asList(
DbTypeEnum.MYSQL.getDb(),
DbTypeEnum.MARIADB.getDb(),
@ -29,8 +27,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

@ -1 +0,0 @@
com.aizuda.easy.retry.sqlserver.datasource.config.EasyRetrySqlServerAutoConfiguration

View File

@ -133,7 +133,7 @@ public class RpcClientInvokeHandler implements InvocationHandler {
Assert.notNull(parasResult.body, () -> new EasyRetryServerException("body cannot be null"));
}
RestTemplate restTemplate = SpringContext.CONTEXT.getBean(RestTemplate.class);
RestTemplate restTemplate = SpringContext.getBean(RestTemplate.class);
Retryer<Result> retryer = buildResultRetryer();
@ -169,7 +169,7 @@ public class RpcClientInvokeHandler implements InvocationHandler {
// 进行路由剔除处理
CacheRegisterTable.remove(groupName, namespaceId, hostId);
// 重新选一个可用的客户端节点
ClientNodeAllocateHandler clientNodeAllocateHandler = SpringContext.CONTEXT.getBean(
ClientNodeAllocateHandler clientNodeAllocateHandler = SpringContext.getBean(
ClientNodeAllocateHandler.class);
RegisterNodeInfo serverNode = clientNodeAllocateHandler.getServerNode(allocKey, groupName, namespaceId,
routeKey);

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

@ -91,7 +91,7 @@ public class JobExecutorActor extends AbstractActor {
} catch (Exception e) {
EasyRetryLog.LOCAL.error("job executor exception. [{}]", taskExecute, e);
handlerTaskBatch(taskExecute, JobTaskBatchStatusEnum.FAIL.getStatus(), JobOperationReasonEnum.TASK_EXECUTION_ERROR.getReason());
SpringContext.CONTEXT.publishEvent(new JobTaskFailAlarmEvent(taskExecute.getTaskBatchId()));
SpringContext.getContext().publishEvent(new JobTaskFailAlarmEvent(taskExecute.getTaskBatchId()));
} finally {
getContext().stop(getSelf());
}

View File

@ -65,7 +65,7 @@ public class JobTaskBatchHandler {
if (failCount > 0) {
jobTaskBatch.setTaskBatchStatus(JobTaskBatchStatusEnum.FAIL.getStatus());
SpringContext.CONTEXT.publishEvent(new JobTaskFailAlarmEvent(completeJobBatchDTO.getTaskBatchId()));
SpringContext.getContext().publishEvent(new JobTaskFailAlarmEvent(completeJobBatchDTO.getTaskBatchId()));
} else if (stopCount > 0) {
jobTaskBatch.setTaskBatchStatus(JobTaskBatchStatusEnum.STOP.getStatus());
} else {

View File

@ -104,7 +104,7 @@ public class RequestHandlerActor extends AbstractActor {
}
UrlBuilder builder = UrlBuilder.ofHttp(uri);
Collection<HttpRequestHandler> httpRequestHandlers = SpringContext.CONTEXT
Collection<HttpRequestHandler> httpRequestHandlers = SpringContext.getContext()
.getBeansOfType(HttpRequestHandler.class).values();
for (HttpRequestHandler httpRequestHandler : httpRequestHandlers) {
if (httpRequestHandler.supports(builder.getPathStr()) && method.name()

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");
}
// 任务类型