Skip to content

Commit 2f960b9

Browse files
committed
发布自定义分布式主键ID
1 parent f6bed74 commit 2f960b9

File tree

9 files changed

+131
-19
lines changed

9 files changed

+131
-19
lines changed

springboot-sharding-jdbc/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
<scope>runtime</scope>
5252
</dependency>
5353

54+
<!-- 集成数据库密码加密工具jasypt jdk8版本-->
55+
<dependency>
56+
<groupId>com.github.ulisesbocchio</groupId>
57+
<artifactId>jasypt-spring-boot-starter</artifactId>
58+
<version>1.14</version>
59+
</dependency>
60+
5461
</dependencies>
5562

5663
<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 < 1; i++) {
45+
for (int i = 0; i < 3; i++) {
4646
// HintManager.clear();
4747
// HintManager hintManager = HintManager.getInstance();
4848
// hintManager.setDatabaseShardingValue(1);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.xiaofu.sharding.key;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
23+
24+
import java.util.Properties;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
27+
public final class IncrementShardingKeyGenerator implements ShardingKeyGenerator {
28+
29+
@Getter
30+
private final String type = "INCREMENT";
31+
32+
private final AtomicInteger count = new AtomicInteger();
33+
34+
@Getter
35+
@Setter
36+
private Properties properties = new Properties();
37+
38+
@Override
39+
public Comparable<?> generateKey() {
40+
return count.incrementAndGet();
41+
}
42+
}

springboot-sharding-jdbc/src/main/java/com/xiaofu/sharding/key/MyKeyGenerator.java renamed to springboot-sharding-jdbc/src/main/java/com/xiaofu/sharding/key/MyShardingKeyGenerator.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@
44
import org.springframework.stereotype.Component;
55

66
import java.util.Properties;
7+
import java.util.concurrent.atomic.AtomicInteger;
78

89
/**
9-
* @Author: xinzhifu
10-
* @Description:
10+
* @Author: xiaofu
11+
* @Description: 自定义主键生成器
1112
*/
1213
@Component
13-
public class MyKeyGenerator implements ShardingKeyGenerator {
14+
public class MyShardingKeyGenerator implements ShardingKeyGenerator {
1415

1516

17+
private final AtomicInteger count = new AtomicInteger();
18+
19+
/**
20+
* 自定义的生成方案类型
21+
*/
1622
@Override
17-
public Comparable<?> generateKey() {
18-
return 1;
23+
public String getType() {
24+
return "XXX";
1925
}
2026

27+
/**
28+
* 核心方法-生成主键ID
29+
*/
2130
@Override
22-
public String getType() {
23-
return "xiaofukey";
31+
public Comparable<?> generateKey() {
32+
return count.incrementAndGet();
2433
}
2534

2635
@Override

springboot-sharding-jdbc/src/main/java/com/xiaofu/sharding/model/TOrder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.xiaofu.sharding.model;
22

3-
import com.baomidou.mybatisplus.annotation.TableId;
43
import lombok.Data;
54

65
import java.math.BigDecimal;
76

87
@Data
98
public class TOrder {
109

11-
@TableId(value = "order_id")
10+
//@TableId(value = "order_id")
1211
private Long orderId;
1312

1413
private Long userId;

springboot-sharding-jdbc/src/main/java/com/xiaofu/sharding/model/TOrderDto.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.xiaofu.sharding.model;
22

3-
import com.baomidou.mybatisplus.annotation.TableId;
43
import lombok.Data;
54

65
import java.math.BigDecimal;
76

87
@Data
98
public class TOrderDto {
109

11-
@TableId(value = "order_id")
10+
//@TableId(value = "order_id")
1211
private Long orderId;
1312

1413
private String orderNo;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.xiaofu.sharding.utils;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.jasypt.util.text.BasicTextEncryptor;
5+
6+
/**
7+
* @Author: xinzhifu
8+
* @Description:
9+
*/
10+
@Slf4j
11+
public class JasyptUtil {
12+
13+
/**
14+
* 加密方法
15+
*
16+
* @param salt 盐值
17+
* @param targetString 待加密字符串
18+
* @return 密文
19+
*/
20+
public static String encrypt(String salt, String targetString) {
21+
BasicTextEncryptor encryptor = new BasicTextEncryptor();
22+
encryptor.setPassword(salt);
23+
return encryptor.encrypt(targetString);
24+
}
25+
26+
/**
27+
* 解密方法
28+
*
29+
* @param salt 盐值
30+
* @param targetString 待解密字符串
31+
* @return 明文
32+
*/
33+
public static String decrypt(String salt, String targetString) {
34+
BasicTextEncryptor encryptor = new BasicTextEncryptor();
35+
encryptor.setPassword(salt);
36+
return encryptor.decrypt(targetString);
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
String salt = "order_database";
42+
String username = "root";
43+
String password = "xinzhifu521";
44+
// 进行加密操作
45+
String usernameEncryptStr = encrypt(salt, username);
46+
String passwordEncryptStr = encrypt(salt, password);
47+
// 进行解密操作
48+
String usernameDecryptStr = decrypt(salt, usernameEncryptStr);
49+
String passwordDecryptStr = decrypt(salt, passwordEncryptStr);
50+
// 输出明文和密文
51+
log.info("usernameEncrypt :{}", usernameEncryptStr);
52+
log.info("passwordEncryptStr :{}", passwordEncryptStr);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
com.xiaofu.sharding.key.MyKeyGenerator
1+
com.xiaofu.sharding.key.MyShardingKeyGenerator

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ 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=order_id
22+
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=user_id
2323
# 分库分片算法
24-
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds-$->{order_id % 2}
24+
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds-$->{user_id % 2}
2525
# 分表策略
2626
# 分表分片健
27-
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
27+
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=user_id
2828
# 分表算法
29-
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 3}
29+
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{user_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=SNOWFLAKE
33+
spring.shardingsphere.sharding.tables.t_order.key-generator.type=XXX
3434
# 工作机器唯一 id
35-
spring.shardingsphere.sharding.tables.t_order.key-generator.worker.id=6
35+
spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=0000
36+
3637

3738
# 配置分片表 t_order_item
3839
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item_$->{0..2}
@@ -61,3 +62,4 @@ mybatis-plus.map-underscore-to-camel-case=true
6162
# 开启热部署(更改文件后,自动重启)
6263
spring.devtools.restart.enabled=true
6364

65+

0 commit comments

Comments
 (0)