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

What is a distributed ID generator Tinyid

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what is a distributed ID generator Tinyid". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Characteristics of Tinyid

Globally unique long ID

Id with increasing trend

Provide http and java-client access

Batch acquisition of ID is supported

Support the generation of 1, 3, 5, 7, 7, 9, 7, 7, 9, 7, 7, 9, 7, 7, 7, 7, 7, 7, 7, 9. ID of the sequence

Support for multiple db configurations

Applicable scenarios: systems that only care that ID is digital and the trend is increasing can tolerate ID discontinuity and ID waste.

Not applicable: business like order ID, because most of the generated ID is continuous, it is easy to scan the database or calculate the order quantity and other information.

Tinyid principle

Tinyid is implemented based on the number segment pattern, and let's briefly talk about the principle of the number segment pattern: it is to obtain self-increasing ID from the database in batches, fetching a range of number segments from the database one at a time. For example, (1 ID 1000) represents 1000 segments, and the business service generates a self-incrementing ID of 1 number segment locally and loads it into memory.

Tinyid loads the available number range into memory and generates ID in memory. The available number segment is loaded when the ID is obtained for the first time. For example, when the current number segment is used in a certain proportion, the system will asynchronously load the next available number segment, so as to ensure that there is always an available number segment in memory, so that there is still available ID for a period of time after the issuing service goes down.

The schematic diagram is roughly as follows:

Tinyid schematic diagram

Tinyid implementation

GitHub address of Tinyid: https://github.com/didi/tinyid.git

Tinyid provides two methods of calling, one based on http provided by Tinyid-server, and the other based on Tinyid-client client. No matter which method is used to call, you must build tables tiny_id_info and tiny_id_token in advance to build Tinyid.

CREATE TABLE `tiny_id_ info` (`id` bigint (20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'self-increasing primary key', `begin_ type` varchar (63) NOT NULL DEFAULT''COMMENT' business type, unique', `begin_ id` bigint (20) NOT NULL DEFAULT'0' COMMENT 'start id, only record the initial value and have no other meaning. The initialization of begin_id and max_id should be the same', `max_ id` bigint (20) NOT NULL DEFAULT'0' COMMENT 'current maximum id', `step`int (11) DEFAULT' 0' COMMENT 'step', `delta`int (11) NOT NULL DEFAULT'1' COMMENT 'each id increment', `remainder` int (11) NOT NULL DEFAULT'0' COMMENT 'remainder', `create_ time`timestamp NOT NULL DEFAULT '2010-01-01 00int 00' COMMENT' creation time `version` timestamp NOT NULL DEFAULT '2010-01-01 00COMMENT' update time', `version` bigint (20) NOT NULL DEFAULT'0' COMMENT 'version number, PRIMARY KEY (`id`), UNIQUE KEY `uniq_biz_ type` (`biz_ type`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT' id information table' CREATE TABLE `tiny_id_ token` (`id`int (11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'self-added id', `token` varchar (255) NOT NULL DEFAULT' 'COMMENT' token', `biz_ type` varchar (63) NOT NULL DEFAULT''COMMENT' this token accessible service type ID', `remark`varchar 'NOT NULL DEFAULT' 'COMMENT' remarks', `create_ `timetimestamp NOT NULL DEFAULT '2010-01-01 00int 00' COMMENT' creation time' `update_ time`timestamp NOT NULL DEFAULT '2010-01-01 00 COMMENT' update time', PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT 'token information table' INSERT INTO `tiny_id_ info` (`id`, `max_ type`, `begin_ id`, `max_ id`, `step`, `delta`, `remainder`, `update_ time`, `version`) VALUES (1, 'test', 1, 1, 100000, 1, 0,' 2018-07-21 23 23 22 22 22 23 19 RV 27, 1) INSERT INTO `tiny_id_ info` (`id`, `max_ type`, `begin_ id`, `max_ id`, `step`, `delta`, `remainder`, `update_ time`, `version`) VALUES (2, 'test_odd', 1, 1, 100000, 2, 1,' 2018-07-21 2323 2323 331 22 22 RV 58, '2018-07-23 00 39 39, 3) INSERT INTO `tiny_id_ token` (`id`, `token`, `biz_ type`, `remark`, `create_ time`, `update_ time`) VALUES (1, '0f673adf80504e2eaa552f5d791b644cbicycle,' test', '1bike,' 2017-12-14 16R 36R 46mm, '2017-12-14 16R 36R 3648') INSERT INTO `tiny_id_ token` (`id`, `token`, `biz_ type`, `remark`, `create_ time`, `update_ time`) VALUES (2, '0f673adf80504e2eaa552f5d791b644cregions,' test_odd','1, '2017-12-14 16R 36R 46mm,' 2017-12-14 16R 36R 3648')

Tiny_id_ info table is a data table of specific business party number segment information.

Max_id: the maximum value of the number segment

Step: step size, that is, the length of the number segment

Biz_type: business type

A update operation is performed on the max_id field, update max_id= max_id + step. If the update is successful, the new range is obtained successfully. The new range is (max_id, max_id + step).

Tiny_id_token is a permission table that indicates the segment information of which businesses the current token can operate.

Modify the\ offline\ application.properties file in tinyid-server to configure the database. Because tinyid supports database multi-master mode, multiple database information can be configured. Start TinyIdServerApplication to test it.

Datasource.tinyid.primary.driver-class-name=com.mysql.jdbc.Driver datasource.tinyid.primary.url=jdbc:mysql://127.0.0.1:3306/xin-master?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 datasource.tinyid.primary.username=junkang datasource.tinyid.primary.password=junkang datasource.tinyid.primary.testOnBorrow=false datasource.tinyid.primary.maxActive=10 datasource.tinyid.secondary.driver-class-name=com.mysql.jdbc.Driver datasource.tinyid.secondary.url=jdbc:mysql://localhost:3306/db2?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 Datasource.tinyid.secondary.username=root datasource.tinyid.secondary.password=123456 datasource.tinyid.secondary.testOnBorrow=false datasource.tinyid.secondary.maxActive=10

1. Http mode

A total of four http APIs are provided within tinyid to obtain ID and number range.

Package com.xiaoju.uemc.tinyid.server.controller; / * * @ author du_imba * / @ RestController @ RequestMapping ("/ id/") public class IdContronller {private static final Logger logger = LoggerFactory.getLogger (IdContronller.class); @ Autowired private IdGeneratorFactoryServer idGeneratorFactoryServer; @ Autowired private SegmentIdService segmentIdService; @ Autowired private TinyIdTokenService tinyIdTokenService; @ Value ("${batch.size.max}") private Integer batchSizeMax @ RequestMapping ("nextId") public Response nextId (String bizType, Integer batchSize, String token) {Response response = new Response (); try {IdGenerator idGenerator = idGeneratorFactoryServer.getIdGenerator (bizType); List ids = idGenerator.nextId (newBatchSize); response.setData (ids);} catch (Exception e) {response.setCode (ErrorCode.SYS_ERR.getCode ()) Response.setMessage (e.getMessage ()); logger.error ("nextId error", e);} return response;} @ RequestMapping ("nextIdSimple") public String nextIdSimple (String bizType, Integer batchSize, String token) {String response = ""; try {IdGenerator idGenerator = idGeneratorFactoryServer.getIdGenerator (bizType) If (newBatchSize = = 1) {Long id = idGenerator.nextId (); response = id + "";} else {List idList = idGenerator.nextId (newBatchSize); StringBuilder sb = new StringBuilder () For (Long id: idList) {sb.append (id) .append (",");} response = sb.deleteCharAt (sb.length ()-1). ToString ();}} catch (Exception e) {logger.error ("nextIdSimple error", e) } return response;} @ RequestMapping ("nextSegmentId") public Response nextSegmentId (String bizType, String token) {try {SegmentId segmentId = segmentIdService.getNextSegmentId (bizType); response.setData (segmentId);} catch (Exception e) {response.setCode (ErrorCode.SYS_ERR.getCode ()) Response.setMessage (e.getMessage ()); logger.error ("nextSegmentId error", e);} return response;} @ RequestMapping ("nextSegmentIdSimple") public String nextSegmentIdSimple (String bizType, String token) {String response = ""; try {SegmentId segmentId = segmentIdService.getNextSegmentId (bizType) Response = segmentId.getCurrentId () +, "+ segmentId.getLoadingId () +", "+ segmentId.getMaxId () +", "+ segmentId.getDelta () +", "+ segmentId.getRemainder ();} catch (Exception e) {logger.error (" nextSegmentIdSimple error ", e);} return response;}}

NextId and nextIdSimple are used to get the next ID,nextSegmentIdSimple, and getNextSegmentId is to get the next available number range. The difference lies in whether the interface has a return status.

NextId: 'http://localhost:9999/tinyid/id/nextId?bizType=test&token=0f673adf80504e2eaa552f5d791b644c' response: {"data": [2], "code": 200, "message": ""} nextId Simple:' http://localhost:9999/tinyid/id/nextIdSimple?bizType=test&token=0f673adf80504e2eaa552f5d791b644c' response: 3

2. Tinyid-client client

If you don't want to go through http, the Tinyid-client client is also a good choice.

Referencing the tinyid-server package

Com.xiaoju.uemc.tinyid tinyid-client ${tinyid.version}

Start the tinyid-server project to package and get tinyid-server-0.1.0-SNAPSHOT.jar, and set the version ${tinyid.version} to 0.1.0-SNAPSHOT.

Configure the request address and user identity token of the tinyid-server service in our project application.properties

Tinyid.server=127.0.0.1:9999 tinyid.token= 0f673adf80504e2eaa552f5d791b644c```

Calling TinyId from Java code is also easy, with only one line of code.

/ / get a single ID Long id = TinyId.nextId ("test") according to the business type; / / get 10 ID List ids = TinyId.nextId in batches according to the business type ("test", 10)

The source code implementation of the whole Tinyid project is also relatively simple, such as interacting with the database is more directly implemented in jdbcTemplate.

@ Override public TinyIdInfo queryByBizType (String bizType) {String sql = "select id, biz_type, begin_id, max_id," + "step, delta, remainder, create_time, update_time, version" + "from tiny_id_info where biz_type =?"; List list = jdbcTemplate.query (sql, new Object [] {bizType}, new TinyIdInfoRowMapper ()) If (list = = null | | list.isEmpty ()) {return null;} return list.get (0);} "what is a distributed ID generator Tinyid" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Database

Wechat

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

12
Report