rain
rain copied to clipboard
Rain ID generator is a distributed ID generation system, easy to use, high performance, high availability. Segmented mode and snowflake algorithm mode are provided
rain
Distributed global ID generation service, ID generation is divided into two modesï¼
- segment
- snowflake
How to use see the following introduction.
Quick Start
1. Install dependencies
- JDK 11
- MySQL8
- Maven 3.8.5
2. Database initialization
2.1 Create table
Run the sql script to create the database and tablesï¼
DROP DATABASE IF EXISTS `uidgenerator`;
CREATE DATABASE `uidgenerator` ;
use `uidgenerator`;
DROP TABLE IF EXISTS mxsm_allocation;
CREATE TABLE `mxsm_allocation` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主é®ID',
`biz_code` varchar(128) COLLATE utf8mb4_general_ci NOT NULL COMMENT 'ä¸å¡ç¼ç (ç¨æ·ID,使ç¨ä¸å¡æ¹ç¼ç )',
`max_id` bigint NOT NULL DEFAULT '1' COMMENT 'æ大å¼',
`step` int NOT NULL COMMENT 'æ¥é¿',
`description` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '说æ',
`create_time` timestamp NOT NULL COMMENT 'å建æ¶é´',
`update_time` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ´æ°æ¶é´',
PRIMARY KEY (`id`),
UNIQUE KEY `biz_code_index` (`biz_code`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
DROP TABLE IF EXISTS mxsm_snowfalke_node;
CREATE TABLE `mxsm_snowfalke_node` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主é®ID',
`host_name` bigint NOT NULL COMMENT 'IPå°å',
`port` int NOT NULL DEFAULT '1' COMMENT '端å£',
`deploy_env_type` enum('ACTUAL','CONTAINER') COLLATE utf8mb4_general_ci DEFAULT 'ACTUAL' COMMENT 'é¨ç½²ç¯å¢ç±»å',
`description` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '说æ',
`create_time` timestamp NOT NULL COMMENT 'å建æ¶é´',
`update_time` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ´æ°æ¶é´',
PRIMARY KEY (`id`),
UNIQUE KEY `mix_index` (`host_name`,`port`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
3. rain deployment and start
3.1 Via the provided package
Step 1ï¼Download binary package
It can be downloaded from the latest stable release page rain-server-1.0.1-SNAPSHOT.tar.gz
tar -zxvf rain-server-1.0.1-SNAPSHOT.tar.gz
cd rain-server-1.0.1-SNAPSHOT/
Step 2ï¼Modify conf/application.properties
Modify the database-related configuration in the application.properties configuration:
spring.datasource.url=jdbc:mysql://ip:port/uidgenerator?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=xxx
spring.datasource.password=xxxxx
Tips: make sure the database address, name, port number, username, and password are correct.
Step 3ï¼Start server
sh bin/start.sh
4. Segment mode UID generation configuration
Modify conf/application.properties
config | default value | explain |
---|---|---|
mxsm.uid.segment.threshold | 40 | In cache mode, when the local cache threshold is lower than or equal to 40%, the segment filling will be loaded to the database, and the value ranges from 0 to 100 |
mxsm.uid.segment.cache-size | 16 | Number of cached segments to load by default in cache mode |
The size of threshold and cache-size affects the frequency of segment obtained from the data. If cache-size is set too large, it will cause a waste of UID when the project is stopped for maintenance. But the cache-size is large enough that bizCode is loaded in memory before it can continue serving in the event of a database crashã
5. Snowflake pattern UID generation configuration
Modify conf/application.properties :
config | default value | explain |
---|---|---|
mxsm.uid.snowflake.timestamp-bits | 41 | The number of bits of timestamp for the snowflake algorithm |
mxsm.uid.snowflake.machine-id-bits | 10 | The number of bits in the machine id of the snowflake algorithm |
mxsm.uid.snowflake.sequence-bits | 12 | The number of bits in the snowflake algorithm sequence number |
mxsm.uid.snowflake.container | false | Whether the deployment is containerized |
mxsm.uid.snowflake.time-bits-second | false | timestamp Whether it is in seconds |
mxsm.uid.snowflake.epoch | 2022-05-01 | timestamp The relative time in the format yyyy-MM-dd and before the current time |
timestamp-bitsãmachine-id-bitsãsequence-bitsä¸ä¸ªä½æ°åå èµ·æ¥è¦çäº63ã
6. Java SDK
maven client dependenceï¼
<dependency>
<groupId>com.github.mxsm</groupId>
<artifactId>rain-uidgenerator-client</artifactId>
<version>${latest version}</version>
</dependency>
example:
UidClient client = UidClient.builder()
.setUidGeneratorServerUir("http://172.29.250.21:8080") //设置æå¡å°å
.setSegmentNum(10) //设置è·åçsegmentæ°é
.setThreshold(20) //设置éå¼
.isSegmentUidFromRemote(false) //设置æ¯å¦ç´æ¥ä»æå¡å¨éè¿Restfulæ¥å£çæ¹å¼è·å
.build();
long uid = client.getSegmentUid("mxsm");
long uidRemote = client.getSegmentUid("mxsm", true);
long snowflake = client.getSnowflakeUid();
Source Code Quick Start
Step 1ï¼ clone code
git clone https://github.com/mxsm/rain.git
cd rain
Step 2ï¼Modify application.properties in rain-uidgenerator-server
spring.datasource.url=jdbc:mysql://ip:port/uidgenerator?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=xxx
spring.datasource.password=xxxxx
Step 3ï¼maven package server
mvn clean package -DskipTests=true
Step 4ï¼Start server
java -Xms1g -Xmx1g -jar ./rain-uidgenerator-server/target/rain-uidgenerator-server-1.0.1-SNAPSHOT.jar
Documentation
TODO