From ecfac23b11f225fb98db72452d3ee666e23e5364 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Tue, 26 Mar 2019 17:17:43 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-sharding-jdbc=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-sharding-jdbc/README.md | 48 ++++++++++++++++++- .../config/CustomSnowflakeKeyGenerator.java | 30 ++++++++++++ .../jdbc/config/DataSourceShardingConfig.java | 15 +++++- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/CustomSnowflakeKeyGenerator.java diff --git a/spring-boot-demo-sharding-jdbc/README.md b/spring-boot-demo-sharding-jdbc/README.md index 01d7135..c445002 100644 --- a/spring-boot-demo-sharding-jdbc/README.md +++ b/spring-boot-demo-sharding-jdbc/README.md @@ -97,7 +97,42 @@ ``` -### 2.2. `DataSourceShardingConfig.java` +### 2.2. `CustomSnowflakeKeyGenerator.java` + +```java +package com.xkcoding.sharding.jdbc.config; + +import cn.hutool.core.lang.Snowflake; +import io.shardingsphere.core.keygen.KeyGenerator; + +/** + *

+ * 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 + *

+ * + * @package: com.xkcoding.sharding.jdbc.config + * @description: 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 + * @author: yangkai.shen + * @date: Created in 2019-03-26 17:07 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +public class CustomSnowflakeKeyGenerator implements KeyGenerator { + private Snowflake snowflake; + + public CustomSnowflakeKeyGenerator(Snowflake snowflake) { + this.snowflake = snowflake; + } + + @Override + public Number generateKey() { + return snowflake.nextId(); + } +} +``` + +### 2.3. `DataSourceShardingConfig.java` ```java /** @@ -115,6 +150,8 @@ */ @Configuration public class DataSourceShardingConfig { + private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1); + /** * 需要手动配置事务管理器 */ @@ -149,7 +186,7 @@ public class DataSourceShardingConfig { // ds${0..1}.t_order_${0..2} 也可以写成 ds$->{0..1}.t_order_$->{0..1} tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}"); tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}")); - tableRule.setKeyGenerator(new DefaultKeyGenerator()); + tableRule.setKeyGenerator(customKeyGenerator()); tableRule.setKeyGeneratorColumnName("order_id"); return tableRule; } @@ -176,6 +213,13 @@ public class DataSourceShardingConfig { return dataSourceMap; } + /** + * 自定义主键生成器 + */ + private KeyGenerator customKeyGenerator() { + return new CustomSnowflakeKeyGenerator(snowflake); + } + } ``` diff --git a/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/CustomSnowflakeKeyGenerator.java b/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/CustomSnowflakeKeyGenerator.java new file mode 100644 index 0000000..1ef1d11 --- /dev/null +++ b/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/CustomSnowflakeKeyGenerator.java @@ -0,0 +1,30 @@ +package com.xkcoding.sharding.jdbc.config; + +import cn.hutool.core.lang.Snowflake; +import io.shardingsphere.core.keygen.KeyGenerator; + +/** + *

+ * 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 + *

+ * + * @package: com.xkcoding.sharding.jdbc.config + * @description: 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 + * @author: yangkai.shen + * @date: Created in 2019-03-26 17:07 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +public class CustomSnowflakeKeyGenerator implements KeyGenerator { + private Snowflake snowflake; + + public CustomSnowflakeKeyGenerator(Snowflake snowflake) { + this.snowflake = snowflake; + } + + @Override + public Number generateKey() { + return snowflake.nextId(); + } +} diff --git a/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/DataSourceShardingConfig.java b/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/DataSourceShardingConfig.java index bacc521..2236503 100644 --- a/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/DataSourceShardingConfig.java +++ b/spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/DataSourceShardingConfig.java @@ -1,11 +1,13 @@ package com.xkcoding.sharding.jdbc.config; +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.IdUtil; import com.zaxxer.hikari.HikariDataSource; import io.shardingsphere.api.config.rule.ShardingRuleConfiguration; import io.shardingsphere.api.config.rule.TableRuleConfiguration; import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration; import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.KeyGenerator; import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; @@ -35,6 +37,8 @@ import java.util.concurrent.ConcurrentHashMap; */ @Configuration public class DataSourceShardingConfig { + private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1); + /** * 需要手动配置事务管理器 */ @@ -69,7 +73,7 @@ public class DataSourceShardingConfig { // ds${0..1}.t_order_${0..2} 也可以写成 ds$->{0..1}.t_order_$->{0..1} tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}"); tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}")); - tableRule.setKeyGenerator(new DefaultKeyGenerator()); + tableRule.setKeyGenerator(customKeyGenerator()); tableRule.setKeyGeneratorColumnName("order_id"); return tableRule; } @@ -96,4 +100,11 @@ public class DataSourceShardingConfig { return dataSourceMap; } + /** + * 自定义主键生成器 + */ + private KeyGenerator customKeyGenerator() { + return new CustomSnowflakeKeyGenerator(snowflake); + } + }