Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
1. Meituan Leaf Distributed ID Generator
The earliest requirements of Meituan Leaf are the order ID generation requirements of each business line. For specific Leaf design documents, see: leaf Meituan distributed ID generation service . Features of Leaf:
- Leaf relies on the database or ZK (the Snowflake implemented by ZK here is an enhancement to Snowflake's original way).
- Leaf provides two modes: Leaf-segment database mode and Leaf-snowflake mode.
- Optimized snowflake time callback. Fixed time callback issue.
2. Java implementation of Leaf
Meituan has implementation code on Github. Let's follow the code to build a local project and run it. And use it to see.
Clone code:
git clone https://github.com/Meituan-Dianping/Leaf.git
复制代码
Configuration:
Enter the leaf-server/src/main/resources/leaf.properties configuration file to set
configure | illustrate | Defaults |
---|---|---|
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 turn on leaf.segment.enable=true
. And leaf.snowflake.enable
because of the need to use zk temporarily not open. leaf.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
复制代码
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')
复制代码
Compile the project:
cd Leaf
mvn clean install -DskipTests
复制代码
Run the project:
cd leaf-server
mvn spring-boot:run
复制代码
Project start completed:
Tips: I am using mysql8.0, so I have modified the connector version of mysql and the version of druid, without modifying the error. The jdk version is using 11
test:
#segment
curl http://localhost:8080/api/segment/get/leaf-segment-test
#snowflake
curl http://localhost:8080/api/snowflake/get/test
复制代码
Totally usable.
Tips: The code is analyzed by itself, generally the same. There was an error in the code Github compilation command which has been corrected here.
3. Summary
Leaf-segment
The database schema combines the advantages of database and in-memory generation. It is equivalent to an enhancement of the Redis implementation in the previous article. Here, the service can be provided for a short time after a database error occurs, but how long the service is depends on step
. If the step size is set large, then 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
Use ZK to realize the assignment of workId. Addresses cases where manual specification is required.
I am an ant carrying an elephant. The article is helpful to you. Like and follow me. If the article is incorrect, please leave a comment~ Thank you
References: