In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to encapsulate the spring ID generator". In the daily operation, I believe many people have doubts about how to encapsulate the spring ID generator. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to encapsulate the spring ID generator". Next, please follow the editor to study!
Overview
The ID number generator (or: global unique ID generator) is the infrastructure of the server system, and the ID number is basically something that programmers engaged in back-end development come into contact with every day. The ID generation algorithm is now the industry's leading Snowflake snowflake algorithm.
UidGenerator is a high-performance and unique ID generator based on Snowflake snowflake algorithm, which is open source by Baidu. In the previous article in this number, UidGenerator has been used in detail, but the use process is still relatively complicated, but also need to quote the source code of the UidGenerator component, which is a bit inconvenient. For this reason, this paper is based on UidGenerator, and then encapsulates a set of ID number generation components that are more conducive to the use of Spring Boot projects, named id-spring-boot-starter.
Usage
Import SQL script
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
This step must not be avoided. After all, UidGenerator needs database support.
Add dependencies to pom
Cn.codesheep id-spring-boot-starter 1.0.0
Configure database connection
Url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useOldAliasMetadataBehavior=true&connectionCollation=utf8mb4_unicode_ci&rewriteBatchedStatements=true&allowMultiQueries=trueusername: xxxxxxpassword: xxxxxx
Or is it because UidGenerator needs database support
Modify the Spring Boot main class
You can add mybatis annotations to the main class of Spring Boot application:
@ MapperScan ({"com.baidu.fsg.uid.worker.dao"})
How the code is used
@ RestControllerpublic class TestController {@ Autowired private UidGenService uidGenService; @ GetMapping ("/ uid") public String genUid () {return String.valueOf ("the only ID generated this time is:" + uidGenService.getUid ());}}
Well, it's super easy to use:
First of all, introduce the UidGenService class in the way of Autowired
A long ID number can be obtained by directly calling the getUid () method of the UidGenService class.
Running effect
The demo source code is here. Please mention it yourself:
Https://github.com/hansonwang99/Spring-Boot-In-Action/tree/master/test-id-spring-boot-starter
Download id-spring-boot-starter source code
If you need to customize the component id-spring-boot-starter source code, you can download the source code on github. The address is here:
Https://github.com/hansonwang99/Spring-Boot-In-Action/tree/master/id-spring-boot-starter
A few points to pay attention to:
Since UidGenerator requires database support, be sure to import the data table before using it, and configure MapperScan
If you need to highly customize the details of UidGenerator components, you can modify the cached-uid-spring.xml file inside id-spring-boot-starter, and then repackage the jar package.
Since ID number generation is generally a basic service of the system, it can be separated into a micro-service that can be called by other micro-services.
At this point, the study of "how to encapsulate the spring ID generator" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.