Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

1. Meituan Leaf distributed ID generator

The earliest demand of Meituan Leaf is the order ID generation demand of each line of business. For the specific Leaf design document, see Leaf Meituan Distributed ID Generation Service. Characteristics of Leaf:

  • Leaf relies on a database or ZK(in this case, the ZK implementation of Snowflake is an enhancement of the original method of Snowflake).
  • Leaf provides two modes: leaf-segment database mode and Leaf-snowflake mode.
  • Optimized for Snowflake time callback. Time rollback is resolved.

2. Java implementation of Leaf

Meituan has the implementation code on Github, let’s follow the code to build a local project to run. And use it.

Clone code:

git clone https://github.com/Meituan-Dianping/Leaf.git
Copy the code

Configuration:

Enter the leaf – server/SRC/main/resources/leaf. The properties in the configuration file Settings

configuration instructions The default value
leaf.name leaf service name
leaf.segment.enable whether segment mode is enabled false
leaf.jdbc.url mysql url
leaf.jdbc.username mysql username
leaf.jdbc.password mysql password
leaf.snowflake.enable whether snowflake mode is enabled false
leaf.snowflake.zk.address zk address under snowflake mode
leaf.snowflake.port service registration port under snowflake mode

We enable leaf.segment. Enable =true. Leaf.snowflake. Enable is temporarily disabled due to the need to use ZK. The leaf. The properties Settings:

leaf.name=mxsm
leaf.segment.enable=true
leaf.jdbc.url=JDBC: mysql: / / 192.168.43.129:3306 / leaf? useUnicode=true&characterEncoding=utf-8
leaf.jdbc.username=root
leaf.jdbc.password=sys123456
Copy the code

Execute the script:

CREATE TABLE `leaf_alloc` (
  `biz_tag` varchar(128)  NOT NULL DEFAULT ' '.-- your biz unique name
  `max_id` bigint(20) NOT NULL DEFAULT '1',
  `step` int(11) NOT NULL,
  `description` varchar(256)  DEFAULT NULL,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB;

insert into leaf_alloc(biz_tag, max_id, step, description) values('leaf-segment-test'.1.2000.'Test leaf Segment Mode Get Id')
Copy the code

Compilation project:

cd Leaf
mvn clean install -DskipTests
Copy the code

Operating Projects:

cd leaf-server
mvn spring-boot:run
Copy the code

Project started and completed:

Tips: I am using mysql8.0, so I have modified the connector version of mysql and druid version. The JDK version uses 11

Testing:

#segment
curl http://localhost:8080/api/segment/get/leaf-segment-test
#snowflake
curl http://localhost:8080/api/snowflake/get/test
Copy the code

It’s totally usable.

Tips: The code is self-analyzed, and it’s pretty much the same. There was an error in the code Github compilation command which has been corrected here.

3. Summary

The Leaf-Segment database schema combines the advantages of database and memory generation. Represents an enhancement of the Redis implementation in the previous article. It can be serviced briefly after a database failure, but for how long depends on step. If the step size is set too large, the service can be provided for a long time even if the database fails. But it can also lead to a lot of waste.

Leaf-snowflake uses ZK to assign workids. Resolved the need to specify manually.

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you

References:

  • Tech.meituan.com/2017/04/21/…
  • Github.com/Meituan-Dia…