生成订单号
生成唯一订单号的方法有很多种,包括uuid,雪花算法等等,还可以利用数据库的约束生成唯一的id,比如自增,但是数据库的性能比较低,如果使用数据库来生成订单号效率比较低。我们可以考虑使用redis来生成唯一的订单号。redis天然单线程,又支持lua脚本原子性操作。所以很好实现这些功能。
代码实现
import jakarta.annotation.resource;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.core.script.defaultredisscript;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
import java.time.localdatetime;
import java.time.format.datetimeformatter;
import java.time.format.datetimeformatterbuilder;
import java.time.temporal.chronofield;
import java.util.collections;
@restcontroller
public class distr_lock {
@resource
private redistemplate<string,string> redistemplate;
@getmapping("/order")
public void order() {
for (int i = 0; i < 50; i++) {
new thread(new runnable() {
@override
public void run() {
for (int i = 0; i < 20; i++) {
string s = generateorderid();
system.out.println(s);
}
}
}).start();
}
}
/**
* 我们可以采用这样一种策略,
* 时间精确到秒+7位数的自增数字
* 这样可以实现同一个用户在同一秒下单的不超过1000万单
* 然后使用时间在redis创建一个key,进行自增,然后30秒过期
*/
public string generateorderid(){
datetimeformatter formatter = new datetimeformatterbuilder()
.appendvalue(chronofield.year, 4) // 年份,4位数字
.appendvalue(chronofield.month_of_year, 2) // 月份,2位数字
.appendvalue(chronofield.day_of_month, 2) // 日期,2位数字
.appendvalue(chronofield.hour_of_day, 2) // 小时,2位数字
.appendvalue(chronofield.minute_of_hour, 2) // 分钟,2位数字
.appendvalue(chronofield.second_of_minute, 2) // 秒,2位数字
.toformatter();
// 获取当前时间的字符串表示,不使用-或:作为分隔符
string currentdatetime = localdatetime.now().format(formatter);
// long count = getandexpirecounter(currentdatetime);
long count = getandexpirecounterbylua(currentdatetime);
string format = string.format("%07d", count);
return currentdatetime + format;
}
public long getandexpirecounter(string key) {
// 获取redis连接,以便执行事务
long increment = 0l;
increment = redistemplate.opsforvalue().increment(key);
if (increment!=null && increment == 1) {
// 设置过期时间,这里并没有使用事务或者lua脚本
// 因为我觉得上面的自增操作是原子性的,也就是肯定会得到一个值为1
// 如果说redis服务崩了,到这一步没有设置好过期时间,那么我们整个
// 服务也会崩溃,这个key就会一直存在于服务器,会出现这个问题,为了严谨
// 最好使用lua脚本编写,项目上线会保证更可靠的效果
redistemplate.expire(key, 30, java.util.concurrent.timeunit.seconds);
}
// 获取自增后的值
return increment;
}
/**
* 整个脚本的作用是:
* 1.检查一个键是否存在于redis中。
* 2.如果键不存在,则设置该键的值为1,并为其设置60秒的过期时间,然后返回1。
* 3.如果键已经存在,则将其值增加1,并返回增加后的新值。
* redis执行lua脚本时是原子的,这意味着在执行这个脚本的过程中,
* 不会有其他脚本或命令同时修改这个键,从而保证了操作的原子性和一致性。
* @param key
* @return
*/
public long getandexpirecounterbylua(string key) {
// 定义lua脚本
string script =
"if redis.call('exists', keys[1]) == 0 then " +
" redis.call('set', keys[1], 1) " +
" redis.call('expire', keys[1], 30) " +
" return 1 " +
"else " +
" return redis.call('incr', keys[1]) " +
"end";
// 使用redis的脚本执行功能
defaultredisscript<long> redisscript = new defaultredisscript<>();
redisscript.setscripttext(script);
redisscript.setresulttype(long.class);
// 执行脚本
return redistemplate.execute(redisscript, collections.singletonlist(key));
}到此这篇关于springboot3利用redis生成唯一订单号的实现示例的文章就介绍到这了,更多相关springboot3 redis生成唯一订单号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论