SpringBoot实现Java秒杀系统

项目是有地址的,我会放到文章的最后面

  1. 直接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);





}
  1. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用Spring Boot、RabbitMQ和Redis实现秒杀功能的示例代码: 首先,确保你的项目中已添加所需的依赖项,包括`spring-boot-starter-amqp`和`spring-boot-starter-data-redis`。 1. 创建一个用于处理秒杀请求的控制器(Controller): ```java @RestController @RequestMapping("/seckill") public class SeckillController { @Autowired private RabbitTemplate rabbitTemplate; @Autowired private RedisTemplate<String, Integer> redisTemplate; @PostMapping("/{productId}") public String seckill(@PathVariable int productId) { String userId = "user1"; // 假设当前用户ID为user1 // 判断用户是否已经秒杀过该商品 if (redisTemplate.opsForSet().isMember("seckill:" + productId, userId)) { return "您已经参与过该秒杀活动!"; } // 判断库存是否充足 int stock = redisTemplate.opsForValue().get("stock:" + productId); if (stock <= 0) { return "该商品已售罄!"; } // 发送秒杀消息到RabbitMQ rabbitTemplate.convertAndSend("seckill-exchange", "seckill.queue", userId + ":" + productId); return "秒杀请求已发送,请等待结果..."; } } ``` 2. 创建一个消息消费者来处理秒杀请求: ```java @Component public class SeckillConsumer { @Autowired private RedisTemplate<String, Integer> redisTemplate; @RabbitListener(queues = "seckill.queue") public void processSeckill(String message) { String[] parts = message.split(":"); String userId = parts[0]; int productId = Integer.parseInt(parts[1]); // 判断用户是否已经秒杀过该商品 if (redisTemplate.opsForSet().isMember("seckill:" + productId, userId)) { System.out.println("用户[" + userId + "]已经参与过该秒杀活动!"); return; } // 判断库存是否充足 int stock = redisTemplate.opsForValue().get("stock:" + productId); if (stock <= 0) { System.out.println("商品[" + productId + "]已售罄!"); return; } // 执行秒杀逻辑,减库存等操作... System.out.println("用户[" + userId + "]秒杀商品[" + productId + "]成功!"); // 将用户加入已秒杀集合 redisTemplate.opsForSet().add("seckill:" + productId, userId); } } ``` 3. 在应用启动类上添加RabbitMQ的配置: ```java @SpringBootApplication public class SeckillApplication { public static void main(String[] args) { SpringApplication.run(SeckillApplication.class, args); } @Bean public Queue seckillQueue() { return new Queue("seckill.queue"); } @Bean public DirectExchange seckillExchange() { return new DirectExchange("seckill-exchange"); } @Bean public Binding seckillBinding(Queue seckillQueue, DirectExchange seckillExchange) { return BindingBuilder.bind(seckillQueue).to(seckillExchange).with("seckill.queue"); } } ``` 以上示例代码演示了如何使用RabbitMQ和Redis实现秒杀功能。当秒杀请求到达控制器时,会判断用户是否已经参与过该秒杀活动以及商品库存是否充足,然后将秒杀请求发送到RabbitMQ中。消费者接收到秒杀请求后,再次判断用户是否已经参与过该秒杀活动以及商品库存是否充足,最后执行秒杀逻辑并将用户加入已秒杀集合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值