2-2-3 Distributed ID problems and solutions

1. Distributed ID

1.1 Introduction to Distributed ID

Why is a distributed ID needed (a global unique ID in a distributed cluster environment)
Insert picture description here

Second, the solution of distributed ID

2.1. Solution one, UUID

UUID refers to Universally Unique Identifier, which translates as a universally unique identifier. The situation of duplicate UUIDs and errors is very low, so there is no need to consider this issue. To get a UUID in Java, you can use the method provided by the java.util package.

public class MyTest {
    
    
 public static void main(String[] args) {
    
    
 System.out.println(java.util.UUID.randomUUID().toString());
 }
}

Insert picture description here

2.2 Option Two, the self-incremented ID of a unique database

If the sub-tables of A table are A1 and A2, then the IDs of A1 and A2 must not be increased, so how to obtain the ID? We can create a Mysql database separately and create a table in this database. The ID of this table is set to increment. When other places need a globally unique ID, we can simulate this table in this Mysql database. Insert a record, the ID will be incremented at this time, and then we can get the newly created ID in this table through select last_insert_id() of Mysql.

For example, we created a database instance global_id_generator and created a data table in it. The table structure is as follows:


-- Table structure for DISTRIBUTE_ID

DROP TABLE IF EXISTS `DISTRIBUTE_ID`;

CREATE TABLE `DISTRIBUTE_ID` (
 `id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '主键',
 `createtime` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

When an application in a distributed cluster environment needs to obtain a globally unique distributed ID, you can use the code to connect to this
database instance and execute the following sql statement.

insert into DISTRIBUTE_ID(createtime) values(NOW());
select LAST_INSERT_ID();

Note:
1) The createtime field here has no practical meaning, it is for inserting a piece of data at random so that the id can be automatically increased. 2) Use only the Mysql examples of ⽣ attention immediately distributed to id, although ⾏, but performance and reliability are not good enough because you need-generation
code to connect to the database in order to get to the id, performance ⽆ law protection, in addition mysql database instances If it hangs up, then the
distributed id cannot be obtained .
3) Some developers designed the mysql database used to generate distributed IDs into a cluster architecture in response to the above situation.
In fact, this method is basically not used now because it is too cumbersome.

2.3. Solution three, SnowFlake algorithm

2.3.1 Introduction to SnowFlake

The snowflake algorithm is a strategy launched by Twitter to generate distributed IDs.
The snowflake algorithm is an algorithm. Based on this algorithm, an ID can be generated. The generated ID is a long type. In Java, a long
type is 8 bytes, which is 64bit. The following is the use of the snowflake algorithm The binary form of the generated ID is shown:
Insert picture description here

2.3.1 SnowFlake source code

In addition, All Internet companies have also encapsulated some distributed ID generators based on the above scheme, such as Didi’s tinyid (based on database implementation), Baidu’s uidgenerator (based on SnowFlake) and Meituan’s leaf (based on database). And SnowFlake) wait, they are.

2.5. Get the global unique ID with the help of Redis Incr command

The Redis Incr command increments the numeric value stored in the key. If the key does not exist, the value of the key will be initialized to 0 first, and then the INCR operation will be performed.
<key,value>
<id,>
.incr(id) 1 2 3 4

Insert picture description here

  • Redis installation (indicating that we install a single node to use it, the specific content of Redis itself will be explained in detail in the subsequent distributed caching course)

    Officially download redis-3.2.10.tar.gz and
    upload it to the linux server. Unzip tar -zxvf redis-3.2.10.tar.gz
    cd unzip the file directory , compile the unzipped redis
    make
    and then cd into the src file Record, execute make install to
    modify the configuration file redis.conf in the decompression directory, and turn off the protection mode.

Insert picture description here
In the src directory, execute ./redis-server …/redis.conf to start the redis service

  • In the Java code, use the Jedis client to call the incr command of Reids to obtain a global id
    reference to the jedis client jar
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.9.0</version>
</dependency>

Java code (here we connect to a single node, and do not use connection pool)

Jedis jedis = new Jedis("127.0.0.1",6379);
try {
    
    
 long id = jedis.incr("id");
 System.out.println("从redis中获取的分布式id为:" + id);
} finally {
    
    
 if (null != jedis) {
    
    
 jedis.close();
 } }

Guess you like

Origin blog.csdn.net/qq_42082278/article/details/112648251