在系统开发中,保证数据的唯一性是至关重要的一件事,目前开发中常用的方式有使用数据库的自增序列、uuid生成唯一编号、时间戳或者时间戳+随机数等。
在某些特定业务场景中,可能会要求我们使用特定格式的唯一编号,比如我有一张订单表(t_order),我需要生成“yewu(order)+日期(yyyymmdd)+序列号(00000000)”格式的订单编号,比如今天的日期是20200716,那我今天第一个订单号就是order2020071600000001、第二个订单号就是order2020071600000002,明天的日期是20200717,那么明天的第一个订单号就是order2020071700000001、第二个订单号就是order2020071700000002,以此类推。
今天介绍下如何使用redis生成唯一的序列号,其实主要思想还是利用redis单线程的特性,可以保证操作的原子性,使读写同一个key时不会出现不同的数据。以springboot项目为例,添加以下依赖。
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-lang3</artifactid>
<version>3.1</version>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
<version>${spring.boot.version}</version>
</dependency>
application.properties中配置redis,我本地redis没有设置密码,所以注释了密码这一行
server.port=9091 server.servlet.context-path=/ spring.redis.host=127.0.0.1 spring.redis.port=6379 #spring.redis.password=1234 spring.redis.database=0
创建sequenceservice类用于生成特定业务编号
package com.xiaochun.service;
import org.apache.commons.lang3.stringutils;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.support.atomic.redisatomiclong;
import org.springframework.stereotype.service;
import javax.annotation.resource;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
@service
public class sequenceservice {
private static logger logger = loggerfactory.getlogger(sequenceservice.class);
@resource
private redistemplate redistemplate;
//用作存放redis中的key
private static string order_key = "order_key";
//生成特定的业务编号,prefix为特定的业务代码
public string getorderno(string prefix){
return getseqno(order_key, prefix);
}
//sequenceservice类中公用部分,传入制定的key和prefix
private string getseqno(string key, string prefix)
{
calendar calendar = calendar.getinstance();
calendar.set(calendar.hour_of_day, 23);
calendar.set(calendar.minute, 59);
calendar.set(calendar.second, 59);
calendar.set(calendar.millisecond, 999);
//设置过期时间,这里设置为当天的23:59:59
date expiredate = calendar.gettime();
//返回当前redis中的key的最大值
long seq = generate(redistemplate, key, expiredate);
//获取当天的日期,格式为yyyymmdd
string date = new simpledateformat("yyyymmdd").format(expiredate);
//生成八为的序列号,如果seq不够八位,seq前面补0,
//如果seq位数超过了八位,那么无需补0直接返回当前的seq
string sequence = stringutils.leftpad(seq.tostring(), 8, "0");
if (prefix == null)
{
prefix = "";
}
//拼接业务编号
string seqno = prefix + date + sequence;
logger.info("key:{}, 序列号生成:{}, 过期时间:{}", key, seqno, string.format("%tf %tt ", expiredate, expiredate));
return seqno;
}
/**
* @param key
* @param expiretime <i>过期时间</i>
* @return
*/
public static long generate(redistemplate<?,?> redistemplate,string key,date expiretime) {
//redisatomiclong为原子类,根据传入的key和redis链接工厂创建原子类
redisatomiclong counter = new redisatomiclong(key,redistemplate.getconnectionfactory());
//设置过期时间
counter.expireat(expiretime);
//返回redis中key的值,内部实现下面详细说明
return counter.incrementandget();
}
}
接下来,启动项目,使用接口的形式访问,或者写test方法执行,就可以得到诸如order2020071600000001、order2020071600000002的编号,而且在高并发环境中也不会出现数据重复的情况。实现原理:上面生成特定业务编号主要分为三部分,如下图
前缀和日期部分,没什么需要解释的,主要是redis中的生成的序列号,而这需要依靠redisatomiclong来实现,先看下上面生成redis序列过程中发生了什么
- 获取redis中对应业务的key,生成过期时间expiretime
- 获取了redistemplate对象,通过该对象获取redisconnectionfactory对象
- 将key,redisconnectionfactory对象作为构造参数生成redisatomiclong对象,并设置过期时间
- 调用redisatomiclong的incrementandget()方法
看下redisatomiclong源码,当然只放一部分源码,不会放全部,redisatomiclong的结构,主要构造函数,和上面提到过的incrementandget()方法
public class redisatomiclong extends number implements serializable, boundkeyoperations<string> {
private static final long serialversionuid = 1l;
//redis中的key,用volatile修饰,获得原子性
private volatile string key;
//当前的key-value对象,根据传入的key获取value值
private valueoperations<string, long> operations;
//传入当前redistemplate对象,为redistemplate对象的顶级接口
private redisoperations<string, long> generalops;
public redisatomiclong(string rediscounter, redisconnectionfactory factory) {
this(rediscounter, (redisconnectionfactory)factory, (long)null);
}
private redisatomiclong(string rediscounter, redisconnectionfactory factory, long initialvalue) {
assert.hastext(rediscounter, "a valid counter name is required");
assert.notnull(factory, "a valid factory is required");
//初始化一个redistemplate对象
redistemplate<string, long> redistemplate = new redistemplate();
redistemplate.setkeyserializer(new stringredisserializer());
redistemplate.setvalueserializer(new generictostringserializer(long.class));
redistemplate.setexposeconnection(true);
//设置当前的redis连接工厂
redistemplate.setconnectionfactory(factory);
redistemplate.afterpropertiesset();
//设置传入的key
this.key = rediscounter;
//设置当前的redistemplate
this.generalops = redistemplate;
//获取当前的key-value集合
this.operations = this.generalops.opsforvalue();
//设置默认值,如果传入为null,则key获取operations中的value,如果value为空,设置默认值为0
if (initialvalue == null) {
if (this.operations.get(rediscounter) == null) {
this.set(0l);
}
//不为空则设置为传入的值
} else {
this.set(initialvalue);
}
}
//将传入key的value+1并返回
public long incrementandget() {
return this.operations.increment(this.key, 1l);
}
其实主要还是通过redis的自增序列来实现
到此这篇关于如何使用redis生成唯一编号及原理的文章就介绍到这了,更多相关redis生成唯一编号内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!