@@ -97,7 +97,42 @@ | |||
</project> | |||
``` | |||
### 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; | |||
/** | |||
* <p> | |||
* 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 | |||
* </p> | |||
* | |||
* @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); | |||
} | |||
} | |||
``` | |||
@@ -0,0 +1,30 @@ | |||
package com.xkcoding.sharding.jdbc.config; | |||
import cn.hutool.core.lang.Snowflake; | |||
import io.shardingsphere.core.keygen.KeyGenerator; | |||
/** | |||
* <p> | |||
* 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数 | |||
* </p> | |||
* | |||
* @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(); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |