Spring Boot整合Redis笔记

亦凉 2024-05-03 14:35 116阅读 0赞

在这里插入图片描述

文章目录

  • 前言
  • Java 操作 Redis
    • Jedis 操作-测试
    • Jedis 实例-手机验证码
  • Redis与Spring Boot整合
    • 整合步骤
  • Redis 的事务操作
    • Redis的事务定义
    • Multi、Exec、discard 基本命令
    • 事务冲突的问题
      • 为什么要做成事务
      • 悲观锁
      • 乐观锁
        • WATCH key [key … ]
    • Redis事务三特性
    • Redis事务秒杀案例
      • 解决计数器和人员记录的事务操作
      • Redis事务—秒杀并发模拟
        • 连接超时问题
        • 超卖问题
        • 解决库存遗留问题
          • LUA脚本
          • LUA脚本在Redis中的优势

前言

?前言?
?博客:【无聊大侠hello word】?
✍有一点思考,有一点想法,有一点理性!✍
✍本文由在下【无聊大侠hello word】原创,首发于CSDN✍

Java 操作 Redis

Jedis 操作-测试

  1. 引入依赖


    redis.clients
    jedis
    3.8.0

    junit
    junit
    4.13.2
    compile

  1. 简单测试

    package com.jedis.example;

    import org.junit.Test;
    import redis.clients.jedis.Jedis;

    import java.util.List;
    import java.util.Set;

    public class JedisDemo1
    {

  1. public static void main(String[] args)
  2. {
  3. // 创建Jedis对象
  4. Jedis jedis = new Jedis("127.0.0.1", 6379);
  5. // 测试
  6. String value = jedis.ping();
  7. System.out.println(value);
  8. jedis.close();
  9. }
  10. /**
  11. * 描述:操作 key [String]
  12. */
  13. @Test
  14. public void Demo1()
  15. {
  16. // 创建Jedis对象
  17. Jedis jedis = new Jedis("127.0.0.1", 6379);
  18. // 添加
  19. jedis.set("name", "lucy");
  20. // 获取
  21. String name = jedis.get("name");
  22. System.out.println(name);
  23. // 设置多个key-value
  24. jedis.mset("k1", "v1", "k2", "v2");
  25. List<String> mget = jedis.mget("k1", "k2");
  26. System.out.println(mget);
  27. Set<String> keys = jedis.keys("*");
  28. for (String key : keys)
  29. {
  30. System.out.println(key);
  31. }
  32. jedis.close();
  33. }
  34. /**
  35. * 描述:操作 List [List]
  36. */
  37. @Test
  38. public void Demo2()
  39. {
  40. // 创建Jedis对象
  41. Jedis jedis = new Jedis("127.0.0.1", 6379);
  42. // 添加List数据
  43. jedis.lpush("key1", "lucy", "mary", "jack");
  44. List<String> list = jedis.lrange("key1", 0, -1);
  45. System.out.println(list);
  46. jedis.close();
  47. }
  48. /**
  49. * 描述:操作 set
  50. */
  51. @Test
  52. public void Demo3()
  53. {
  54. // 创建Jedis对象
  55. Jedis jedis = new Jedis("127.0.0.1", 6379);
  56. // 添加Set数据
  57. jedis.sadd("names", "lucy");
  58. jedis.sadd("names", "jack");
  59. Set<String> name = jedis.smembers("name");
  60. System.out.println(name);
  61. jedis.close();
  62. }
  63. /**
  64. * 描述:操作 Hash
  65. */
  66. @Test
  67. public void Demo4()
  68. {
  69. // 创建Jedis对象
  70. Jedis jedis = new Jedis("127.0.0.1", 6379);
  71. // 添加Hash数据
  72. jedis.hset("users", "age", "20");
  73. String value = jedis.hget("users", "age");
  74. System.out.println(value);
  75. jedis.close();
  76. }
  77. /**
  78. * 描述:操作 zset
  79. */
  80. @Test
  81. public void Demo5()
  82. {
  83. // 创建Jedis对象
  84. Jedis jedis = new Jedis("127.0.0.1", 6379);
  85. // 添加zset数据
  86. jedis.zadd("china", 100d, "shanghai");
  87. Set<String> value = jedis.zrange("china", 0, -1);
  88. System.out.println(value);
  89. jedis.close();
  90. }
  91. }

更多Redis数据类型命令操作


Jedis 实例-手机验证码

要求:

1、输入手机号,点击发送后随机生成6位数字码,2分钟有效

2、输入验证码,点击验证,返回成功或失败

3、每个手机号每天只能输入3次

只实现Java操作

在这里插入图片描述


分析:
在这里插入图片描述


代码实现:

  1. package com.jedis.example;
  2. import redis.clients.jedis.Jedis;
  3. import java.util.Random;
  4. /**
  5. * 描述:Jedis 实例-手机验证码
  6. *
  7. * @author 为一道彩虹
  8. */
  9. public class PhoneCode
  10. {
  11. /**
  12. * 描述:1.生成6位数字验证码
  13. */
  14. public static String getCode()
  15. {
  16. Random random = new Random();
  17. String code = "";
  18. for (int i = 0; i < 6; i++)
  19. {
  20. int rand = random.nextInt(10);
  21. code += rand;
  22. }
  23. return code;
  24. }
  25. /**
  26. * 描述:2.每个手机每天只能发送三次,验证码放到redis中,设置过期时间120秒
  27. */
  28. public static void verifyCode(String phone, String code)
  29. {
  30. // 连接redis
  31. Jedis jedis = new Jedis("127.0.0.1", 6379);
  32. // 拼接key [制定规则]
  33. // 手机号发送次数key
  34. String countKey = "VerifyCode" + phone + ":count";
  35. // 验证码key
  36. String codeKey = "VerifyCode" + phone + ":code";
  37. // 每个手机每天只能发送三次
  38. String count = jedis.get(countKey);
  39. if (count == null)
  40. {
  41. // 没有发送次数,第一次发送, 设置发送次数是1
  42. jedis.setex(countKey, 24*60*60, "1");
  43. }
  44. else if(Integer.parseInt(count) <= 2)
  45. {
  46. // 发送次数+1
  47. jedis.incr(countKey);
  48. }
  49. else if (Integer.parseInt(count) > 2)
  50. {
  51. // 发送三次,不能再发送
  52. System.out.println("今天发送次数已经超过三次");
  53. jedis.close();
  54. return;
  55. }
  56. // 发送验证码放到redis里面
  57. jedis.setex(codeKey, 120, code);
  58. jedis.close();
  59. }
  60. /**
  61. * 描述:3.验证码校验
  62. */
  63. public static void getRedisCode(String phone, String code)
  64. {
  65. // 连接redis
  66. Jedis jedis = new Jedis("127.0.0.1", 6379);
  67. // 验证码key
  68. String codeKey = "VerifyCode" + phone + ":code";
  69. // 从redis获取验证码
  70. String redisCode = jedis.get(codeKey);
  71. // 判断
  72. if (redisCode.equals(code))
  73. {
  74. System.out.println("成功");
  75. }
  76. else{
  77. System.out.println("失败");
  78. }
  79. jedis.close();
  80. }
  81. public static void main(String[] args)
  82. {
  83. // 生成6位数字验证码
  84. String code = getCode();
  85. // 模拟验证码发送
  86. verifyCode("1613075408", code);
  87. // 验证码校验
  88. // getRedisCode("1613075408", "466604");
  89. }
  90. }

在这里插入图片描述


Redis与Spring Boot整合

Spring Boot整合Redis非常简单,只需要按如下步骤整合即可

整合步骤

1、在pom.xml文件中引入redis相关依赖

  1. <!--redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!--spring2.X集成redis 所需common-pooL2-->
  7. <dependency>
  8. <groupId>org.apache.commons</groupId>
  9. <artifactId>commons-pool2</artifactId>
  10. <version>2.6.0</version>
  11. </dependency>

2、application.properties配置redis配置

  1. #Redis服务器地址
  2. spring.redis.host=127.0.0.1
  3. #Redis服务器连接端口
  4. spring.redis.port=6379
  5. #Redis数据库素引(默认为0)
  6. spring.redis.database=0
  7. #连接超时时间(毫秒)
  8. spring.redis.timeout=1800000
  9. #连接池最太连接数(使用负值表示没有限制)
  10. spring.redis.lettuce.pool.max-active=20
  11. #最大阻塞等待时间(负数表示没限制)
  12. spring.redis.lettuce.pool.max-wait=-1
  13. #连接池中的最大空闲连接
  14. spring.redis.lettuce.pool.max-idle=5
  15. #连接池中的最小空闲连接
  16. spring.redis.lettuce.pool.min-idle=0

3、添加redis配置类

  1. package com.redis.spring.boot.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.cache.CacheManager;
  6. import org.springframework.cache.annotation.CachingConfigurerSupport;
  7. import org.springframework.cache.annotation.EnableCaching;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  11. import org.springframework.data.redis.cache.RedisCacheManager;
  12. import org.springframework.data.redis.connection.RedisConnectionFactory;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  15. import org.springframework.data.redis.serializer.RedisSerializationContext;
  16. import org.springframework.data.redis.serializer.RedisSerializer;
  17. import org.springframework.data.redis.serializer.StringRedisSerializer;
  18. import java.time.Duration;
  19. /**
  20. * 描述:Redis配置类
  21. *
  22. * @author 为一道彩虹
  23. */
  24. @EnableCaching
  25. @Configuration
  26. public class RedisConfig extends CachingConfigurerSupport
  27. {
  28. @Bean
  29. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory)
  30. {
  31. RedisTemplate<String, Object> template = new RedisTemplate<>();
  32. RedisSerializer<String> redisSerializer = new StringRedisSerializer();
  33. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  34. ObjectMapper om = new ObjectMapper();
  35. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  36. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  37. jackson2JsonRedisSerializer.setObjectMapper(om);
  38. template.setConnectionFactory(factory);
  39. // key序列化方式
  40. template.setKeySerializer(redisSerializer);
  41. // value序列化
  42. template.setValueSerializer(jackson2JsonRedisSerializer);
  43. // value hashmap序列化
  44. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  45. return template;
  46. }
  47. @Bean
  48. public CacheManager cacheManager(RedisConnectionFactory factory)
  49. {
  50. RedisSerializer<String> redisSerializer = new StringRedisSerializer();
  51. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  52. // 解决查询缓存转换异常的问题
  53. ObjectMapper om = new ObjectMapper();
  54. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  55. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  56. jackson2JsonRedisSerializer.setObjectMapper(om);
  57. // 配置序列化(解决乱码的问题),过期时间600秒
  58. RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
  59. .entryTtl(Duration.ofSeconds(600))
  60. .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
  61. .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
  62. .disableCachingNullValues();
  63. RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
  64. .cacheDefaults(config)
  65. .build();
  66. return cacheManager;
  67. }
  68. }

4、测试一下

  1. package com.redis.spring.boot.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * 描述:RedisTestController
  9. *
  10. * @author 为一道彩虹
  11. */
  12. @RestController
  13. @RequestMapping("/redisTest")
  14. public class RedisTestController
  15. {
  16. @Autowired
  17. private RedisTemplate redisTemplate = null;
  18. @GetMapping
  19. public String testRedis()
  20. {
  21. // 设置值到Redis
  22. redisTemplate.opsForValue().set("name", "lucy");
  23. // 从Redis获取值
  24. Object name = redisTemplate.opsForValue().get("name");
  25. return name.toString();
  26. }
  27. }

在这里插入图片描述


Redis 的事务操作

Redis的事务定义

Redis 事务是一个单独的隔离操作:事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。

Redis 事务的主要作用就是串联多个命令防止别的命令插队。


Multi、Exec、discard 基本命令

从输入Multi 命令开始,输入的命令都会依次进入命令队列中,但不会执行,直到输入Exec后,Redis会将之前的命令队列中的命令依次执行。

组队的过程中可以通过discard来放弃组队。

在这里插入图片描述


案例:

在这里插入图片描述


组队成功,提交成功:

在这里插入图片描述


组队阶段报错,提交失败:

在这里插入图片描述
组队成功,提交有成功有失败情况


事务的错误处理

  1. 组队中某个命令出现了报告错误,执行时整个的所有队列都会被取消。

在这里插入图片描述


  1. 如果执行价段果个命令报出了错误,则只有报错的命令不会被执行,而其也的命令都会执行,不会回滚。

在这里插入图片描述


事务冲突的问题

为什么要做成事务

想想一个场景:有很多人有你的账户,同时去参加双十一抢购

例子:

  • 一个请求想给金额减8000
  • 一个请求想给金额减5000
  • 一 个请求想给金额减1000

在这里插入图片描述


悲观锁

在这里插入图片描述

  • 悲观锁( Pessimistic Lock),顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会 block 直到它拿到锁。传统的关系型数据库里边就用到了很多这种锁机制,比如行锁表锁等,读锁写锁等,都是在做操作之前先上

乐观锁

在这里插入图片描述

  • 乐观锁 (Optimistic Lock),顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号等机制。乐观锁适用于多读的应用类型,这样可以提高吞吐量。Redis就是利用这种check-and-set机制实现事务的

WATCH key [key … ]
  • 在执行 multi 之前,先执行 watch key1 [key2],可以监视一个(或多个)key,如果在事务执行之前这个(或这些)key被其他命令所改动,那么事务将被打断。

在这里插入图片描述


Redis事务三特性

单独的隔离操作:

  • 事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。

没有隔离级别的概念:

  • 队列中的命令没有提交之前都不会实际被执行,因为事务提交前任何指令都不会被实际执行

不保证原子性:

  • 事务中如果有一条命令执行失败,其后的命令仍然会被执行,没有回滚

Redis事务秒杀案例

项目代码:链接:https://pan.baidu.com/s/1TIizVmha8HeKtsO1T1Kstg
提取码:0915

解决计数器和人员记录的事务操作

在这里插入图片描述


秒杀过程类

  1. package com.redis.example;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. import redis.clients.jedis.Transaction;
  5. import java.io.IOException;
  6. import java.util.List;
  7. /**
  8. * 描述:秒杀过程类
  9. */
  10. public class SecKill_redis
  11. {
  12. public static void main(String[] args)
  13. {
  14. Jedis jedis = new Jedis("127.0.0.1", 6379);
  15. System.out.println(jedis.ping());
  16. // 添加测试秒杀数据
  17. jedis.set("sk:0101:qt", "10");
  18. jedis.close();
  19. }
  20. /**
  21. * 描述:秒杀过程
  22. */
  23. public static boolean doSecKill(String uid, String prodid) throws IOException
  24. {
  25. //1 uid和prodid非空判断
  26. if (uid == null || prodid == null)
  27. {
  28. return false;
  29. }
  30. //2 连接redis
  31. Jedis jedis = new Jedis("127.0.0.1",6379);
  32. //3 拼接key
  33. // 3.1 库存key
  34. String kcKey = "sk:" + prodid + ":qt";
  35. // 3.2 秒杀成功用户key
  36. String userKey = "sk:" + prodid + ":user";
  37. //4 获取库存,如果库存null,秒杀还没有开始
  38. String kc = jedis.get(kcKey);
  39. if (kc == null)
  40. {
  41. System.out.println("秒杀还没有开始,请等待");
  42. jedis.close();
  43. return false;
  44. }
  45. // 5 判断用户是否重复秒杀操作
  46. if (jedis.sismember(userKey, uid))
  47. {
  48. System.out.println("已经秒杀成功了,不能重复秒杀");
  49. jedis.close();
  50. return false;
  51. }
  52. //6 判断如果商品数量,库存数量小于1,秒杀结束
  53. if (Integer.parseInt(kc) <= 0)
  54. {
  55. System.out.println("秒杀已经结束了");
  56. jedis.close();
  57. return false;
  58. }
  59. //7 秒杀过程
  60. //7.1 库存-1
  61. jedis.decr(kcKey);
  62. //7.2 把秒杀成功用户添加清单里面
  63. jedis.sadd(userKey,uid);
  64. System.out.println("秒杀成功了..");
  65. jedis.close();
  66. return true;
  67. }
  68. }

简单页面 index.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>iPhone 13 Pro !!! 1元秒杀!!!
  11. </h1>
  12. <form id="msform" action="${pageContext.request.contextPath}/doseckill" enctype="application/x-www-form-urlencoded">
  13. <input type="hidden" id="prodid" name="prodid" value="0101">
  14. <input type="button" id="miaosha_btn" name="seckill_btn" value="秒杀点我"/>
  15. </form>
  16. </body>
  17. <script type="text/javascript" src="${pageContext.request.contextPath}/script/jquery/jquery-3.1.0.js"></script>
  18. <script type="text/javascript">
  19. $(function(){
  20. $("#miaosha_btn").click(function(){
  21. var url=$("#msform").attr("action");
  22. $.post(url,$("#msform").serialize(),function(data){
  23. if(data=="false"){
  24. alert("抢光了" );
  25. $("#miaosha_btn").attr("disabled",true);
  26. }
  27. } );
  28. })
  29. })
  30. </script>
  31. </html>

servlet:

  1. package com.redis.example;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.util.Random;
  8. /**
  9. * 描述:秒杀案例
  10. */
  11. public class SecKillServlet extends HttpServlet
  12. {
  13. private static final long serialVersionUID = 1L;
  14. public SecKillServlet()
  15. {
  16. super();
  17. }
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  19. {
  20. String userid = new Random().nextInt(50000) + "";
  21. String prodid = request.getParameter("prodid");
  22. boolean isSuccess=SecKill_redis.doSecKill(userid,prodid);
  23. response.getWriter().print(isSuccess);
  24. }
  25. }

测试结果:

在这里插入图片描述

在这里插入图片描述


Redis事务–秒杀并发模拟

使用ab工具能模拟多个http请求,并包括并发效果

  • 使用工具ab模拟测试
  • CentOS6 默认安装
  • CentOS7需要手动安装

1 .手动安装:yum install httpd-tools

在这里插入图片描述


2 .查询帮助手册:ab —help

在这里插入图片描述

1、-n:表示请求次数

2、-c:请求中并发数量

3、-p:提交参数格式【POST/PUT】

4、-T:参数类型


3 .通过ab测试:

  • 新建:vim postfile 模拟表单提交参数,以&符号结尾;存放当前目录
  • 内容:prodid=0101&

在这里插入图片描述

-T :类型
在这里插入图片描述

  • 访问路径:ab -n 1000 -c 100 -p ~/postfile -T application/x-www-form-urlencoded http://本机 IP:8080/Seckill/doseckill
  • 重置redis数据

在这里插入图片描述

  • 测试结果

运行:ab -n 1000 -c 100 -p ~/postfile -T application/x-www-form-urlencoded http://本机 IP:8080/Seckill/doseckill

在这里插入图片描述


线程安全问题:

1、观察控制台和Redis中的数据可以看到出来了线程安全问题,秒杀已结束,后续还可以执行秒杀操作。由于在多线程程序运行时,会发生多个线程同时访问同一个对象或资源的情况下,第一、第二、第三线程对该对象进行修改,这就会导致该对象最终结果的不统一,引发线程安全问题

在这里插入图片描述

在这里插入图片描述


2、连接超时问题

由于每次访问都需要连接Redis,如果有2000个请求,连接Redis等待时间过长,满足条件情况下会报连接超时问题

3、超卖问题

商品已卖完,后续可以买!

在这里插入图片描述

在这里插入图片描述


连接超时问题

连接超时,可以通过连接池解决,节省每次连接redis服务带来的消耗,把连接好的实例反复利用。

1、Jedis连接池类:

  1. package com.redis.example;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. import redis.clients.jedis.JedisPoolConfig;
  5. /**
  6. * Jedis连接池类
  7. */
  8. public class JedisPoolUtil
  9. {
  10. private static volatile JedisPool jedisPool = null;
  11. public static JedisPool getJedisPoolInstance()
  12. {
  13. if (null == jedisPool)
  14. {
  15. synchronized (JedisPoolUtil.class)
  16. {
  17. if (null == jedisPool)
  18. {
  19. JedisPoolConfig poolConfig = new JedisPoolConfig();
  20. poolConfig.setMaxTotal(200);
  21. poolConfig.setMaxIdle(32);
  22. poolConfig.setMaxWaitMillis(100 * 1000);
  23. poolConfig.setBlockWhenExhausted(true);
  24. poolConfig.setTestOnBorrow(true); // ping PONG
  25. jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 60000);
  26. }
  27. }
  28. }
  29. return jedisPool;
  30. }
  31. public static void release(JedisPool jedisPool, Jedis jedis)
  32. {
  33. if (null != jedis)
  34. {
  35. jedisPool.returnResource(jedis);
  36. }
  37. }
  38. }

2、链接池参数:

  • MaxTotal:控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;如果赋值为-1,则表示不限制;如果pool已经分配了MaxTotal个jedis实例,则此时pool的状态为exhausted
  • maxIdle:控制一个pool最多有多少个状态为idle(空闲)的jedis实例
  • MaxWaitMillis:表示当borrow一个jedis实例时,最大的等待毫秒数,如果超过等待时间,则直接抛JedisConnectionException
  • testOnBorrow:获得一个jedis实例的时候是否检查连接可用性(ping());如果为true,则得到的jedis实例均是可用的

    //2 连接redis
    //Jedis jedis = new Jedis(“127.0.0.1”,6379);

    //通过连接池得到jedis对象
    JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
    Jedis jedis = jedisPoolInstance.getResource();


超卖问题

利用乐观锁淘汰用户,解决超卖问题

在这里插入图片描述

1、监视库存

  1. //监视库存
  2. jedis.watch(kcKey);

2、使用事务

  1. //7 秒杀过程
  2. //使用事务
  3. Transaction multi = jedis.multi();

3、组队操作

  1. //组队操作
  2. multi.decr(kcKey);
  3. multi.sadd(userKey, uid);
  4. //执行
  5. List<Object> results = multi.exec();
  6. if (results == null || results.size() == 0)
  7. {
  8. System.out.println("秒杀失败了....");
  9. jedis.close();
  10. return false;
  11. }

秒杀过程类:

  1. package com.redis.example;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. import redis.clients.jedis.Transaction;
  5. import java.io.IOException;
  6. import java.util.List;
  7. /**
  8. * 描述:秒杀过程类
  9. */
  10. public class SecKill_redis
  11. {
  12. public static void main(String[] args)
  13. {
  14. Jedis jedis = new Jedis("127.0.0.1", 6379);
  15. System.out.println(jedis.ping());
  16. // 添加测试秒杀数据
  17. jedis.set("sk:0101:qt", "10");
  18. jedis.close();
  19. }
  20. /**
  21. * 描述:秒杀过程
  22. */
  23. public static boolean doSecKill(String uid, String prodid) throws IOException
  24. {
  25. //1 uid和prodid非空判断
  26. if (uid == null || prodid == null)
  27. {
  28. return false;
  29. }
  30. //2 连接redis
  31. //Jedis jedis = new Jedis("127.0.0.1",6379);
  32. //通过连接池得到jedis对象
  33. JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
  34. Jedis jedis = jedisPoolInstance.getResource();
  35. //3 拼接key
  36. // 3.1 库存key
  37. String kcKey = "sk:" + prodid + ":qt";
  38. // 3.2 秒杀成功用户key
  39. String userKey = "sk:" + prodid + ":user";
  40. //监视库存
  41. jedis.watch(kcKey);
  42. //4 获取库存,如果库存null,秒杀还没有开始
  43. String kc = jedis.get(kcKey);
  44. if (kc == null)
  45. {
  46. System.out.println("秒杀还没有开始,请等待");
  47. jedis.close();
  48. return false;
  49. }
  50. // 5 判断用户是否重复秒杀操作
  51. if (jedis.sismember(userKey, uid))
  52. {
  53. System.out.println("已经秒杀成功了,不能重复秒杀");
  54. jedis.close();
  55. return false;
  56. }
  57. //6 判断如果商品数量,库存数量小于1,秒杀结束
  58. if (Integer.parseInt(kc) <= 0)
  59. {
  60. System.out.println("秒杀已经结束了");
  61. jedis.close();
  62. return false;
  63. }
  64. //7 秒杀过程
  65. //使用事务
  66. Transaction multi = jedis.multi();
  67. //组队操作
  68. multi.decr(kcKey);
  69. multi.sadd(userKey, uid);
  70. //执行
  71. List<Object> results = multi.exec();
  72. if (results == null || results.size() == 0)
  73. {
  74. System.out.println("秒杀失败了....");
  75. jedis.close();
  76. return false;
  77. }
  78. //7.1 库存-1
  79. //jedis.decr(kcKey);
  80. //7.2 把秒杀成功用户添加清单里面
  81. //jedis.sadd(userKey,uid);
  82. System.out.println("秒杀成功了..");
  83. jedis.close();
  84. return true;
  85. }
  86. }

解决库存遗留问题

已经秒光,可是还有库存。原因,就是乐观锁导致很多请求都失败。先点的没秒到,后点的可能秒到了。

LUA脚本

在这里插入图片描述

  • Lua 是一个小巧的脚本语言,Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,Lua并没有提供强大的库,一个完整的Lua解释器不过200k,所以Lua不适合作为开发独立应用程序的语言,而是作为嵌入式脚本语言
  • 很多应用程序、游戏使用LUA作为自己的嵌入式脚本语言,以此来实现可配置性、可扩展性。
  • 这其中包括魔兽争霸地图、魔兽世界、博德之门、愤怒的小鸟等众多游戏插件或外挂。

网站:https://www.w3cschool.cn/lua/


LUA脚本在Redis中的优势
  • 将复杂的或者多步的redis操作,写为一个脚本,一次提交给redis执行,减少反复连接redis的次数。提升性能。
  • LUA脚本是类似redis事务,有一定的原子性,不会被其他命令插队,可以完成一些redis事务性的操作
  • 但是注意redis的lua脚本功能,只有在Redis 2.6以上的版本才可以使用。
  • 利用lua脚本淘汰用户,解决超卖问题。

redis 2.6版本以后,通过lua脚本解决争抢问题,实际上是redis 利用其单线程的特性,用任务队列的方式解决多任务并发问题

在这里插入图片描述


第一版:简单版

  • 点10次,正常秒杀
  • 我们一起点试一试,秒杀也是正常的。这是因为还达不到并发的效果
  • 使用工具ab模拟并发测试,会出现超卖情况。查看库存会出现负数

第二版:加事务-乐观锁(解决超卖),但出现遗留库存和连接超时

第三版:连接池解决超时问题

第四版:解决库存依赖问题,LUA脚本

LUA脚本类:

  1. package com.redis.example;
  2. import org.slf4j.LoggerFactory;
  3. import redis.clients.jedis.HostAndPort;
  4. import redis.clients.jedis.Jedis;
  5. import redis.clients.jedis.JedisPool;
  6. import java.io.IOException;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9. /**
  10. * 描述:LUA脚本类
  11. */
  12. public class SecKill_redisByScript
  13. {
  14. private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SecKill_redisByScript.class);
  15. public static void main(String[] args)
  16. {
  17. JedisPool jedispool = JedisPoolUtil.getJedisPoolInstance();
  18. Jedis jedis = jedispool.getResource();
  19. System.out.println(jedis.ping());
  20. Set<HostAndPort> set = new HashSet<HostAndPort>();
  21. // doSecKill("201","sk:0101");
  22. }
  23. /**
  24. * 描述:LUA脚本
  25. */
  26. static String secKillScript = "local userid=KEYS[1];\r\n" +
  27. "local prodid=KEYS[2];\r\n" +
  28. "local qtkey='sk:'..prodid..\":qt\";\r\n" +
  29. "local usersKey='sk:'..prodid..\":usr\";\r\n" +
  30. "local userExists=redis.call(\"sismember\",usersKey,userid);\r\n" +
  31. "if tonumber(userExists)==1 then \r\n" +
  32. " return 2;\r\n" +
  33. "end\r\n" +
  34. "local num= redis.call(\"get\" ,qtkey);\r\n" +
  35. "if tonumber(num)<=0 then \r\n" +
  36. " return 0;\r\n" +
  37. "else \r\n" +
  38. " redis.call(\"decr\",qtkey);\r\n" +
  39. " redis.call(\"sadd\",usersKey,userid);\r\n" +
  40. "end\r\n" +
  41. "return 1";
  42. static String secKillScript2 =
  43. "local userExists=redis.call(\"sismember\",\"{sk}:0101:usr\",userid);\r\n" +
  44. " return 1";
  45. public static boolean doSecKill(String uid, String prodid) throws IOException
  46. {
  47. JedisPool jedispool = JedisPoolUtil.getJedisPoolInstance();
  48. Jedis jedis = jedispool.getResource();
  49. //String sha1= .secKillScript;
  50. String sha1 = jedis.scriptLoad(secKillScript);
  51. Object result = jedis.evalsha(sha1, 2, uid, prodid);
  52. String reString = String.valueOf(result);
  53. if ("0".equals(reString)) {
  54. System.err.println("已抢空!!");
  55. } else if ("1".equals(reString)) {
  56. System.out.println("抢购成功!!!!");
  57. } else if ("2".equals(reString)) {
  58. System.err.println("该用户已抢过!!");
  59. } else {
  60. System.err.println("抢购异常!!");
  61. }
  62. jedis.close();
  63. return true;
  64. }
  65. }

servlet类:

  1. package com.redis.example;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.util.Random;
  8. /**
  9. * 描述:秒杀案例
  10. */
  11. public class SecKillServlet extends HttpServlet
  12. {
  13. private static final long serialVersionUID = 1L;
  14. public SecKillServlet()
  15. {
  16. super();
  17. }
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  19. {
  20. String userid = new Random().nextInt(50000) + "";
  21. String prodid = request.getParameter("prodid");
  22. // boolean isSuccess=SecKill_redis.doSecKill(userid,prodid);
  23. boolean isSuccess = SecKill_redisByScript.doSecKill(userid, prodid);
  24. response.getWriter().print(isSuccess);
  25. }
  26. }

测试结果:

在这里插入图片描述

先赞后看,养成习惯!!!^ _ ^ ❤️ ❤️ ❤️
码字不易,大家的支持就是我的坚持下去的动力。点赞后不要忘了关注我哦!

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot整合Redis笔记

    ?前言??博客:【无聊大侠hello word】?✍有一点思考,有一点想法,有一点理性!✍✍本文由在下【无聊大侠hello word】原创,首发于CSDN✍。