In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the Spring Boot project integrated global unique ID generator UidGenerator example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Integration experiment of Global unique flow number ID Generator in Spring Boot
Overview
The serial number generator (global unique ID generator) is the infrastructure of the service-oriented system, which plays an important role in ensuring the correct operation and high availability of the system. Snowflake snowflake algorithm is the most important algorithm about serial number generation. However, Snowflake itself is difficult to be directly used in real projects, so a landing scheme is needed in practical application.
UidGenerator is developed by Baidu and implemented by Java. It is the only ID generator based on Snowflake algorithm. UidGenerator works in application projects in the form of components, and supports custom workerId bits and initialization policies, which is suitable for scenarios such as automatic restart and drift of instances in virtualized environments such as docker.
This article integrates the UidGenerator project in the project as the global unique ID generator for the project.
The brain map of this article is as follows:
Foundation project creation
Just create a Maven project for Multi-Moudule, and then we integrate two Module:
Uid-generator: the source code is here
Uid-consumer: consumer (using uid-generator to generate globally unique serial numbers)
I won't say much about the uid-generator module. Just bring the source code without any changes. As for the uid-consumer module, add the relevant dependencies in pom.xml as follows:
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime 8.0.12 com.alibaba druid-spring-boot-starter 1.1.9 cn.codesheep uid-generator 1.0
Then add some configurations (mainly MySQL and MyBatis configurations) to the application.properties configuration file
Server.port=9999spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=xxxspring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.mapper-locations=classpath:mapper/*.xmlmybatis.configuration.map-underscore-to-camel-case=true
After completion, the miniature of the project is shown in the following figure:
Let's integrate the source code of UidGenerator step by step.
Database table building
First, go to the MySQL database and create a data table named WORKER_NODE. Its sql is as follows:
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
Detailed configuration of Spring
CachedUidGenerator configuration
UidGenerator has two specific implementation classes, DefaultUidGenerator and CachedUidGenerator, but the latter is also recommended for performance-sensitive projects, so this article also uses CachedUidGenerator without too much elaboration on DefaultUidGenerator.
We introduced the cached-uid-spring.xml file in the UidGenerator source code, which contains the default configuration. I haven't made any changes so far.
Mybatis Mapper XML configuration
That is, the mapper xml file about the operation of the worker node (Worker Node) in the UidGenerator source code is introduced as is: WORKER_NODE.xml, which contains the following contents:
INSERT INTO WORKER_NODE (HOST_NAME, PORT, TYPE, LAUNCH_DATE, MODIFIED, CREATED) VALUES (# {hostName}, # {port}, # {type}, # {launchDate}, NOW (), NOW () SELECT ID, HOST_NAME, PORT, TYPE, LAUNCH_DATE, MODIFIED, CREATED FROM WORKER_NODE WHERE HOST_NAME = # {host} AND PORT = # {port}
Write business code
Config class creation and configuration
Create a new UidConfig class to introduce the cached-uid-spring.xml configuration above for us
@ Configuration@ImportResource (locations = {"classpath:uid/cached-uid-spring.xml"}) public class UidConfig {}
Service class creation and configuration
Create a new UidGenService and introduce UidGenerator to generate the business interface of UID
@ Servicepublic class UidGenService {@ Resource private UidGenerator uidGenerator; public long getUid () {return uidGenerator.getUID ();}}
Controller creation and configuration
A new UidTestController is created to make it easier for us to test the interface with a browser and observe the effect:
RestControllerpublic class UidTestController {@ Autowired private UidGenService uidGenService; @ GetMapping ("/ testuid") public String test () {return String.valueOf (uidGenService.getUid ());}}
Experimental test
Every time we start the Spring Boot project, it will automatically insert a row of records about the work node into the WORKER_NODE table of the MySQL data, similar to the figure below:
Next, let's visit the browser: http://localhost:9999/testuid.
OK, the global unique flow number ID has been successfully generated and returned!
The above is all the contents of the article "sample Analysis of Spring Boot Project Integrated Global unique ID Generator UidGenerator". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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: 213
*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.