使用shardingsphere开源雪花算法

1、shardingsphere是一个开源系统,提供了一套分布式数据库解决方案。

  而这里面肯定会涉及到如何生成系统中不重复的主键id

2、官方文档地址:https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/key-generator/

3、上代码

 1 /**
 2  * @author jijiecong
 3  * @version 1.0
 4  * @date 2021/12/1 14:21
 5  * @description 雪花算法生成id工具类
 6  */
 7 @Slf4j
 8 public class SnowFlakeUtil {
 9 
10     private static final String WORK_ID_KEY = "worker.id";
11 
12     private static SnowflakeShardingKeyGenerator keyGenerator;
13 
14     /**
15      * 单例
16      */
17     public static SnowflakeShardingKeyGenerator getInstance() {
18 
19         if (keyGenerator == null) {
20 
21             synchronized (SnowFlakeUtil.class) {
22 
23                 // 双重校验
24                 if (keyGenerator == null) {
25 
26                     keyGenerator = new SnowflakeShardingKeyGenerator();
27 
28                     // 设置workId,注意一定要转换成String再put进去
29                     keyGenerator.getProperties().put(WORK_ID_KEY, getWorkId());
30                 }
31 
32             }
33 
34         }
35 
36         return keyGenerator;
37     }
38 
39     /**
40      * 获取id
41      *
42      * @return id
43      */
44     public static Long getId() {
45 
46         Long id = (Long) getInstance().generateKey();
47 
48         log.info("___此次生成的id为:{}", id);
49 
50         return id;
51 
52     }
53 
54     /**
55      * 获取workId,保证分布式服务下每台机器都不同
56      * 使用机器ip都最后3位作为workId
57      *
58      * @return workId
59      */
60     public static String getWorkId() {
61         String workId = "0";
62 
63         try {
64 
65             InetAddress inetAddress = InetAddress.getLocalHost();
66 
67             String localIp = StringUtils.isBlank(inetAddress.getHostAddress()) ?
68                     null : inetAddress.getHostAddress().trim();
69 
70             if (StringUtils.isNotBlank(localIp) && localIp.contains(".")) {
71 
72                 String[] ipArray = localIp.split("\\.");
73 
74                 workId = ipArray[ipArray.length - 1];
75 
76                 log.info("___workId:{}", workId);
77 
78             }
79 
80         } catch (Exception e) {
81 
82             log.error("获取workId失败", e);
83 
84         }
85 
86         return workId;
87     }
88 
89     public static void main(String[] args) {
90         long id = SnowFlakeUtil.getId();
91         System.out.println(id);
92     }
93 }

4、踩坑

  4.1、在设置properities时要转成String再设置,Properities源码

 1     /**
 2      * Searches for the property with the specified key in this property list.
 3      * If the key is not found in this property list, the default property list,
 4      * and its defaults, recursively, are then checked. The method returns
 5      * {@code null} if the property is not found.
 6      *
 7      * @param   key   the property key.
 8      * @return  the value in this property list with the specified key value.
 9      * @see     #setProperty
10      * @see     #defaults
11      */
12     public String getProperty(String key) {
13         Object oval = super.get(key);
14         String sval = (oval instanceof String) ? (String)oval : null;
15         return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
16     }

 

posted on 2021-12-01 15:16  Iversonstear  阅读(1043)  评论(0编辑  收藏  举报

导航