SpringBoot实现Java秒杀系统
直接service,我们会介绍两种秒杀模式
public interface GoodsService {
/**
* 通过lua脚本实现的秒杀
* @param skuCode 商品编码
* @param buyNum 购买数量
* @return 购买数量
*/
Long flashSellByLuaScript(String skuCode,int buyNum);
/**
* 通过redis 事务 实现的秒杀
* @param skuCode 商品编码
* @param buyNum 购买数量
* @return 购买数量
*/
Long flashSellByRedisWatch(String skuCode,int buyNum);
}
service实现类
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public Long flashSellByLuaScript(String skuCode,int num) {
//下面是lua脚本
String luaScript ="local buyNum = ARGV[1]\n" +
"local goodsKey = KEYS[1] \n" +
"local goodsNum = redis.call('get',goodsKey) \n" +
"if goodsNum >= buyNum \n" +
"then redis.call('decrby',goodsKey,buyNum) \n" +
"return buyNum \n" +
"else \n" +
"return '0'\n" +
"end\n" +
"\n" ;
DefaultRedisScript<String> re = new DefaultRedisScript<String>();
//设置脚本
re.setScriptText(luaScript);
//定义返回值类型,注意,如果没有这个定义,Spring不会返回结果
re.setResultType(String.class);
RedisSerializer<String> stringRedisSerializer = stringRedisTemplate.getStringSerializer();
//执行LUA脚本
String result = (String) stringRedisTemplate.execute(re, stringRedisSerializer, stringRedisSerializer, null);
return Long.valueOf(result);
}
@Override
public Long flashSellByRedisWatch(String skuCode,int num){
SessionCallback<Long> sessionCallback = new SessionCallback<Long>() {
@Override
public Long execute(RedisOperations operations) throws DataAccessException {
int result = num;
//redis 乐观锁
//我们观察商品编码是否发生改变
operations.watch(skuCode);
ValueOperations<String, String> valueOperations = operations.opsForValue();
String goodsNumStr = valueOperations.get(skuCode);
Integer goodsNum = Integer.valueOf(goodsNumStr);
//标记一个事务块的开始。
//事务块内的多条命令会按照先后顺序被放进一个队列当中,
//最后由 EXEC 命令原子性(atomic)地执行。
operations.multi();
if (goodsNum >= num) {
valueOperations.increment(skuCode, 0 - num);
} else {
result = 0;
}
//多条命令执行的结果集合
List exec = operations.exec();
if(exec.size()>0){
System.out.println(exec);
}
return (long) result;
}
};
return stringRedisTemplate.execute(sessionCallback);
}
**//省略 其他的方法**
controller
但是首先要向你的redis里面仍一个数据,key=‘xiaomi’,value=‘100’
@ApiOperation(value = "用事务秒杀测试接口", notes = "用事务秒杀测试接口")
@RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET)
@ResponseBody
public Long miaoTransaction() {
Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);
return res;
}
@ApiOperation(value = " 秒杀Lua测试接口", notes = "秒杀Lua测试接口")
@RequestMapping(value = "/miaoLua", method = RequestMethod.GET)
@ResponseBody
public Long miaoLua() {
Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);
System.out.println(res.toString());
return res;
}
然后就可以用jemeter并发访问了