美文网首页
学习redis布隆过滤器实现秒杀案例

学习redis布隆过滤器实现秒杀案例

作者: 家hao | 来源:发表于2020-09-14 15:40 被阅读0次

    PS:在网上看到的案例进行学习

    1、分析秒杀业务流程

    image.png image.png

    2、利用 Redis 缓存 INCR拦截流量

    • 首先通过数据控制模块,提前将秒杀商品缓存到读写分离 Redis,并设置秒杀开始标记如下:

    skuId_start: 0    开始标记,0表示秒杀还没开始
    skuId_count: 10000   表示总数
    skuId_access: 12000  表示接受抢购数
    秒杀开始前,服务集群读取 skuId_start 为 0,直接返回未开始。之所以设置这个值而不是根据时间判断是否开始,是因为服务时间可能不一致(相差几百毫秒)这样可能导致流量倾斜(其他服务没开始,会将大量的流量堆积到开始的服务上)

    • 数据控制模块将 skuId_start 改为1,标志秒杀开始。

    • 当接受下单数达到 skuId_count*1.2 后,继续拦截所有请求。

    3、利用 Redis 缓存加速库存扣量

    • skuId_booked: 0 表示没有抢购

    4、 将用户订单数据写入mq

    5、监听mq入库

    6、代码实现

    • SeckillService.java
    @Service
    public class SeckillService {
    
        private static final String secStartPrefix = "skuId_start_";   //一开始要在redis初始化0_1554045087    
        private static final String secAccess = "skuId_access_";
        private static final String secCount = "skuId_count_"; //一开始要在redis初始化数量“0”
        private static final String filterName = "skuId_bloomfilter_";
        private static final String bookedName = "skuId_booked_"; //一开始要在redis初始化数量“0”
    
    
        @Resource
        private RedisService redisService;
    
        public String seckill(int uid, int skuId) {
            //流量拦截层
            //1、判断秒杀是否开始   0_1554045087    开始标识_开始时间
            String isStart = (String) redisService.get(secStartPrefix + skuId);
            if (StringUtils.isBlank(isStart)) {
                return "还未开始";
            }
            if (isStart.contains("_")) {
                Integer isStartInt = Integer.parseInt(isStart.split("_")[0]);
                Integer startTime = Integer.parseInt(isStart.split("_")[1]);
                if (isStartInt == 0) {
                    if (startTime > getNow()) {
                        return "还未开始";
                    } else {
                        //代表秒杀已经开始
                        redisService.set(secStartPrefix + skuId, 1 + "");
                    }
                } else {
                    return "系统异常";
                }
            } else {
                if (Integer.parseInt(isStart) != 1) {
                    return "系统异常";
                }
            }
            //2、流量拦截
            String skuIdAccessName = secAccess + skuId;
            Integer accessNumInt = 0;
            String accessNum = (String) redisService.get(skuIdAccessName);
            if (StringUtils.isNotBlank(accessNum)) {
                accessNumInt = Integer.parseInt(accessNum);
            }
            String skuIdCountName = secCount + skuId;
            Integer countNumInt = Integer.parseInt((String) redisService.get(skuIdCountName));
            if (countNumInt * 1.2 < accessNumInt) {
                return "抢购已经完成,欢迎下次参与";
            } else {
                redisService.incr(skuIdAccessName);
            }
            //信息校验层
            if (redisService.bloomFilterExists(filterName, uid)) {
                return "您已经抢购过该商品,请勿重复下发!";
            } else {
                redisService.bloomFilterAdd(filterName, uid);
            }
            Boolean isSuccess = redisService.getAndIncrLua(bookedName + skuId);
            if (isSuccess) {
                return "恭喜您抢购成功!!!";
            } else {
                return "抢购结束,欢迎下次参与";
            }
        }
    
        private long getNow() {
            return System.currentTimeMillis() / 1000;
        }
    }
    
    • 工具类 RedisService.java
    @Service
    public class RedisService {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        private static double size = Math.pow(2, 32);
    
    
        /**
         * 写入缓存
         *
         * @param key
         * @param offset 位 8Bit=1Byte
         * @return
         */
        public boolean setBit(String key, long offset, boolean isShow) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.setBit(key, offset, isShow);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 写入缓存
         *
         * @param key
         * @param offset
         * @return
         */
        public boolean getBit(String key, long offset) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                result = operations.getBit(key, offset);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    
        /**
         * 写入缓存
         *
         * @param key
         * @param value
         * @return
         */
        public boolean set(final String key, Object value) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                redisTemplate.opsForList();
                operations.set(key, value);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    
        /**
         * 写入缓存
         *
         * @param key
         * @return
         */
        public Object get(final String key) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                return operations.get(key);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
        /**
         * 写入缓存
         *
         * @param key
         * @param value
         * @return
         */
        public boolean decr(final String key, int value) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.increment(key, -value);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    
        /**
         * 写入缓存
         *
         * @param key
         * @return
         */
        public boolean incr(final String key) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.increment(key, 1);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 写入缓存设置时效时间
         *
         * @param key
         * @param value
         * @return
         */
        public boolean set(final String key, Object value, Long expireTime) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 批量删除对应的value
         *
         * @param keys
         */
        public void remove(final String... keys) {
            for (String key : keys) {
                remove(key);
            }
        }
    
    
        /**
         * 删除对应的value
         *
         * @param key
         */
        public void remove(final String key) {
            if (exists(key)) {
                redisTemplate.delete(key);
            }
        }
    
        /**
         * 判断缓存中是否有对应的value
         *
         * @param key
         * @return
         */
        public boolean exists(final String key) {
            return redisTemplate.hasKey(key);
        }
    
        /**
         * 读取缓存
         *
         * @param key
         * @return
         */
        public Object genValue(final String key) {
            Object result = null;
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            result = operations.get(key);
            return result;
        }
    
        /**
         * 哈希 添加
         *
         * @param key
         * @param hashKey
         * @param value
         */
        public void hmSet(String key, Object hashKey, Object value) {
            HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
            hash.put(key, hashKey, value);
        }
    
        /**
         * 哈希获取数据
         *
         * @param key
         * @param hashKey
         * @return
         */
        public Object hmGet(String key, Object hashKey) {
            HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
            return hash.get(key, hashKey);
        }
    
        /**
         * 列表添加
         *
         * @param k
         * @param v
         */
        public void lPush(String k, Object v) {
            ListOperations<String, Object> list = redisTemplate.opsForList();
            list.rightPush(k, v);
        }
    
        /**
         * 列表获取
         *
         * @param k
         * @param l
         * @param l1
         * @return
         */
        public List<Object> lRange(String k, long l, long l1) {
            ListOperations<String, Object> list = redisTemplate.opsForList();
            return list.range(k, l, l1);
        }
    
        /**
         * 集合添加
         *
         * @param key
         * @param value
         */
        public void add(String key, Object value) {
            SetOperations<String, Object> set = redisTemplate.opsForSet();
            set.add(key, value);
        }
    
        /**
         * 集合获取
         *
         * @param key
         * @return
         */
        public Set<Object> setMembers(String key) {
            SetOperations<String, Object> set = redisTemplate.opsForSet();
            return set.members(key);
        }
    
        /**
         * 有序集合添加
         *
         * @param key
         * @param value
         * @param scoure
         */
        public void zAdd(String key, Object value, double scoure) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            zset.add(key, value, scoure);
        }
    
        /**
         * 有序集合获取
         *
         * @param key
         * @param scoure
         * @param scoure1
         * @return
         */
        public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            redisTemplate.opsForValue();
            return zset.rangeByScore(key, scoure, scoure1);
        }
    
    
        //第一次加载的时候将数据加载到redis中
        public void saveDataToRedis(String name) {
            double index = Math.abs(name.hashCode() % size);
            long indexLong = new Double(index).longValue();
            boolean availableUsers = setBit("availableUsers", indexLong, true);
        }
    
        //第一次加载的时候将数据加载到redis中
        public boolean getDataToRedis(String name) {
    
            double index = Math.abs(name.hashCode() % size);
            long indexLong = new Double(index).longValue();
            return getBit("availableUsers", indexLong);
        }
    
        /**
         * 有序集合获取排名
         *
         * @param key   集合名称
         * @param value 值
         */
        public Long zRank(String key, Object value) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            return zset.rank(key, value);
        }
    
    
        /**
         * 有序集合获取排名
         *
         * @param key
         */
        public Set<ZSetOperations.TypedTuple<Object>> zRankWithScore(String key, long start, long end) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            Set<ZSetOperations.TypedTuple<Object>> ret = zset.rangeWithScores(key, start, end);
            return ret;
        }
    
        /**
         * 有序集合添加
         *
         * @param key
         * @param value
         */
        public Double zSetScore(String key, Object value) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            return zset.score(key, value);
        }
    
    
        /**
         * 有序集合添加分数
         *
         * @param key
         * @param value
         * @param scoure
         */
        public void incrementScore(String key, Object value, double scoure) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            zset.incrementScore(key, value, scoure);
        }
    
    
        /**
         * 有序集合获取排名
         *
         * @param key
         */
        public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithScore(String key, long start, long end) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeByScoreWithScores(key, start, end);
            return ret;
        }
    
        /**
         * 有序集合获取排名
         *
         * @param key
         */
        public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithRank(String key, long start, long end) {
            ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
            Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeWithScores(key, start, end);
            return ret;
        }
    
    
        public Boolean bloomFilterAdd(String filterName, int value) {
            DefaultRedisScript<Boolean> bloomAdd = new DefaultRedisScript<>();
            bloomAdd.setScriptSource(new ResourceScriptSource(new ClassPathResource("bloomFilterAdd.lua")));
            bloomAdd.setResultType(Boolean.class);
            List<Object> keyList = new ArrayList<>();
            keyList.add(filterName);
            keyList.add(value + "");
            Boolean result = (Boolean) redisTemplate.execute(bloomAdd, keyList);
            return result;
        }
    
    
        public Boolean bloomFilterExists(String filterName, int value) {
            DefaultRedisScript<Boolean> bloomExists = new DefaultRedisScript<>();
            bloomExists.setScriptSource(new ResourceScriptSource(new ClassPathResource("bloomFilterExist.lua")));
            bloomExists.setResultType(Boolean.class);
            List<Object> keyList = new ArrayList<>();
            keyList.add(filterName);
            keyList.add(value + "");
            Boolean result = (Boolean) redisTemplate.execute(bloomExists, keyList);
            return result;
        }
    
        public Boolean getAndIncrLua(String key) {
            DefaultRedisScript<Boolean> bloomExists = new DefaultRedisScript<>();
            bloomExists.setScriptSource(new ResourceScriptSource(new ClassPathResource("secKillIncr.lua")));
            bloomExists.setResultType(Boolean.class);
            List<Object> keyList = new ArrayList<>();
            keyList.add(key);
            Boolean result = (Boolean) redisTemplate.execute(bloomExists, keyList);
            return result;
        }
    }
    
    • 基于 Lua 脚本实现 Spring Boot 和布隆过滤器的整合
      (1)bloomFilterAdd.lua
    local bloomName = KEYS[1]
    local value = KEYS[2]
    
    -- bloomFilter
    local result_1 = redis.call('BF.ADD', bloomName, value)
    return result_1
    

    (2)bloomFilterExist.lua

    local bloomName = KEYS[1]
    local value = KEYS[2]
    
    -- bloomFilter
    local result_1 = redis.call('BF.EXISTS', bloomName, value)
    return result_1
    

    (3)secKillIncr.lua

    local lockKey = KEYS[1]
    
    -- get info
    local result_1 = redis.call('GET', lockKey)
    if tonumber(result_1) <10000
    then
    local result_2= redis.call('INCR', lockKey)
    return result_1
    else
    return result_1
    end
    
    • 测试Controller SeckillController.java
    @RestController
    public class SeckillController {
    
        @Resource
        private SeckillService seckillService;
    
        @RequestMapping("/redis/seckill")
        public String secKill(int uid,int skuId){
             return seckillService.seckill(uid,skuId);
        }
    }
    

    相关文章

      网友评论

          本文标题:学习redis布隆过滤器实现秒杀案例

          本文链接:https://www.haomeiwen.com/subject/uuezektx.html