GithubHelp home page GithubHelp logo

mxsm / rain Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 5.0 181 KB

Rain ID generator is a distributed ID generation system, easy to use, high performance, high availability. Segmented mode and snowflake algorithm mode are provided

License: Apache License 2.0

Java 96.39% Shell 3.61%
distributed-systems snowflake spring-boot uid uidgenerator msyql high-availability

rain's Introduction

rain

Publish package to the Maven Central Repository and GitHub Packages

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

image-20220604145105893

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

rain's People

Contributors

antcarryele avatar mxsm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rain's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.