Skip to content

Commit bb5a1e0

Browse files
committed
增加tingid 分布式ID生成器
1 parent 2f960b9 commit bb5a1e0

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

springboot-sharding-jdbc/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<version>1.14</version>
5959
</dependency>
6060

61+
<dependency>
62+
<groupId>com.xiaoju.uemc.tinyid</groupId>
63+
<artifactId>tinyid-client</artifactId>
64+
<version>0.1.0-SNAPSHOT</version>
65+
</dependency>
66+
6167
</dependencies>
6268

6369
<build>

springboot-sharding-jdbc/src/main/java/com/xiaofu/sharding/contoller/TestController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class TestController {
4242
@ResponseBody
4343
public String insertOrder() {
4444

45-
for (int i = 0; i < 3; i++) {
45+
for (int i = 0; i < 5; i++) {
4646
// HintManager.clear();
4747
// HintManager hintManager = HintManager.getInstance();
4848
// hintManager.setDatabaseShardingValue(1);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.xiaofu.sharding.key;
2+
3+
import com.xiaoju.uemc.tinyid.client.utils.TinyId;
4+
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.Properties;
8+
import java.util.concurrent.atomic.AtomicInteger;
9+
10+
/**
11+
* @Author: xiaofu
12+
* @Description: 自定义主键生成器
13+
*/
14+
@Component
15+
public class TinyIdShardingKeyGenerator implements ShardingKeyGenerator {
16+
17+
/**
18+
* 自定义的生成方案类型
19+
*/
20+
@Override
21+
public String getType() {
22+
return "tinyid";
23+
}
24+
25+
/**
26+
* 核心方法-生成主键ID
27+
*/
28+
@Override
29+
public Comparable<?> generateKey() {
30+
31+
Long id = TinyId.nextId("order");
32+
33+
return id;
34+
}
35+
36+
@Override
37+
public Properties getProperties() {
38+
return null;
39+
}
40+
41+
@Override
42+
public void setProperties(Properties properties) {
43+
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
com.xiaofu.sharding.key.MyShardingKeyGenerator
2+
com.xiaofu.sharding.key.TinyIdShardingKeyGenerator

springboot-sharding-jdbc/src/main/resources/application.properties

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ spring.shardingsphere.datasource.ds-1.password=xinzhifu521
1919
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order_$->{0..2}
2020
### 分库策略
2121
# 分库分片健
22-
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=user_id
22+
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=order_id
2323
# 分库分片算法
24-
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds-$->{user_id % 2}
24+
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds-$->{order_id % 2}
2525
# 分表策略
2626
# 分表分片健
27-
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=user_id
27+
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
2828
# 分表算法
29-
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{user_id % 3}
29+
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 3}
3030
# 自增主键字段
3131
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
3232
# 自增主键ID 生成方案
33-
spring.shardingsphere.sharding.tables.t_order.key-generator.type=XXX
33+
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
3434
# 工作机器唯一 id
3535
spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=0000
36-
36+
#
37+
spring.shardingsphere.sharding.tables.t_order.key-generator.max.tolerate.time.difference.milliseconds=5
3738

3839
# 配置分片表 t_order_item
3940
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item_$->{0..2}
@@ -63,3 +64,5 @@ mybatis-plus.map-underscore-to-camel-case=true
6364
spring.devtools.restart.enabled=true
6465

6566

67+
68+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# tinyid ·Ö²¼Ê½ID
2+
# ·þÎñµØÖ·
3+
tinyid.server=127.0.0.1:9999
4+
# ÒµÎñtoken
5+
tinyid.token=0f673adf80504e2eaa552f5d791b644c

0 commit comments

Comments
 (0)