1,全局唯一ID
在分布式系统下生成全局唯一ID的工具,满足 唯一性,高可用,高性能,递增性,安全性
uuid生成工具类
/*
* 文 件 名:IdsUtil.java
* 系统名称:风险管控平台
* Copyright@2003-2019 State Grid Corporation of China, All Rights Reserved
* 版本信息:V1.0
* 版 权:NARI
*/
package com.study.utils;
import java.security.SecureRandom;
import java.util.UUID;
/**
* 概述:id生成工具类
* 功能:
* 作者:15657
* 创建时间:2019-05-29 14:20
*/
public class IdsUtil {
public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"};
/**
* 生成8位uuid
*
* @return
*/
public static String getUUID8() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
/**
* 生成16位uuid
*
* @return
*/
public static String getUUID16() {
String uuid = UUID.randomUUID().toString();
char[] cs = new char[32];
char c = 0;
for (int i = uuid.length() / 2, j = 1; i --> 0;) {
if ((c = uuid.charAt(i)) != '-') {
cs[j++] = c;
}
}
String uid = String.valueOf(cs);
return uid.trim();
}
/**
* 生成32位uuid
*
* @return
*/
public static String getUUID32() {
return UUID.randomUUID().toString().replace("-", "");
}
//生成指定长度字符串
public static String getRandomString(int length){
String base="0123456789";
SecureRandom random = new SecureRandom();
StringBuffer sb = new StringBuffer();
for(int i=0;i<length;i++){
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}
1.1 测试
2,复现重复减库存现象
@Resource
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/deductStock_demo01")
public ResponseResult deductStock(){
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
if(stock > 0){
int realStock = stock - 1;
stringRedisTemplate.opsForValue().set("stock",realStock+"");
log.info("库存扣除成功,剩余库存:{}",realStock);
}else {
log.info("库存扣除失败,库存不足");
}
return ResponseResult.ok().message("end");
}
2.1 测试
3 使用 synchronized解决重复减库存
public ResponseResult deductStock_demo01_synchronized(){
synchronized (this){
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
if(stock > 0){
int realStock = stock - 1;
stringRedisTemplate.opsForValue().set("stock",realStock+"");
log.info("库存扣除成功,剩余库存:{}",realStock);
}else {
log.info("库存扣除失败,库存不足");
}
}
return ResponseResult.ok().message("end");
}
3.1 测试
3,分布式环境中超卖的问题
吧项目打成jar包,启动两个服务,发现任然出现超卖问题,说明synchronized只能再单体服务中生效,分布式服务中起不到锁的作用
3.1,分布式锁解决超卖问题
public ResponseResult deductStock_demo01_fbs(){
String lockKey = "lockKey";
String clientId = IdsUtil.getUUID32()+UUID.randomUUID().toString();
//加 try catch 是为了防止死锁
try {
//判断 lockKey 是否存在,不存在则添加,存在则不做操作
Boolean res = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, clientId,30,TimeUnit.SECONDS);
if(!res){
// log.info("error_redis_stock");
return ResponseResult.error().message("error_redis_stock");
}
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
if(stock > 0){
int realStock = stock - 1;
stringRedisTemplate.opsForValue().set("stock",realStock+"");
log.info("库存扣除成功,剩余库存:{}",realStock);
}else {
log.info("库存扣除失败,库存不足");
}
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
if(clientId.equals(stringRedisTemplate.opsForValue().get(lockKey))){
log.info("clientId:{}",clientId);
stringRedisTemplate.delete(lockKey);
}
}
return ResponseResult.ok().message("end");
}
3.2 测试
4,使用redisson实现分布式锁
4.1 引入依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.3</version>
</dependency>
4.2 新建redisson配置类
package com.study.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class MyRedissonConfig {
/**
* 所有对Redisson的使用都是通过RedissonClient
* @return
* @throws IOException
*/
@Bean(destroyMethod="shutdown")
public RedissonClient redisson() throws IOException {
//1、创建配置
Config config = new Config();
//指定使用单节点配置
config.useSingleServer().setAddress("redis://127.0.0.1:6931");
//2、根据Config创建出RedissonClient实例
//Redis url should start with redis:// or rediss://
RedissonClient redissonClient = Redisson.create(config);
return redissonClient;
}
}
4.3 代码演示
@Resource
Redisson redisson;
public ResponseResult deductStock_redisson(){
String lockKey = "lockKey";
//获取锁对象
RLock redissonLock = redisson.getLock(lockKey);
//加 try catch 是为了防止死锁
try {
redissonLock.lock(); //加锁 类似 setIfAbsent(lockKey, clientId,30,TimeUnit.SECONDS);
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
if(stock > 0){
int realStock = stock - 1;
stringRedisTemplate.opsForValue().set("stock",realStock+"");
log.info("库存扣除成功,剩余库存:{}",realStock);
}else {
log.info("库存扣除失败,库存不足");
}
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
redissonLock.unlock();
//
}
return ResponseResult.ok().message("end");
}