Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate UidGenerator using Spring Boot projects

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use the Spring Boot project to integrate UidGenerator", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Spring Boot project to integrate UidGenerator.

Preface

Implementation of UidGenerator based on snowflake algorithm

UidGenerator is a unique ID generator based on SnowFlake algorithm developed by Baidu. UidGenerator already works in application projects in the form of components, and supports custom workeid bits and initialization policies, which are suitable for scenarios such as automatic restart of instances in virtualized environments such as docker.

Prepare a maven project and build two modules. As the user and the provider respectively. (the main purpose of building two modules is to "build wheels". Other modules or projects can be referenced directly, and there is no need to care about uid configuration. If there are no sub-modules, you can ignore building two modules.)

Download the uid source code, put it in the project, open source address https://github.com/baidu/uid-generator

Database table building

DROP TABLE IF EXISTS WORKER_NODE;CREATE TABLE WORKER_NODE (ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',HOST_NAME VARCHAR (64) NOT NULL COMMENT' host name',PORT VARCHAR (64) NOT NULL COMMENT 'port',TYPE INT NOT NULL COMMENT' node type: ACTUAL or CONTAINER',LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',MODIFIED TIMESTAMP NOT NULL COMMENT' modified time',CREATED TIMESTAMP NOT NULL COMMENT 'created time',PRIMARY KEY (ID)) COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB

Spring configuration

CachedUidGennerator:

UidGenerator has two specific implementation classes, DefaultUidGenerator and CachedUidGenerator, which are officially recommended to use CachedUidGenerator with strong performance.

We refer directly to the cached-uid-spring.xml file in the UdiGenerator source code and use the default configuration

Introduce the cached-uid-spring.xml configuration file in our own newly created UidConfig

Package com.xxx.uid.config;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;/*** @ author lishuzhen* @ date 2020-8-11 16:10*/@Configuration@ImportResource (locations = {"classpath:/uid/cached-uid-spring.xml"}) public class UidConfig {}

Maven is introduced in another module to create a UidGenUtils utility class that is easy to use

Package com.xxxx.utils;import com.xxx.uid.UidGenerator;import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** @ author lishuzhen* @ date 2020-8-11 16:13*/@Componentpublic class UidGenUtils {@ Resource private UidGenerator uidGenerator; public long getUid () {return uidGenerator.getUID ();} public String getUidStr () {return String.valueOf (uidGenerator.getUID ()) }} at this point, I believe you have a deeper understanding of "how to use Spring Boot project integration UidGenerator". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report