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}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148788.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • web前端开发面试中常见的算法题(JS)

    web前端开发面试中常见的算法题(JS)前言最近在准备秋招,做过了大大小小的公司的面试题,发现除了基础知识外,算法还是挺重要的。特意整理了一些常见的算法题,添加了自己的理解并实现。除此之外,建议大家还可以刷刷《剑指offer》(但我还没刷完?,任重道远呐)。此外,左神在牛客网上也有算法课程,听了基础班的感觉还不错,起码让我这个算法小白也能快速地理解了很多问题,知识付费的时代,这个真的是良心课程了。就我个人而言的话,平时为了解决一…

    2022年6月29日
    33
  • YUV 简介及使用

    YUV 简介及使用一、YUV简介YUV:是一种颜色编码方法,常使用在各个视频处理组件中Y’UV,YCbCr,YPbPr等专有名词都可以称为YUV,彼此有重叠Y表示明亮度(单取此通道即可得灰度图),U和V则是色度、浓度主流的采样方式有三种,YUV4:4:4,YUV4:2:2,YUV4:2:0可以根据其采样格式来从码流中还原每个像素点的YUV值,进而通过YUV…

    2022年7月16日
    15
  • tracker服务器地址(每日更新)_hypixel服务器地址

    tracker服务器地址(每日更新)_hypixel服务器地址https://dns.icoa.cn/tracker/udp://tracker.tiny-vps.com:6969/announcehttps://1337.abcvg.info/announcehttps://tracker.fastdownload.xyz:443/announcehttps://opentracker.xyz:443/announceh…

    2022年10月1日
    3
  • 前端面试题angular_Vue前端面试题

    前端面试题angular_Vue前端面试题Angular1,ng-if跟ng-show/hide的区别有哪些?第一点区别是,ng-if在后面表达式为true的时候才创建这个dom节点,ng-show是初始时就创建了,用display:block和display:none来控制显示和不显示。第二点区别是,ng-if会(隐式地)产生新作用域,ng-switch、ng-include等会动态创建一块界面的…

    2022年10月10日
    3
  • 手眼标定算法Tsai-Lenz代码实现(Python、C++、Matlab)

    手眼标定算法Tsai-Lenz代码实现(Python、C++、Matlab)上一节介绍了手眼标定算法Tsai的原理,这一节介绍算法的代码实现,分别有Python、C++、Matlab版本的算法实现方式。该算法适用于将相机装在手抓上和将相机装在外部两种情况论文已经传到git上,地址:https://gitee.com/ohhuo/handeye-tsai如果你要进行手眼标定,可以参考我的其他文章:手眼标定-基础使用手眼标定-JAKA机械臂手眼标定-AUBO机械臂手眼标定-Aruco使用与相机标定手眼标定-注意事项Python版本使用前需要安装库:pip3

    2022年5月20日
    41
  • Visual Studio 2008/2010中Xaml开发格式设置技巧

    Visual Studio 2008/2010中Xaml开发格式设置技巧

    2021年8月6日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号