In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how MyBatis uses Snowflake ID to achieve, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this MyBatis article on how to achieve Snowflake ID. Let's take a look.
First, implement the MyBatis ID build interface @ Slf4j@Componentpublic class CustomIdGenerator implements IdentifierGenerator {@ Override public Long nextId (Object entity) {/ / generate ID long id = SnowFlakeUtils.nextId (); log.info ("generate ID:" + id); return id;}} II. Snowflake ID generation tool class @ Slf4jpublic class SnowFlakeUtils {/ * initial offset timestamp * / private static final long OFFSET = 1546300800L / * * Machine id (015 reserves 16x31 as a backup machine) * / private static final long WORKER_ID; / * * number of bits occupied by machine id (5bit, maximum number of machines 2 ^ 5 = 32) * / private static final long WORKER_ID_BITS = 5L; / * number of bits occupied by self-incrementing sequence (16bit, supports maximum generation of 2 ^ 16 = 65536 per second) * / private static final long SEQUENCE_ID_BITS = 16L / * * Machine id offset bits * / private static final long WORKER_SHIFT_BITS = SEQUENCE_ID_BITS; / * * self-increasing sequence offset bits * / private static final long OFFSET_SHIFT_BITS = SEQUENCE_ID_BITS + WORKER_ID_BITS; / * * maximum machine ID (2 ^ 5 / 2-1 = 15) * / private static final long WORKER_ID_MAX = (1 > 1) / * * backup machine ID start position (2 ^ 5 / 2 = 16) * / private static final long BACK_WORKER_ID_BEGIN = (1 > 1; / * * maximum self-increment sequence (2 ^ 16-1 = 65535) * / private static final long SEQUENCE_MAX = (1 WORKER_ID_MAX) {throw new IllegalArgumentException ("cmallshop.workerId range: 0 ~% d currently:% d", WORKER_ID_MAX, workerId)) } WORKER_ID = workerId;} private static Long getWorkId () {try {String hostAddress = Inet4Address.getLocalHost (). GetHostAddress (); int [] ints = StringUtils.toCodePoints (hostAddress); int sums = 0; for (int b: ints) {sums + = b } return (long) (sums% WORKER_ID_MAX);} catch (UnknownHostException e) {/ / if the acquisition fails, a random number backup return RandomUtils.nextLong is used. }} / * * Private constructor forbids external access to * / private SnowFlakeUtils () {} / * get the self-increasing sequence * @ return long * / public static long nextId () {return nextId (SystemClock.now () / 1000) } / * Host self-increment sequence * @ param timestamp current Unix timestamp * @ return long * / private static synchronized long nextId (long timestamp) {/ / clock callback check if (timestamp < lastTimestamp) {/ / occurrence of clock callback log.warn ("clock callback") Enable backup machine ID: now: [{}] last: [{}] ", timestamp, lastTimestamp) Return nextIdBackup (timestamp);} / / start the next second if (timestamp! = lastTimestamp) {lastTimestamp = timest sequence = 0L } if (0L = = (+ + sequence & SEQUENCE_MAX)) {/ / log.warn ("within seconds [{}] sequence is exhausted, enable backup machine ID sequence", timestamp); sequence--; return nextIdBackup (timestamp);} return ((timestamp-OFFSET))
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.