Skip to content

Commit ecfac23

Browse files
committed
✨ spring-boot-demo-sharding-jdbc 完成
1 parent 6a82d29 commit ecfac23

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

spring-boot-demo-sharding-jdbc/README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,42 @@
9797
</project>
9898
```
9999

100-
### 2.2. `DataSourceShardingConfig.java`
100+
### 2.2. `CustomSnowflakeKeyGenerator.java`
101+
102+
```java
103+
package com.xkcoding.sharding.jdbc.config;
104+
105+
import cn.hutool.core.lang.Snowflake;
106+
import io.shardingsphere.core.keygen.KeyGenerator;
107+
108+
/**
109+
* <p>
110+
* 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数
111+
* </p>
112+
*
113+
* @package: com.xkcoding.sharding.jdbc.config
114+
* @description: 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数
115+
* @author: yangkai.shen
116+
* @date: Created in 2019-03-26 17:07
117+
* @copyright: Copyright (c) 2019
118+
* @version: V1.0
119+
* @modified: yangkai.shen
120+
*/
121+
public class CustomSnowflakeKeyGenerator implements KeyGenerator {
122+
private Snowflake snowflake;
123+
124+
public CustomSnowflakeKeyGenerator(Snowflake snowflake) {
125+
this.snowflake = snowflake;
126+
}
127+
128+
@Override
129+
public Number generateKey() {
130+
return snowflake.nextId();
131+
}
132+
}
133+
```
134+
135+
### 2.3. `DataSourceShardingConfig.java`
101136

102137
```java
103138
/**
@@ -115,6 +150,8 @@
115150
*/
116151
@Configuration
117152
public class DataSourceShardingConfig {
153+
private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
154+
118155
/**
119156
* 需要手动配置事务管理器
120157
*/
@@ -149,7 +186,7 @@ public class DataSourceShardingConfig {
149186
// ds${0..1}.t_order_${0..2} 也可以写成 ds$->{0..1}.t_order_$->{0..1}
150187
tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}");
151188
tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}"));
152-
tableRule.setKeyGenerator(new DefaultKeyGenerator());
189+
tableRule.setKeyGenerator(customKeyGenerator());
153190
tableRule.setKeyGeneratorColumnName("order_id");
154191
return tableRule;
155192
}
@@ -176,6 +213,13 @@ public class DataSourceShardingConfig {
176213
return dataSourceMap;
177214
}
178215

216+
/**
217+
* 自定义主键生成器
218+
*/
219+
private KeyGenerator customKeyGenerator() {
220+
return new CustomSnowflakeKeyGenerator(snowflake);
221+
}
222+
179223
}
180224
```
181225

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.xkcoding.sharding.jdbc.config;
2+
3+
import cn.hutool.core.lang.Snowflake;
4+
import io.shardingsphere.core.keygen.KeyGenerator;
5+
6+
/**
7+
* <p>
8+
* 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数
9+
* </p>
10+
*
11+
* @package: com.xkcoding.sharding.jdbc.config
12+
* @description: 自定义雪花算法,替换 DefaultKeyGenerator,避免DefaultKeyGenerator生成的id大几率是偶数
13+
* @author: yangkai.shen
14+
* @date: Created in 2019-03-26 17:07
15+
* @copyright: Copyright (c) 2019
16+
* @version: V1.0
17+
* @modified: yangkai.shen
18+
*/
19+
public class CustomSnowflakeKeyGenerator implements KeyGenerator {
20+
private Snowflake snowflake;
21+
22+
public CustomSnowflakeKeyGenerator(Snowflake snowflake) {
23+
this.snowflake = snowflake;
24+
}
25+
26+
@Override
27+
public Number generateKey() {
28+
return snowflake.nextId();
29+
}
30+
}

spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/config/DataSourceShardingConfig.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.xkcoding.sharding.jdbc.config;
22

3+
import cn.hutool.core.lang.Snowflake;
4+
import cn.hutool.core.util.IdUtil;
35
import com.zaxxer.hikari.HikariDataSource;
46
import io.shardingsphere.api.config.rule.ShardingRuleConfiguration;
57
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
68
import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration;
79
import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration;
8-
import io.shardingsphere.core.keygen.DefaultKeyGenerator;
10+
import io.shardingsphere.core.keygen.KeyGenerator;
911
import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
1012
import org.springframework.beans.factory.annotation.Qualifier;
1113
import org.springframework.context.annotation.Bean;
@@ -35,6 +37,8 @@
3537
*/
3638
@Configuration
3739
public class DataSourceShardingConfig {
40+
private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
41+
3842
/**
3943
* 需要手动配置事务管理器
4044
*/
@@ -69,7 +73,7 @@ private TableRuleConfiguration orderTableRule() {
6973
// ds${0..1}.t_order_${0..2} 也可以写成 ds$->{0..1}.t_order_$->{0..1}
7074
tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}");
7175
tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}"));
72-
tableRule.setKeyGenerator(new DefaultKeyGenerator());
76+
tableRule.setKeyGenerator(customKeyGenerator());
7377
tableRule.setKeyGeneratorColumnName("order_id");
7478
return tableRule;
7579
}
@@ -96,4 +100,11 @@ private Map<String, DataSource> dataSourceMap() {
96100
return dataSourceMap;
97101
}
98102

103+
/**
104+
* 自定义主键生成器
105+
*/
106+
private KeyGenerator customKeyGenerator() {
107+
return new CustomSnowflakeKeyGenerator(snowflake);
108+
}
109+
99110
}

0 commit comments

Comments
 (0)