Skip to content

Commit

Permalink
✨ spring-boot-demo-sharding-jdbc 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Mar 26, 2019
1 parent 6a82d29 commit ecfac23
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
48 changes: 46 additions & 2 deletions spring-boot-demo-sharding-jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand All @@ -115,6 +150,8 @@
*/
@Configuration
public class DataSourceShardingConfig {
private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1);

/**
* 需要手动配置事务管理器
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -176,6 +213,13 @@ public class DataSourceShardingConfig {
return dataSourceMap;
}

/**
* 自定义主键生成器
*/
private KeyGenerator customKeyGenerator() {
return new CustomSnowflakeKeyGenerator(snowflake);
}

}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,6 +37,8 @@
*/
@Configuration
public class DataSourceShardingConfig {
private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1);

/**
* 需要手动配置事务管理器
*/
Expand Down Expand Up @@ -69,7 +73,7 @@ private TableRuleConfiguration orderTableRule() {
// 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;
}
Expand All @@ -96,4 +100,11 @@ private Map<String, DataSource> dataSourceMap() {
return dataSourceMap;
}

/**
* 自定义主键生成器
*/
private KeyGenerator customKeyGenerator() {
return new CustomSnowflakeKeyGenerator(snowflake);
}

}

0 comments on commit ecfac23

Please sign in to comment.