shardingsphere-proxy 5.0.0-alpha按時間分片
阿新 • • 發佈:2020-12-17
一、分片類說明
org.apache.shardingsphere.sharding.algorithm.sharding.datetime.AutoIntervalShardingAlgorithm。
在AutoIntervalShardingAlgorithm這個類中有幾個重要的屬性需要在配置檔案中進行配置
public final class AutoIntervalShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>>, ShardingAutoTableAlgorithm { // 時間下限 分片的開始時間 private static final String DATE_TIME_LOWER_KEY = "datetime-lower"; // 時間上限 分片的結束時間 private static final String DATE_TIME_UPPER_KEY = "datetime-upper"; // 按時間分片的秒數 一天一張表就是86400秒 private static final String SHARDING_SECONDS_KEY = "sharding-seconds"; // 時間格式 private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
二、單庫分表關鍵配置說明
其他配置這裡不贅述,重點是加註釋的配置。
config-sharding.yaml
schemaName: logical_shardingsphere dataSourceCommon: connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 maintenanceIntervalMilliseconds: 30000 dataSources: ds_0: url: jdbc:mysql://127.0.0.1:3306/sharding1?serverTimezone=UTC&useSSL=false username: root password: root rules: - !SHARDING tables: user: actualDataNodes: ds_0.user_${0..4} tableStrategy: standard: # 分片列名 shardingColumn: create_time # 分片演算法名稱 shardingAlgorithmName: sharding_by_time keyGenerateStrategy: column: id keyGeneratorName: snowflake defaultDatabaseStrategy: none: defaultTableStrategy: none: shardingAlgorithms: sharding_by_time: # 分片型別 type: auto_interval props: # 分片開始時間 要加""不然會被解析為非字串型別導致拿不到值 datetime-lower: "2020-10-01 00:00:00" # 分片秒數 這裡是一天一張表 所以秒數為86400秒 要加""不然會被解析為非字串型別導致拿不到值 sharding-seconds: "86400" # 分片結束時間 要加""不然會被解析為非字串型別導致拿不到值 datetime-upper: "2020-10-05 00:00:00" keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 1
從2020-10-01 00:00:00開始分片每86400秒(一天)分成一片
實際建表5張
3、測試
向邏輯表中插入以下資料
進入對應的分片
為了方便在本地進行除錯,自己fork了一個倉庫,加了一點註釋。
程式碼地址:https://github.com/zhangdj-study/shardingsphere/tree/5.0.0-alpha-own
分支:5.0.0-alpha-own