# springboot-redis-lua
**Repository Path**: lh1293/springboot-redis-lua
## Basic Information
- **Project Name**: springboot-redis-lua
- **Description**: springboot+redis+lua实现商品秒杀-demo
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 3
- **Created**: 2022-03-18
- **Last Updated**: 2023-11-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Springboot+Redis+Lua
## 介绍
springboot+redis+lua实现商品秒杀demo
经过ab测试,基本解决了超卖问题和库存遗留问题
没有加持久化,只是一个demo,实际项目要复杂的多
## 项目
> 版本说明
* springboot 2.6.4
* 如果使用的是2.0之前的版本,spring-boot-starter-data-redis使用的jedis,需要加上连接池,要不然会有超时问题
* 2.0之后的版本,spring-boot-starter-data-redis使用的是Lettuce,是基于Netty的
* redis 6.2.6
* 系统 centos7
> 主体代码
下面是完整案例,包括了超卖问题的两种解决方式,如果使用redis事务只解决了超卖,没有解决库存遗留
==使用lua既可以解决超卖问题,又可以解决库存遗留问题==
pom.xml
```xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.4
com.miaosha
miaosha
0.0.1-SNAPSHOT
miaosha
miaosha
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-thymeleaf
src/main/resources
**/*.*
org.springframework.boot
spring-boot-maven-plugin
```
application.properties
```properties
#设置thymeleaf模板引擎的前后缀
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#关闭模板缓存
spring.thymeleaf.cache=false
spring.redis.host=192.168.130.100
spring.redis.port=6379
#redis数据库索引
spring.redis.database=0
```
RedisConfig
```java
package com.miaosha.miaosha.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.CacheManager;
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.core.io.ClassPathResource;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.support.ResourceScriptSource;
import java.time.Duration;
import java.util.List;
@EnableCaching//开启缓存
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
//自定义序列化机制
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
RedisTemplate template = new RedisTemplate<>();
RedisSerializer redisSerializer = new StringRedisSerializer();
//使用JackSon的redis序列化机制
Jackson2JsonRedisSerializer