id 生成器(id软件是做什么用的)
生活随笔
收集整理的這篇文章主要介紹了
id 生成器(id软件是做什么用的)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
應用場景
- 全局流水號
- 區分前后臺
- 包含日期,可以從流水號一眼看出日期
- 不需要顯示在前臺,可以在后臺顯示
- 唯一標識每一次請求
- 訂單號 , 優惠券號碼等電商業務相關
參考電商訂單號設計的資料,自定義實現
- 數據庫主鍵
- [單調]遞增
- 可能考慮分庫分表
- 使用 美團leaf
全局流水號
構成: 1/2/3-時間格式化-序號
1: auth
2: admin
3: api
例:
2-2020-10-28-22:58:58-3
代碼參考
https://gitee.com/sanren2016/shop-boot/tree/847b1c1d9037d8156d91c588561806c39edee63e/
效果
實現思路
使用全局攔截器+ThreadLocal,在preHandle中獲取流水號,流水號獲取代碼:
com.laolang.shop.common.data.mvc.trace.TraceComponent
美團 Leaf
參考: https://github.com/Meituan-Dianping/Leaf/blob/master/README_CN.md
git clone git@github.com:Meituan-Dianping/Leaf.git
git checkout feature/spring-boot-starter
cd leaf
mvn clean install -Dmaven.test.skip=true
引入時需要注意mybatis依賴和druid依賴的沖突
<dependency>
<artifactId>leaf-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>druid</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
<exclusion>
<artifactId>mybatis</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
</exclusions>
<groupId>com.sankuai.inf.leaf</groupId>
<version>1.0.1-RELEASE</version>
</dependency>
配置文件: leaf.properties
leaf.name=shop-boot-leaf
leaf.segment.enable=true
leaf.segment.url=jdbc:mysql://192.168.1.110:3306/shop_boot?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
leaf.segment.username=shopboot
leaf.segment.password=shopboot
leaf.snowflake.enable=false
#leaf.snowflake.address=
#leaf.snowflake.port=
建表語句
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`)
) ENGINE=InnoDB;
insert into leaf_alloc(biz_tag, max_id, step, description) values('leaf-segment-test', 1, 2000, 'Test leaf Segment Mode Get Id')
測試
package com.laolang.shop;
import com.sankuai.inf.leaf.service.SegmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
public class ComponentTest extends BaseComponentTest {
@Autowired
private SegmentService segmentService;
@Test
public void componentTest() {
System.out.println(segmentService.getId("leaf-segment-test"));
System.out.println(segmentService.getId("leaf-segment-test"));
System.out.println(segmentService.getId("leaf-segment-test"));
System.out.println(segmentService.getId("leaf-segment-test"));
System.out.println(segmentService.getId("leaf-segment-test"));
}
}
輸出
Result{id=1, status=SUCCESS}
Result{id=2, status=SUCCESS}
Result{id=3, status=SUCCESS}
Result{id=4, status=SUCCESS}
Result{id=5, status=SUCCESS}
總結
以上是生活随笔為你收集整理的id 生成器(id软件是做什么用的)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一张色环图教你搞定配色_24色环颜色调配
- 下一篇: 人工智能如何打造下一个爆款游戏