金华做网站,wordpress怎么进行页面修改,装修设计公司网站有哪些,开发网站网络公司排行分布式ID生成方案
UUID数据库自增号段模式Redis实现雪花算法#xff08;SnowFlake#xff09;百度Uidgenerator美团Leaf滴滴TinyID
本文重点介绍能够ID自增的Leaf和TinyID
号段模式
这种模式也是现在生成分布式ID的一种方法#xff0c;实现思路是会从数据库获取一个号段…分布式ID生成方案
UUID数据库自增号段模式Redis实现雪花算法SnowFlake百度Uidgenerator美团Leaf滴滴TinyID
本文重点介绍能够ID自增的Leaf和TinyID
号段模式
这种模式也是现在生成分布式ID的一种方法实现思路是会从数据库获取一个号段范围比如[1,1000]生成1到1000的自增ID加载到内存中建表结构如
CREATE TABLE id_generator (id int(10) NOT NULL,max_id bigint(20) NOT NULL COMMENT 当前最大id,step int(20) NOT NULL COMMENT 号段的布长,biz_type int(20) NOT NULL COMMENT 业务类型,version int(20) NOT NULL COMMENT 版本号,PRIMARY KEY (id)
) biz_type 不同业务类型 max_id 当前最大的id step 代表号段的步长 version 版本号就像MVCC一样可以理解为乐观锁 等ID都用了再去数据库获取然后更改最大值 update id_generator set max_id #{max_idstep}, version version 1 where version # {version} and biz_type XXX 优点有比较成熟的方案像百度Uidgenerator美团Leaf 缺点依赖于数据库实现 美团Leaf
Leaf 提供两种生成的ID的方式号段模式(Leaf-segment)和snowflake模式(Leaf-snowflake。你可以同时开启两种方式也可以指定开启某种方式默认两种方式为关闭状态。
git位置
GitHub - Meituan-Dianping/Leaf: Distributed ID Generate Service
介绍文档
https://github.com/Meituan-Dianping/Leaf/blob/master/README_CN.md
创建数据表
CREATE DATABASE leaf
CREATE TABLE leaf_alloc (biz_tag varchar(128) NOT NULL DEFAULT ,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)
) ENGINEInnoDB;insert into leaf_alloc(biz_tag, max_id, step, description) values(leaf-segment-test, 1, 2000, Test leaf Segment Mode Get Id)
获取项目
git clone gitgithub.com:Meituan-Dianping/Leaf.git
升级mysql驱动
如果连接的是mysql1.8以上需要升级mysql驱动分别是
Leaf/poe.xml以及 leaf_core/poe.xml
# Leaf/poe.xml
mybatis-spring.version1.2.5/mybatis-spring.version#leaf_core/poe.xmldependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.25/version/dependency修改配置
下面是号段模式的配置
leaf_server/resouces/leaf.properties
leaf.namecom.sankuai.leaf.opensource.test
leaf.segment.enabletrue
leaf.jdbc.urljdbc:mysql://127.0.0.1:3306/leaf?useSSLfalse
leaf.jdbc.usernameroot
leaf.jdbc.password123456leaf.snowflake.enablefalse
#leaf.snowflake.zk.address
#leaf.snowflake.port
打包 cd leaf mvn clean install -DskipTests 运行 cd leaf-server #mvn方式 mvn spring-boot:run #脚本方式 sh deploy/run.sh 测试 #segment curl http://localhost:8080/api/segment/get/leaf-segment-test #snowflake curl http://localhost:8080/api/snowflake/get/test 监控页面 号段模式http://localhost:8080/cache #注意添加一个biz_type以后10s以后生效。 滴滴TinyID
github位置
GitHub - didi/tinyid: ID Generator id生成器 介绍文档
Home · didi/tinyid Wiki · GitHub 获取项目
git clone https://github.com/didi/tinyid.git
创建数据表
tinyid-server/db.sql
CREATE TABLE tiny_id_info (id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 自增主键,biz_type varchar(63) NOT NULL DEFAULT COMMENT 业务类型唯一,begin_id bigint(20) NOT NULL DEFAULT 0 COMMENT 开始id仅记录初始值无其他含义。初始化时begin_id和max_id应相同,max_id bigint(20) NOT NULL DEFAULT 0 COMMENT 当前最大id,step int(11) DEFAULT 0 COMMENT 步长,delta int(11) NOT NULL DEFAULT 1 COMMENT 每次id增量,remainder int(11) NOT NULL DEFAULT 0 COMMENT 余数,create_time timestamp NOT NULL DEFAULT 2010-01-01 00:00:00 COMMENT 创建时间,update_time timestamp NOT NULL DEFAULT 2010-01-01 00:00:00 COMMENT 更新时间,version bigint(20) NOT NULL DEFAULT 0 COMMENT 版本号,PRIMARY KEY (id),UNIQUE KEY uniq_biz_type (biz_type)
) ENGINEInnoDB AUTO_INCREMENT1 DEFAULT CHARSETutf8 COMMENT id信息表;CREATE TABLE tiny_id_token (id int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 自增id,token varchar(255) NOT NULL DEFAULT COMMENT token,biz_type varchar(63) NOT NULL DEFAULT COMMENT 此token可访问的业务类型标识,remark varchar(255) NOT NULL DEFAULT COMMENT 备注,create_time timestamp NOT NULL DEFAULT 2010-01-01 00:00:00 COMMENT 创建时间,update_time timestamp NOT NULL DEFAULT 2010-01-01 00:00:00 COMMENT 更新时间,PRIMARY KEY (id)
) ENGINEInnoDB AUTO_INCREMENT1 DEFAULT CHARSETutf8 COMMENT token信息表;INSERT INTO tiny_id_info (id, biz_type, begin_id, max_id, step, delta, remainder, create_time, update_time, version)
VALUES(1, test, 1, 1, 100000, 1, 0, 2018-07-21 23:52:58, 2018-07-22 23:19:27, 1);INSERT INTO tiny_id_info (id, biz_type, begin_id, max_id, step, delta, remainder, create_time, update_time, version)
VALUES(2, test_odd, 1, 1, 100000, 2, 1, 2018-07-21 23:52:58, 2018-07-23 00:39:24, 3);INSERT INTO tiny_id_token (id, token, biz_type, remark, create_time, update_time)
VALUES(1, 0f673adf80504e2eaa552f5d791b644c, test, 1, 2017-12-14 16:36:46, 2017-12-14 16:36:48);INSERT INTO tiny_id_token (id, token, biz_type, remark, create_time, update_time)
VALUES(2, 0f673adf80504e2eaa552f5d791b644c, test_odd, 1, 2017-12-14 16:36:46, 2017-12-14 16:36:48);
修改配置
cd tinyid-server/src/main/resources/offline vi application.properties
server.port9999
server.context-path/tinyidbatch.size.max100000#datasource.tinyid.namesprimary
#如果希望数据库能够高可用可以设置多个不同节点两个节点上的数据保持一致。
#注意添加配置的时候多个节点都要添加
datasource.tinyid.namesprimary,secondary
datasource.tinyid.typeorg.apache.tomcat.jdbc.pool.DataSourcedatasource.tinyid.primary.driver-class-namecom.mysql.jdbc.Driver
datasource.tinyid.primary.urljdbc:mysql://localhost:3306/db1?autoReconnecttrueuseUnicodetruecharacterEncodingUTF-8
datasource.tinyid.primary.usernameroot
datasource.tinyid.primary.password123456
#datasource.tinyid.primary.testOnBorrowfalse
#datasource.tinyid.primary.maxActive10datasource.tinyid.secondary.driver-class-namecom.mysql.jdbc.Driver
datasource.tinyid.secondary.urljdbc:mysql://localhost:3306/db2?autoReconnecttrueuseUnicodetruecharacterEncodingUTF-8
datasource.tinyid.secondary.usernameroot
datasource.tinyid.secondary.password123456
datasource.tinyid.secondary.testOnBorrowfalse
datasource.tinyid.secondary.maxActive10
打包 cd tinyid mvn clean install -DskipTests #或者 cd tinyid-server/ sh build.sh offline 运行 cd tinyid-server/ #sh build.sh offline java -jar output/tinyid-server-xxx.jar 测试
nextId:
curl http://localhost:9999/tinyid/id/nextId?bizTypetesttoken0f673adf80504e2eaa552f5d791b644c
response:{data:[2],code:200,message:}nextId Simple:
curl http://localhost:9999/tinyid/id/nextIdSimple?bizTypetesttoken0f673adf80504e2eaa552f5d791b644c
response: 3with batchSize:
curl http://localhost:9999/tinyid/id/nextIdSimple?bizTypetesttoken0f673adf80504e2eaa552f5d791b644cbatchSize10
response: 4,5,6,7,8,9,10,11,12,13Get nextId like 1,3,5,7,9...
bizTypetest_odd : delta is 2 and remainder is 1
curl http://localhost:9999/tinyid/id/nextIdSimple?bizTypetest_oddbatchSize10token0f673adf80504e2eaa552f5d791b644c
response: 3,5,7,9,11,13,15,17,19,21 客户端使用
客户端打包 cd tinyid mvn clean install -DskipTests #tinyid-client\target\tinyid-client-0.1.0-SNAPSHOT.jar就是可以使用的客户端 客户端配置文件
tinyid_client.properties
将tinyid_client.properties放在resouces下面
tinyid.serverlocalhost:9999
tinyid.token0f673adf80504e2eaa552f5d791b644c使用
public class ClientTest {Testpublic void testNextId() {for (int i 0; i 100000; i) {Long id TinyId.nextId(test_odd);System.out.println(current id is: id);}}
}注意每次重启程序id就会往前加一个step不管你有没有用完如果程序经常重启step不宜设置过大。但是step不宜设置过小否则与数据库交互过于频繁。 来源
8种分布式ID生成方案汇总