目标:
了解订单超卖场景
步骤:
1、启动Redis环境2、 搭建两个demo,消费同一个redis的key
3、使用Apifox并发消费,看结果
一、启动Redis环境
使用虚拟机起 redis 或者你本地起一个redis也行,无所谓,能用即可
如果是虚拟机的redis,配置如下 本地demo连接虚拟机redis
并设置一个商品(String)的值:> set goods:001 100
二、搭建两个demo,消费同一个redis的key
程序:redis_01
应用相关配置:application.properties
# 应用名称
spring.application.name=redis_01
# 端口号
server.port=8080
#=========================redis相关配香========================
#Redis数据库索引(默认方0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=192.168.128.128
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)默认8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)默认-1
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接默认8
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接默犬认0
spring.redis.lettuce.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
Redis的配置类:RedisConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory factory){
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
超卖商品接口:GoodsController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GoodsController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${server.port}")
private String serverPort;
@GetMapping("/buy")
public String buyGoods(){
System.out.println(stringRedisTemplate.opsForValue().get("name"));
// 查看redis中库存数量
String res = stringRedisTemplate.opsForValue().get("goods:001");
int goodsNum = res == null ? 0 : Integer.parseInt(res);
if(goodsNum > 0 ){
int realNum = goodsNum - 1;
stringRedisTemplate.opsForValue().set("goods:001",String.valueOf(realNum));
System.out.println("服务端口:"+serverPort+":成功购买商品,库存仅剩:"+realNum);
return "服务端口:"+serverPort+":成功购买商品,库存仅剩:"+realNum;
}else{
System.out.println("服务端口:"+serverPort+":商品售罄");
}
return "服务端口:"+serverPort+":商品售罄";
}
}
程序:redis_02
应用相关配置:application.properties
# 应用名称
spring.application.name=redis_02
# 端口号
server.port=8081
#=========================redis相关配香========================
#Redis数据库索引(默认方0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=192.168.128.128
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)默认8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)默认-1
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接默认8
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接默犬认0
spring.redis.lettuce.pool.min-idle=0
超卖商品接口:GoodsController
@RestController
public class GoodsController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${server.port}")
private String serverPort;
@GetMapping("/buy")
public String buyGoods(){
// 查看redis中库存数量
String res = stringRedisTemplate.opsForValue().get("goods:001");
int goodsNum = res == null ? 0 : Integer.parseInt(res);
if(goodsNum > 0 ){
int realNum = goodsNum - 1;
stringRedisTemplate.opsForValue().set("goods:001",String.valueOf(realNum));
System.out.println("服务端口:"+serverPort+":成功购买商品,库存仅剩:"+realNum);
return "服务端口:"+serverPort+":成功购买商品,库存仅剩:"+realNum;
}else{
System.out.println("服务端口:"+serverPort+":商品售罄");
}
return "服务端口:"+serverPort+":商品售罄";
}
}
三、使用Apifox并发消费,看结果