diff --git a/fastbee-admin/src/main/resources/application-dev.yml b/fastbee-admin/src/main/resources/application-dev.yml index 53d62bc0..f43efce0 100644 --- a/fastbee-admin/src/main/resources/application-dev.yml +++ b/fastbee-admin/src/main/resources/application-dev.yml @@ -44,56 +44,58 @@ spring: # url: jdbc:mysql://1.116.124.85:3306/fastbee1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # username: root # password: fastbee - shardingsphere: - # props: - # 是否显示 ShardingSpher 的sql,用于Debug - # sql-show: true - datasource: - # 配置真实数据源 - names: ds0 - ds0: # 配置 mysql 数据源 - type: com.alibaba.druid.pool.DruidDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: root - password: fastbee - filters: stat,wall - filter: - stat: - enabled: true - # 慢SQL记录 - log-slow-sql: true - slow-sql-millis: 1000 - merge-sql: true - wall: - config: - multi-statement-allow: true - - rules: # 配置表规则 - sharding: - # 表策略配置 - tables: - # iot_device_log 是逻辑表 - iot_device_log: - actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} - tableStrategy: - # 使用标准分片策略 - standard: - # 配置分片字段 - shardingColumn: create_time - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - shardingAlgorithmName: time-sharding-algorithm - # 分片算法配置 - shardingAlgorithms: - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - time-sharding-algorithm: - # 类型:自定义策略 - type: CLASS_BASED - props: - # 分片策略 - strategy: standard - # 分片算法类 - algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm + # ===== ShardingSphere 分库分表配置(已禁用)===== + # 如需启用分库分表,请取消以下注释 + # shardingsphere: + # # props: + # # 是否显示 ShardingSpher 的sql,用于Debug + # # sql-show: true + # datasource: + # # 配置真实数据源 + # names: ds0 + # ds0: # 配置 mysql 数据源 + # type: com.alibaba.druid.pool.DruidDataSource + # driver-class-name: com.mysql.cj.jdbc.Driver + # url: jdbc:mysql://localhost:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + # username: root + # password: fastbee + # filters: stat,wall + # filter: + # stat: + # enabled: true + # # 慢SQL记录 + # log-slow-sql: true + # slow-sql-millis: 1000 + # merge-sql: true + # wall: + # config: + # multi-statement-allow: true + # + # rules: # 配置表规则 + # sharding: + # # 表策略配置 + # tables: + # # iot_device_log 是逻辑表 + # iot_device_log: + # actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} + # tableStrategy: + # # 使用标准分片策略 + # standard: + # # 配置分片字段 + # shardingColumn: create_time + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # shardingAlgorithmName: time-sharding-algorithm + # # 分片算法配置 + # shardingAlgorithms: + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # time-sharding-algorithm: + # # 类型:自定义策略 + # type: CLASS_BASED + # props: + # # 分片策略 + # strategy: standard + # # 分片算法类 + # algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm # redis 配置 diff --git a/fastbee-admin/src/main/resources/application-prod.yml b/fastbee-admin/src/main/resources/application-prod.yml index e85c8171..c936a670 100644 --- a/fastbee-admin/src/main/resources/application-prod.yml +++ b/fastbee-admin/src/main/resources/application-prod.yml @@ -44,56 +44,58 @@ spring: # url: jdbc:mysql://localhost:3306/fastbee1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # username: root # password: fastbee - shardingsphere: - # props: - # 是否显示 ShardingSpher 的sql,用于Debug - # sql-show: true - datasource: - # 配置真实数据源 - names: ds0 - ds0: # 配置 mysql 数据源 - type: com.alibaba.druid.pool.DruidDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://177.7.0.11:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: root - password: fastbee - filters: stat,wall - filter: - stat: - enabled: true - # 慢SQL记录 - log-slow-sql: true - slow-sql-millis: 1000 - merge-sql: true - wall: - config: - multi-statement-allow: true - - rules: # 配置表规则 - sharding: - # 表策略配置 - tables: - # iot_device_log 是逻辑表 - iot_device_log: - actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} - tableStrategy: - # 使用标准分片策略 - standard: - # 配置分片字段 - shardingColumn: create_time - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - shardingAlgorithmName: time-sharding-algorithm - # 分片算法配置 - shardingAlgorithms: - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - time-sharding-algorithm: - # 类型:自定义策略 - type: CLASS_BASED - props: - # 分片策略 - strategy: standard - # 分片算法类 - algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm + # ===== ShardingSphere 分库分表配置(已禁用)===== + # 如需启用分库分表,请取消以下注释 + # shardingsphere: + # # props: + # # 是否显示 ShardingSpher 的sql,用于Debug + # # sql-show: true + # datasource: + # # 配置真实数据源 + # names: ds0 + # ds0: # 配置 mysql 数据源 + # type: com.alibaba.druid.pool.DruidDataSource + # driver-class-name: com.mysql.cj.jdbc.Driver + # url: jdbc:mysql://177.7.0.11:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + # username: root + # password: fastbee + # filters: stat,wall + # filter: + # stat: + # enabled: true + # # 慢SQL记录 + # log-slow-sql: true + # slow-sql-millis: 1000 + # merge-sql: true + # wall: + # config: + # multi-statement-allow: true + # + # rules: # 配置表规则 + # sharding: + # # 表策略配置 + # tables: + # # iot_device_log 是逻辑表 + # iot_device_log: + # actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} + # tableStrategy: + # # 使用标准分片策略 + # standard: + # # 配置分片字段 + # shardingColumn: create_time + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # shardingAlgorithmName: time-sharding-algorithm + # # 分片算法配置 + # shardingAlgorithms: + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # time-sharding-algorithm: + # # 类型:自定义策略 + # type: CLASS_BASED + # props: + # # 分片策略 + # strategy: standard + # # 分片算法类 + # algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm # redis 配置 redis: host: 177.7.0.10 # 地址 diff --git a/fastbee-admin/src/main/resources/application-sql.yml b/fastbee-admin/src/main/resources/application-sql.yml index d02d4fa6..ab8216ed 100644 --- a/fastbee-admin/src/main/resources/application-sql.yml +++ b/fastbee-admin/src/main/resources/application-sql.yml @@ -71,56 +71,58 @@ spring: # username: root # password: fastbee - shardingsphere: - props: - # 是否显示 ShardingSpher 的sql,用于Debug - sql-show: true - datasource: - # 配置真实数据源 - names: ds0 - ds0: # 配置 mysql 数据源 - type: com.alibaba.druid.pool.DruidDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: root - password: fastbee - filters: stat,wall - filter: - stat: - enabled: true - # 慢SQL记录 - log-slow-sql: true - slow-sql-millis: 1000 - merge-sql: true - wall: - config: - multi-statement-allow: true - - rules: # 配置表规则 - sharding: - # 表策略配置 - tables: - # iot_device_log 是逻辑表 - iot_device_log: - actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} - tableStrategy: - # 使用标准分片策略 - standard: - # 配置分片字段 - shardingColumn: create_time - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - shardingAlgorithmName: time-sharding-algorithm - # 分片算法配置 - shardingAlgorithms: - # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 - time-sharding-algorithm: - # 类型:自定义策略 - type: CLASS_BASED - props: - # 分片策略 - strategy: standard - # 分片算法类 - algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm + # ===== ShardingSphere 分库分表配置(已禁用)===== + # 如需启用分库分表,请取消以下注释 + # shardingsphere: + # props: + # # 是否显示 ShardingSpher 的sql,用于Debug + # sql-show: true + # datasource: + # # 配置真实数据源 + # names: ds0 + # ds0: # 配置 mysql 数据源 + # type: com.alibaba.druid.pool.DruidDataSource + # driver-class-name: com.mysql.cj.jdbc.Driver + # url: jdbc:mysql://localhost:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + # username: root + # password: fastbee + # filters: stat,wall + # filter: + # stat: + # enabled: true + # # 慢SQL记录 + # log-slow-sql: true + # slow-sql-millis: 1000 + # merge-sql: true + # wall: + # config: + # multi-statement-allow: true + # + # rules: # 配置表规则 + # sharding: + # # 表策略配置 + # tables: + # # iot_device_log 是逻辑表 + # iot_device_log: + # actualDataNodes: ds0.iot_device_log_$->{2024..2030}0$->{1..9},ds0.iot_device_log_$->{2024..2030}1$->{0..2} + # tableStrategy: + # # 使用标准分片策略 + # standard: + # # 配置分片字段 + # shardingColumn: create_time + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # shardingAlgorithmName: time-sharding-algorithm + # # 分片算法配置 + # shardingAlgorithms: + # # 分片算法名称,不支持大写字母和下划线,否则启动就会报错 + # time-sharding-algorithm: + # # 类型:自定义策略 + # type: CLASS_BASED + # props: + # # 分片策略 + # strategy: standard + # # 分片算法类 + # algorithmClassName: com.fastbee.framework.config.sharding.TimeShardingAlgorithm # redis 配置 redis: diff --git a/fastbee-framework/pom.xml b/fastbee-framework/pom.xml index 4dc2e15f..7f86f04d 100644 --- a/fastbee-framework/pom.xml +++ b/fastbee-framework/pom.xml @@ -42,18 +42,24 @@ ${druid.version} + + + - + + diff --git a/fastbee-framework/src/main/java/com/fastbee/framework/config/DataSourceConfig.java b/fastbee-framework/src/main/java/com/fastbee/framework/config/DataSourceConfig.java index f9196162..7ec1c02b 100644 --- a/fastbee-framework/src/main/java/com/fastbee/framework/config/DataSourceConfig.java +++ b/fastbee-framework/src/main/java/com/fastbee/framework/config/DataSourceConfig.java @@ -7,7 +7,7 @@ import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider; import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider; import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration; import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties; -import org.springframework.beans.factory.annotation.Qualifier; +// import org.springframework.beans.factory.annotation.Qualifier; // ShardingSphere 已禁用 import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -28,14 +28,17 @@ public class DataSourceConfig { private final DynamicDataSourceProperties properties; private final DefaultDataSourceCreator dataSourceCreator; - private final DataSource shardingSphereDataSource; + // ===== ShardingSphere 已禁用 ===== + // private final DataSource shardingSphereDataSource; public DataSourceConfig(DynamicDataSourceProperties properties, - DefaultDataSourceCreator dataSourceCreator, - @Qualifier("shardingSphereDataSource") DataSource shardingSphereDataSource) { + DefaultDataSourceCreator dataSourceCreator + // ===== ShardingSphere 已禁用 ===== + // @Qualifier("shardingSphereDataSource") DataSource shardingSphereDataSource + ) { this.properties = properties; this.dataSourceCreator = dataSourceCreator; - this.shardingSphereDataSource = shardingSphereDataSource; + // this.shardingSphereDataSource = shardingSphereDataSource; } @Bean(name = "masterDataSource") @@ -56,8 +59,9 @@ public class DataSourceConfig { @Override public Map loadDataSources() { Map dataSourceMap = new HashMap<>(); + // ===== ShardingSphere 已禁用 ===== // 把 shardingSphereDataSource 加入多数据源,到时候使用的时候就可以 `@DS("shardingSphere")` - dataSourceMap.put("shardingSphere", shardingSphereDataSource); + // dataSourceMap.put("shardingSphere", shardingSphereDataSource); return dataSourceMap; } }; diff --git a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingAlgorithmTool.java b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingAlgorithmTool.java index 7da965f8..b8dbf9ac 100644 --- a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingAlgorithmTool.java +++ b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingAlgorithmTool.java @@ -1,5 +1,12 @@ package com.fastbee.framework.config.sharding; +/** + * ===== ShardingSphere 已禁用 ===== + * 如需启用分库分表,请取消以下注释并恢复 pom.xml 中的依赖 + */ + +/* + import cn.hutool.extra.spring.SpringUtil; import com.alibaba.druid.util.StringUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; @@ -18,152 +25,80 @@ import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; -/** - *

@Title ShardingAlgorithmTool - *

@Description 按月分片算法工具 - */ @Slf4j public class ShardingAlgorithmTool { - /** - * 表分片符号,例:siot_device_log_202201 中,分片符号为 "_" - */ private static final String TABLE_SPLIT_SYMBOL = "_"; - - /** - * 数据库配置 - */ private static final Environment ENV = SpringUtil.getApplicationContext().getEnvironment(); private static final String DATASOURCE_URL = ENV.getProperty("spring.shardingsphere.datasource.ds0.url"); private static final String DATASOURCE_USERNAME = ENV.getProperty("spring.shardingsphere.datasource.ds0.username"); private static final String DATASOURCE_PASSWORD = ENV.getProperty("spring.shardingsphere.datasource.ds0.password"); - - /** - * 检查分表获取的表名是否存在,不存在则自动建表 - * - * @param logicTable 逻辑表 - * @param resultTableNames 真实表名,例:iot_device_log_202201 - * @return 存在于数据库中的真实表名集合 - */ public static Set getShardingTablesAndCreate(ShardingTableCacheEnum logicTable, Collection resultTableNames) { return resultTableNames.stream().map(o -> getShardingTableAndCreate(logicTable, o)).collect(Collectors.toSet()); } - /** - * 检查分表获取的表名是否存在,不存在则自动建表 - * - * @param logicTable 逻辑表 - * @param resultTableName 真实表名,例:iot_device_log_202201 - * @return 确认存在于数据库中的真实表名 - */ public static String getShardingTableAndCreate(ShardingTableCacheEnum logicTable, String resultTableName) { - // 缓存中有此表则返回,没有则判断创建 if (logicTable.resultTableNamesCache().contains(resultTableName)) { return resultTableName; } else { - // 未创建的表返回逻辑空表 boolean isSuccess = createShardingTable(logicTable, resultTableName); return isSuccess ? resultTableName : logicTable.logicTableName(); } } - /** - * 重载全部缓存 - */ public static void tableNameCacheReloadAll() { Arrays.stream(ShardingTableCacheEnum.values()).forEach(ShardingAlgorithmTool::tableNameCacheReload); } - /** - * 重载指定分表缓存 - * - * @param logicTable 逻辑表 - */ public static void tableNameCacheReload(ShardingTableCacheEnum logicTable) { - // 读取数据库中所有表名 List tableNameList = getAllTableNameBySchema(logicTable); - // 更新缓存、配置(原子操作) logicTable.atomicUpdateCacheAndActualDataNodes(tableNameList); - // 删除旧的缓存(如果存在) logicTable.resultTableNamesCache().clear(); - // 写入新的缓存 logicTable.resultTableNamesCache().addAll(tableNameList); - // 动态更新配置 actualDataNodes actualDataNodesRefresh(logicTable.logicTableName(), tableNameList); } - /** - * 获取所有表名 - * - * @param logicTable 逻辑表 - * @return 表名集合 - */ public static List getAllTableNameBySchema(ShardingTableCacheEnum logicTable) { List tableNames = new ArrayList<>(); if (StringUtils.isEmpty(DATASOURCE_URL) || StringUtils.isEmpty(DATASOURCE_USERNAME) || StringUtils.isEmpty(DATASOURCE_PASSWORD)) { - log.error(">>>>>>>>>> 【ERROR】数据库连接配置有误,请稍后重试,URL:{}, username:{}, password:{}", DATASOURCE_URL, DATASOURCE_USERNAME, DATASOURCE_PASSWORD); - throw new IllegalArgumentException("数据库连接配置有误,请稍后重试"); + log.error(">>>>>>>>>> 【ERROR】数据库连接配置有误"); + throw new IllegalArgumentException("数据库连接配置有误"); } try (Connection conn = DriverManager.getConnection(DATASOURCE_URL, DATASOURCE_USERNAME, DATASOURCE_PASSWORD); Statement st = conn.createStatement()) { String logicTableName = logicTable.logicTableName(); try (ResultSet rs = st.executeQuery("show TABLES like '" + logicTableName + TABLE_SPLIT_SYMBOL + "%'")) { - log.info("查询数据库所有表:{}", "show TABLES like '" + logicTableName + TABLE_SPLIT_SYMBOL + "%'"); while (rs.next()) { String tableName = rs.getString(1); - log.info("分表格式:{}", String.format("^(%s\\d{6})$", logicTableName + TABLE_SPLIT_SYMBOL)); - // 匹配分表格式 例:^(t\_contract_\d{6})$ if (org.apache.commons.lang3.StringUtils.isNotBlank(tableName) && tableName.matches(String.format("^(%s\\d{6})$", logicTableName + TABLE_SPLIT_SYMBOL))) { tableNames.add(rs.getString(1)); } } } } catch (SQLException e) { - log.error(">>>>>>>>>> 【ERROR】数据库连接失败,请稍后重试,原因:{}", e.getMessage(), e); - throw new IllegalArgumentException("数据库连接失败,请稍后重试"); + log.error(">>>>>>>>>> 【ERROR】数据库连接失败", e); + throw new IllegalArgumentException("数据库连接失败"); } return tableNames; } - /** - * 动态更新配置 actualDataNodes - * - * @param logicTableName 逻辑表名 - * @param tableNamesCache 真实表名集合 - */ public static void actualDataNodesRefresh(String logicTableName, List tableNamesCache) { try { if (CollectionUtils.isEmpty(tableNamesCache)) { return; } - // 获取数据分片节点 String dbName = "ds0"; - log.info(">>>>>>>>>> 【INFO】更新分表配置,logicTableName:{},tableNamesCache:{}", logicTableName, tableNamesCache); - - // generate actualDataNodes String newActualDataNodes = tableNamesCache.stream().map(o -> String.format("%s.%s", dbName, o)).collect(Collectors.joining(",")); ShardingSphereDataSource shardingSphereDataSource = SpringUtil.getBean(ShardingSphereDataSource.class); updateShardRuleActualDataNodes(shardingSphereDataSource, logicTableName, newActualDataNodes); } catch (Exception e) { - log.error("初始化 动态表单失败,原因:{}", e.getMessage(), e); + log.error("初始化 动态表单失败", e); } } - - // -------------------------------------------------------------------------------------------------------------- - // 私有方法 - // -------------------------------------------------------------------------------------------------------------- - - - /** - * 刷新ActualDataNodes - */ private static void updateShardRuleActualDataNodes(ShardingSphereDataSource dataSource, String logicTableName, String newActualDataNodes) { - // Context manager. ContextManager contextManager = dataSource.getContextManager(); - - // Rule configuration. String schemaName = "logic_db"; Collection newRuleConfigList = new LinkedList<>(); Collection oldRuleConfigList = dataSource.getContextManager() @@ -174,12 +109,8 @@ public class ShardingAlgorithmTool { for (RuleConfiguration oldRuleConfig : oldRuleConfigList) { if (oldRuleConfig instanceof AlgorithmProvidedShardingRuleConfiguration) { - - // Algorithm provided sharding rule configuration AlgorithmProvidedShardingRuleConfiguration oldAlgorithmConfig = (AlgorithmProvidedShardingRuleConfiguration) oldRuleConfig; AlgorithmProvidedShardingRuleConfiguration newAlgorithmConfig = new AlgorithmProvidedShardingRuleConfiguration(); - - // Sharding table rule configuration Collection Collection newTableRuleConfigList = new LinkedList<>(); Collection oldTableRuleConfigList = oldAlgorithmConfig.getTables(); @@ -189,7 +120,6 @@ public class ShardingAlgorithmTool { newTableRuleConfig.setTableShardingStrategy(oldTableRuleConfig.getTableShardingStrategy()); newTableRuleConfig.setDatabaseShardingStrategy(oldTableRuleConfig.getDatabaseShardingStrategy()); newTableRuleConfig.setKeyGenerateStrategy(oldTableRuleConfig.getKeyGenerateStrategy()); - newTableRuleConfigList.add(newTableRuleConfig); } else { newTableRuleConfigList.add(oldTableRuleConfig); @@ -209,48 +139,28 @@ public class ShardingAlgorithmTool { newRuleConfigList.add(newAlgorithmConfig); } } - - // update context contextManager.alterRuleConfiguration(schemaName, newRuleConfigList); } - /** - * 创建分表 - * - * @param logicTable 逻辑表 - * @param resultTableName 真实表名,例:sys_user_behavior_202201 - * @return 创建结果(true创建成功,false未创建) - */ private static boolean createShardingTable(ShardingTableCacheEnum logicTable, String resultTableName) { - // 根据日期判断,当前月份之后分表不提前创建 String month = resultTableName.replace(logicTable.logicTableName() + TABLE_SPLIT_SYMBOL, ""); YearMonth shardingMonth = YearMonth.parse(month, DateTimeFormatter.ofPattern("yyyyMM")); if (shardingMonth.isAfter(YearMonth.now())) { return false; } - synchronized (logicTable.logicTableName().intern()) { - // 缓存中有此表 返回 if (logicTable.resultTableNamesCache().contains(resultTableName)) { return false; } - // 缓存中无此表,则建表并添加缓存 executeSql(Collections.singletonList("CREATE TABLE IF NOT EXISTS `" + resultTableName + "` LIKE `" + logicTable.logicTableName() + "`;")); - // 缓存重载 tableNameCacheReload(logicTable); } return true; } - /** - * 执行SQL - * - * @param sqlList SQL集合 - */ private static void executeSql(List sqlList) { if (StringUtils.isEmpty(DATASOURCE_URL) || StringUtils.isEmpty(DATASOURCE_USERNAME) || StringUtils.isEmpty(DATASOURCE_PASSWORD)) { - log.error(">>>>>>>>>> 【ERROR】数据库连接配置有误,请稍后重试,URL:{}, username:{}, password:{}", DATASOURCE_URL, DATASOURCE_USERNAME, DATASOURCE_PASSWORD); - throw new IllegalArgumentException("数据库连接配置有误,请稍后重试"); + throw new IllegalArgumentException("数据库连接配置有误"); } try (Connection conn = DriverManager.getConnection(DATASOURCE_URL, DATASOURCE_USERNAME, DATASOURCE_PASSWORD)) { try (Statement st = conn.createStatement()) { @@ -260,12 +170,12 @@ public class ShardingAlgorithmTool { } } catch (Exception e) { conn.rollback(); - log.error(">>>>>>>>>> 【ERROR】数据表创建执行失败,请稍后重试,原因:{}", e.getMessage(), e); - throw new IllegalArgumentException("数据表创建执行失败,请稍后重试"); + throw new IllegalArgumentException("数据表创建执行失败"); } } catch (SQLException e) { - log.error(">>>>>>>>>> 【ERROR】数据库连接失败,请稍后重试,原因:{}", e.getMessage(), e); - throw new IllegalArgumentException("数据库连接失败,请稍后重试"); + throw new IllegalArgumentException("数据库连接失败"); } } } + +*/ diff --git a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingTablesLoadRunner.java b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingTablesLoadRunner.java index 3ad093bc..49d96313 100644 --- a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingTablesLoadRunner.java +++ b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/ShardingTablesLoadRunner.java @@ -1,20 +1,25 @@ package com.fastbee.framework.config.sharding; +/** + * ===== ShardingSphere 已禁用 ===== + * 如需启用分库分表,请取消以下注释并恢复 pom.xml 中的依赖 + */ + +/* + import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -/** - *

@Title ShardingTablesLoadRunner - *

@Description 项目启动后,读取已有分表,进行缓存 - */ -@Order(value = 1) // 数字越小,越先执行 +@Order(value = 1) @Component public class ShardingTablesLoadRunner implements CommandLineRunner { @Override public void run(String... args) { // 读取已有分表,进行缓存 - //ShardingAlgorithmTool.tableNameCacheReloadAll(); + // ShardingAlgorithmTool.tableNameCacheReloadAll(); } } + +*/ diff --git a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/TimeShardingAlgorithm.java b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/TimeShardingAlgorithm.java index 8e2616d3..31141458 100644 --- a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/TimeShardingAlgorithm.java +++ b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/TimeShardingAlgorithm.java @@ -1,5 +1,12 @@ package com.fastbee.framework.config.sharding; +/** + * ===== ShardingSphere 已禁用 ===== + * 如需启用分库分表,请取消以下注释并恢复 pom.xml 中的依赖 + */ + +/* + import com.fastbee.framework.config.sharding.enums.ShardingTableCacheEnum; import com.google.common.collect.Range; import lombok.extern.slf4j.Slf4j; @@ -17,91 +24,44 @@ import java.time.format.DateTimeFormatter; import java.util.*; import java.util.function.Function; -/** - *

@Title TimeShardingAlgorithm - *

@Description 分片算法,按月分片 - */ @Slf4j public class TimeShardingAlgorithm implements StandardShardingAlgorithm { - /** - * Date类型的分片时间格式 - */ + private static final SimpleDateFormat TABLE_SHARD_Date_FORMATTER = new SimpleDateFormat("yyyyMM"); - - /** - * 分片时间格式 - */ private static final DateTimeFormatter TABLE_SHARD_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM"); - - /** - * 完整时间格式 - */ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"); - /** - * 完整时间格式 - */ private static final SimpleDateFormat DATE_TIME_FORMATTER_SPILE = new SimpleDateFormat("yyyy-MM-dd"); - - /** - * 表分片符号,例:t_user_202201 中,分片符号为 "_" - */ private final String TABLE_SPLIT_SYMBOL = "_"; - - /** - * 精准分片 - * - * @param tableNames 对应分片库中所有分片表的集合 - * @param preciseShardingValue 分片键值,其中 logicTableName 为逻辑表,columnName 分片键,value 为从 SQL 中解析出来的分片键的值 - * @return 表名 - */ @Override public String doSharding(Collection tableNames, PreciseShardingValue preciseShardingValue) { String logicTableName = preciseShardingValue.getLogicTableName(); ShardingTableCacheEnum logicTable = ShardingTableCacheEnum.of(logicTableName); createAllTable(logicTable, tableNames); - - /// 打印分片信息 log.info(">>>>>>>>>> 【INFO】精确分片,节点配置表名:{},数据库缓存表名:{}", tableNames, logicTable.resultTableNamesCache()); - Date date = preciseShardingValue.getValue(); Instant instant = date.toInstant(); LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); String resultTableName = logicTableName + "_" + TABLE_SHARD_TIME_FORMATTER.format(localDateTime); - // 检查分表获取的表名是否存在,不存在则自动建表 if (!tableNames.contains(resultTableName)) { tableNames.add(resultTableName); } return ShardingAlgorithmTool.getShardingTableAndCreate(logicTable, resultTableName); } - /** - * 范围分片 - * - * @param tableNames 对应分片库中所有分片表的集合 - * @param rangeShardingValue 分片范围 - * @return 表名集合 - */ @Override public Collection doSharding(Collection tableNames, RangeShardingValue rangeShardingValue) { log.info("开始分表查询开始:{}", System.currentTimeMillis()); String logicTableName = rangeShardingValue.getLogicTableName(); ShardingTableCacheEnum logicTable = ShardingTableCacheEnum.of(logicTableName); createAllTable(logicTable, tableNames); - - /// 打印分片信息 log.info(">>>>>>>>>> 【INFO】范围分片,节点配置表名:{},数据库缓存表名:{}", tableNames, logicTable.resultTableNamesCache()); - - // between and 的起始值 Range valueRange = rangeShardingValue.getValueRange(); boolean hasLowerBound = valueRange.hasLowerBound(); boolean hasUpperBound = valueRange.hasUpperBound(); - - // 获取最大值和最小值 Set tableNameCache = logicTable.resultTableNamesCache(); String min = hasLowerBound ? String.valueOf(valueRange.lowerEndpoint()) : getLowerEndpoint(tableNameCache); String max = hasUpperBound ? String.valueOf(valueRange.upperEndpoint()) : getUpperEndpoint(tableNameCache); - // 循环计算分表范围 Set resultTableNames = new LinkedHashSet<>(); try { Date minDate = DATE_TIME_FORMATTER_SPILE.parse(min); @@ -110,8 +70,8 @@ public class TimeShardingAlgorithm implements StandardShardingAlgorithm { while (minDate.before(maxDate) || minDate.equals(maxDate)) { String tableName = logicTableName + TABLE_SPLIT_SYMBOL + TABLE_SHARD_Date_FORMATTER.format(minDate); resultTableNames.add(tableName); - calendar.setTime(minDate); // 设置Calendar的时间为Date对象的时间 - calendar.add(Calendar.DAY_OF_MONTH, 1); // 给日期加一天 + calendar.setTime(minDate); + calendar.add(Calendar.DAY_OF_MONTH, 1); minDate = calendar.getTime(); } log.info("开始分表查询结束:{}", System.currentTimeMillis()); @@ -121,27 +81,12 @@ public class TimeShardingAlgorithm implements StandardShardingAlgorithm { } } + @Override + public void init() {} @Override - public void init() { + public String getType() { return null; } - } - - @Override - public String getType() { - return null; - } - - // -------------------------------------------------------------------------------------------------------------- - // 私有方法 - // -------------------------------------------------------------------------------------------------------------- - - /** - * 获取 最小分片值 - * - * @param tableNames 表名集合 - * @return 最小分片值 - */ private String getLowerEndpoint(Collection tableNames) { Optional optional = tableNames.stream() .map(o -> LocalDateTime.parse(o.replace(TABLE_SPLIT_SYMBOL, "") + "01 00:00:00", DATE_TIME_FORMATTER)) @@ -156,12 +101,6 @@ public class TimeShardingAlgorithm implements StandardShardingAlgorithm { } } - /** - * 获取 最大分片值 - * - * @param tableNames 表名集合 - * @return 最大分片值 - */ private String getUpperEndpoint(Collection tableNames) { Optional optional = tableNames.stream() .map(o -> LocalDateTime.parse(o.replace(TABLE_SPLIT_SYMBOL, "") + "01 00:00:00", DATE_TIME_FORMATTER)) @@ -176,20 +115,13 @@ public class TimeShardingAlgorithm implements StandardShardingAlgorithm { } } - /** - * 根据分片规则获取的表,创建所有的表 - * - * @param logicTable - * @param tableNames - */ private void createAllTable(ShardingTableCacheEnum logicTable, Collection tableNames) { if (!CollectionUtils.isEmpty(logicTable.resultTableNamesCache())) { - //如果缓存中有表了,则证明已经创建了表,无需再创建 return; } - //根据分片规则创建表 ShardingAlgorithmTool.getShardingTablesAndCreate(logicTable, tableNames); - //刷新缓存 ShardingAlgorithmTool.tableNameCacheReload(logicTable); } } + +*/ diff --git a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/enums/ShardingTableCacheEnum.java b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/enums/ShardingTableCacheEnum.java index 2fa0cad0..cf3d2c47 100644 --- a/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/enums/ShardingTableCacheEnum.java +++ b/fastbee-framework/src/main/java/com/fastbee/framework/config/sharding/enums/ShardingTableCacheEnum.java @@ -1,21 +1,20 @@ package com.fastbee.framework.config.sharding.enums; +/** + * ===== ShardingSphere 已禁用 ===== + * 如需启用分库分表,请取消以下注释并恢复 pom.xml 中的依赖 + */ + +/* + import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import java.util.*; import static com.fastbee.framework.config.sharding.ShardingAlgorithmTool.actualDataNodesRefresh; - -/** - *

@Title ShardingTableCacheEnum - *

@Description 分片表缓存枚举 - */ public enum ShardingTableCacheEnum { - /** - * 用户埋点表 - */ DEVICE_LOG("iot_device_log", new HashSet<>()); private static Map valueMap = new HashMap<>(); @@ -24,13 +23,7 @@ public enum ShardingTableCacheEnum { Arrays.stream(ShardingTableCacheEnum.values()).forEach(o -> valueMap.put(o.logicTableName, o)); } - /** - * 逻辑表名 - */ private final String logicTableName; - /** - * 实际表名 - */ private final Set resultTableNamesCache; ShardingTableCacheEnum(String logicTableName, Set resultTableNamesCache) { @@ -54,21 +47,13 @@ public enum ShardingTableCacheEnum { return resultTableNamesCache; } - /** - * 更新缓存、配置(原子操作) - * - * @param tableNameList - */ public void atomicUpdateCacheAndActualDataNodes(List tableNameList) { if (CollectionUtils.isEmpty(tableNameList)) { return; } synchronized (resultTableNamesCache) { - // 删除缓存 resultTableNamesCache.clear(); - // 写入新的缓存 resultTableNamesCache.addAll(tableNameList); - // 动态更新配置 actualDataNodes actualDataNodesRefresh(logicTableName, tableNameList); } } @@ -81,3 +66,5 @@ public enum ShardingTableCacheEnum { '}'; } } + +*/ diff --git a/pom.xml b/pom.xml index 2121cfc8..9067de1d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,7 @@ 2.2.7 3.3.3 1.2.23 - 5.1.1 + 4.3.1 3.9.1 3.3.2 @@ -78,11 +78,14 @@ ${p6spy.version} + +