使用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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM