日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

id 生成器(id软件是做什么用的)

發布時間:2023/12/19 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 id 生成器(id软件是做什么用的) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

應用場景

  1. 全局流水號
  1. 區分前后臺
  2. 包含日期,可以從流水號一眼看出日期
  3. 不需要顯示在前臺,可以在后臺顯示
  4. 唯一標識每一次請求
  1. 訂單號 , 優惠券號碼等電商業務相關

參考電商訂單號設計的資料,自定義實現

  1. 數據庫主鍵
  1. [單調]遞增
  2. 可能考慮分庫分表
  3. 使用 美團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软件是做什么用的)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。