业务描述:发起红包,规定好总金额100,红包个数10。发完红包后,1秒钟内100个人同时抢。
需要注意的点:
1.数据库瞬时压力过大,需采用缓存;
2.线程并发进行,避免超卖;
处理:使用redis配合Redission加锁的方式,sexnx也可实现。
表设计:
列依次为:红包总金额,领取总人数,当前领取红包金额,当前领取人数
pom.xml
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Redisson 实现分布式锁 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.8</version>
</dependency>
application.yml
spring:
redis:
database: 0
host: ${ip}
port: 6379
password: 123456
redisson:
address: redis://${ip}:6379
password: 123456
自动装载部分
RedisConfig.java
package com.example.cisum.config;
import lombok.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//参照StringRedisTemplate内部实现指定序列化器
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashValueSerializer(valueSerializer());
return redisTemplate;
}
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}
//使用Jackson序列化器
private RedisSerializer<Object> valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
RedissonConfig.java
package com.example.cisum.config;
import com.example.cisum.utils.RedisLockUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonConfig {
@Autowired
private RedissonProperties redssionProperties;
/**
* 单机模式自动装配
*
* @return
*/
@Bean
@ConditionalOnProperty(name = "redisson.address")
RedissonClient redissonSingle() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress(redssionProperties.getAddress())
.setTimeout(redssionProperties.getTimeout())
.setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
.setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
if (StringUtils.isNotBlank(redssionProperties.getPassword())) {
serverConfig.setPassword(redssionProperties.getPassword());
}
return Redisson.create(config);
}
/**
* 装配locker类,并将实例注入到RedissLockUtil中
*
* @return
*/
@Bean
RedisLockUtil redissLockUtil(RedissonClient redissonClient) {
RedisLockUtil redissLockUtil = new RedisLockUtil();
redissLockUtil.setRedissonClient(redissonClient);
return redissLockUtil;
}
}
工具类:
RedisLockUtil.java
package com.example.cisum.utils;
import org.redisson.api.RLock;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import java.util.concurrent.TimeUnit;
/**
* redis分布式锁帮助类
*
* @author 姜通通
*/
public class RedisLockUtil {
@Autowired
private static RedissonClient redissonClient;
public void setRedissonClient(RedissonClient locker) {
redissonClient = locker;
}
/**
* 加锁
*
* @param lockKey
* @return
*/
public static RLock lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock();
return lock;
}
/**
* 释放锁
*
* @param lockKey
*/
public static void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
/**
* 释放锁
*
* @param lock
*/
public static void unlock(RLock lock) {
lock.unlock();
}
/**
* 带超时的锁
*
* @param lockKey
* @param timeout 超时时间 单位:秒
*/
public static RLock lock(String lockKey, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, TimeUnit.SECONDS);
return lock;
}
/**
* 带超时的锁
*
* @param lockKey
* @param unit 时间单位
* @param timeout 超时时间
*/
public static RLock lock(String lockKey, TimeUnit unit, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
return lock;
}
/**
* 尝试获取锁
*
* @param lockKey
* @param waitTime 最多等待时间
* @param leaseTime 上锁后自动释放锁时间
* @return
*/
public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
} catch (InterruptedException e) {
return false;
}
}
/**
* 尝试获取锁
*
* @param lockKey
* @param unit 时间单位
* @param waitTime 最多等待时间
* @param leaseTime 上锁后自动释放锁时间
* @return
*/
public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(buildKey(lockKey));
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (InterruptedException e) {
return false;
}
}
/**
* 初始红包数量
*
* @param key
* @param count
*/
public void initCount(String key, int count) {
RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");
mapCache.putIfAbsent(key, count, 3, TimeUnit.DAYS);
}
/**
* 递增
*
* @param key
* @param delta 要增加几(大于0)
* @return
*/
public int incr(String key, int delta) {
RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return mapCache.addAndGet(key, 1);//加1并获取计算后的值
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public int decr(String key, int delta) {
RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return mapCache.addAndGet(key, -delta);//加1并获取计算后的值
}
private static String buildKey(String key) {
return DigestUtils.md5DigestAsHex(key.getBytes());
}
}
RedisUtil.java
package com.example.cisum.utils;
/**
* Redis工具类
*
* @author 姜通通
* @date 2021年5月22日
*/
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public final class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// ================================String=================================
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(buildKey(key), value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(buildKey(key), value, time, TimeUnit.SECONDS);
} else {
set(buildKey(key), value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(buildKey(key));
}
public <T>T get(String key,Class<T> obj) {
if(key == null)return null;
Object o = redisTemplate.opsForValue().get(buildKey(key));
if(ObjectUtils.isEmpty(o))return null;
T t = JSONObject.parseObject(o.toString(), obj);
return t;
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(buildKey(key), time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(buildKey(key), TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(buildKey(key));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key
* @param delta 要增加几(大于0)
* @return
*/
public long increment(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(buildKey(key), delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decrement(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(buildKey(key), -delta);
}
// ================================zSet=================================
/**
* 添加元素,有序集合是按照元素的score值由小到大排列
*
* @param key
* @param value
* @param score
* @return
*/
public Boolean zAdd(String key, String value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}
/**
* 增加元素的score值,并返回增加后的值
*
* @param key
* @param value
* @param delta
* @return
*/
public Double zIncrementScore(String key, String value, double delta) {
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
}
/**
* 获取集合的元素, 从大到小排序
*
* @param key
* @param start
* @param end
* @return
*/
public Set<Object> zReverseRange(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRange(key, start, end);
}
/**
* 获取集合的元素, 从大到小排序, 并返回score值
*
* @param key
* @param start
* @param end
* @return
*/
public Set<ZSetOperations.TypedTuple<Object>> zReverseRangeWithScores(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}
// ================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
*/
public Object hget(String key, String item) {
try {
return redisTemplate.opsForHash().get(buildKey(key), item);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
try {
return redisTemplate.opsForHash().entries(buildKey(key));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(buildKey(key), map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(buildKey(key), map);
if (time > 0) {
expire(buildKey(key), time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(buildKey(key), item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(buildKey(key), item, value);
if (time > 0) {
expire(buildKey(key), time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(buildKey(key), item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(buildKey(key), item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(buildKey(key), item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(buildKey(key), item, -by);
}
private static String buildKey(String key) {
return DigestUtils.md5DigestAsHex(key.getBytes());
}
}
实体:RedPacket.java
package com.example.cisum.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("tb_red_packet")
public class RedPacket {
private Long id;//主键
private int totalAmount;//红包总金额
private int totalNum;//红包总个数
private int actualAmount;//实际抢红包金额
private int actualNum;//实际抢红包个数
}
业务类:RedPacketServiceImpl.java
package com.example.cisum.service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cisum.domain.RedPacket;
import com.example.cisum.mapper.RedPcketMapper;
import com.example.cisum.utils.RedisUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedPacketServiceImpl extends ServiceImpl<RedPcketMapper, RedPacket> implements RedPacketService {
@Autowired(required = false)
private RedPcketMapper redPcketMapper;
@Autowired
private RedisUtil redisUtil;
@Autowired
private RedissonClient redissonClient;
private String createLcokKey(long id) {
return String.format("LOCK:%s:%d", RedPacket.class, id);
}
private String createCacheKey(long id) {
return String.format("CACHE:%s:%d", RedPacket.class, id);
}
public void initRedPacket(long id) {
RedPacket redPacket = redPcketMapper.selectById(id);
redisUtil.set(createCacheKey(id), JSONObject.toJSONString(redPacket));
}
public RedPacket viewRedPacket(long id) {
return redisUtil.get(createCacheKey(id), RedPacket.class);
}
public int start(long id) {
RedPacket redPacket = redisUtil.get(createCacheKey(id), RedPacket.class);
if (redPacket.getActualAmount() < redPacket.getTotalAmount() && redPacket.getActualNum() < redPacket.getTotalNum()) {
redPacket.setActualAmount(redPacket.getActualAmount() + 10);
redPacket.setActualNum(redPacket.getActualNum() + 1);
redisUtil.set(createCacheKey(id), JSONObject.toJSONString(redPacket));
return 10;
}
return 0;
}
public int start2(long id) {
RLock lock = redissonClient.getLock(createLcokKey(id));
try {
boolean tryLock = lock.tryLock(5, 10, TimeUnit.SECONDS);
if (tryLock) {
RedPacket redPacket = redisUtil.get(createCacheKey(id), RedPacket.class);
if (redPacket.getActualAmount() < redPacket.getTotalAmount() && redPacket.getActualNum() < redPacket.getTotalNum()) {
redPacket.setActualAmount(redPacket.getActualAmount() + 10);
redPacket.setActualNum(redPacket.getActualNum() + 1);
redisUtil.set(createCacheKey(id), JSONObject.toJSONString(redPacket));
return 10;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return 0;
}
}
控制层:RedPacketController.java
package com.example.cisum.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.cisum.domain.RedPacket;
import com.example.cisum.service.RedPacketService;
import com.example.cisum.utils.RedisLockUtil;
import com.example.cisum.utils.RedisUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
public class RedPacketController {
@Autowired
private RedPacketService redPacketService;
@Autowired
private RedisUtil redisUtils;
@Autowired
private RedissonClient redissonClient;
@GetMapping("list/{id}")
public String list(@PathVariable Long id) {
return JSONObject.toJSONString(redPacketService.getById(id));
}
@GetMapping("initRedPacket/{id}")
public String initRedPacket(@PathVariable Long id) {
redPacketService.initRedPacket(id);
return JSONObject.toJSONString(redPacketService.viewRedPacket(id));
}
@GetMapping("start/{id}")
public int start(@PathVariable Long id) {
return redPacketService.start(id);
}
@GetMapping("start2/{id}")
public int start2(@PathVariable Long id) {
return redPacketService.start2(id);
}
private int doStart(Long id) {
int money = 0;
boolean res = false;
try {
/**
* 获取锁
*/
res = RedisLockUtil.tryLock(id + "", TimeUnit.SECONDS, 3, 10);
if (res) {
Object redpacket = redisUtils.get("RED:" + id);
if (redpacket == null) {
return 0;
}
RedPacket redPacket = JSONObject.parseObject(redpacket.toString(), RedPacket.class);
if (redPacket.getActualAmount() < redPacket.getTotalAmount() && redPacket.getActualNum() < redPacket.getTotalNum()) {
redPacket.setActualAmount(redPacket.getActualAmount() + 10);
redPacket.setActualNum(redPacket.getActualNum() + 1);
redisUtils.set("RED:" + id, JSONObject.toJSONString(redPacket));
return 10;
}
} else {
System.out.println(Thread.currentThread().getName() + "未获取到锁");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放锁
if (res) {
RedisLockUtil.unlock(id + "");
}
}
return 0;
}
}
测试:
初始化红包:http://localhost:8081/initRedPacket/1
设置JMeter