zoukankan      html  css  js  c++  java
  • java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购

    此教程不涉及整合spring整合redis,可另行查阅资料教程。

    代码:

    RedisLock

    package com.cashloan.analytics.utils;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisCallback;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RedisLock {
        private static Logger logger = LoggerFactory.getLogger(RedisLock.class);
        private static final int DEFAULT_ACQUIRY_RESOLUTION_MILLIS = 100;
        public static final String LOCK_PREFIX = "redis_lock_";
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        /**
         * 锁超时时间,防止线程在入锁以后,无限的执行等待
         */
        private int expireMsecs = 60 * 1000;
    
        /**
         * 锁等待时间,防止线程饥饿
         */
        private int timeoutMsecs = 10 * 1000;
    
    
        public String get(final String key) {
            Object obj = null;
            try {
                obj = redisTemplate.execute((RedisCallback<Object>) connection -> {
                    StringRedisSerializer serializer = new StringRedisSerializer();
                    byte[] data = connection.get(serializer.serialize(key));
                    connection.close();
                    if (data == null) {
                        return null;
                    }
                    return serializer.deserialize(data);
                });
            } catch (Exception e) {
                logger.error("get redis error, key : {}", key);
            }
            return obj != null ? obj.toString() : null;
        }
    
        public boolean setNX(final String key, final String value) {
            Object obj = null;
            try {
                obj = redisTemplate.execute((RedisCallback<Object>) connection -> {
                    StringRedisSerializer serializer = new StringRedisSerializer();
                    Boolean success = connection.setNX(serializer.serialize(key), serializer.serialize(value));
                    connection.close();
                    return success;
                });
            } catch (Exception e) {
                logger.error("setNX redis error, key : {}", key);
            }
            return obj != null ? (Boolean) obj : false;
        }
    
        private String getSet(final String key, final String value) {
            Object obj = null;
            try {
                obj = redisTemplate.execute((RedisCallback<Object>) connection -> {
                    StringRedisSerializer serializer = new StringRedisSerializer();
                    byte[] ret = connection.getSet(serializer.serialize(key), serializer.serialize(value));
                    connection.close();
                    return serializer.deserialize(ret);
                });
            } catch (Exception e) {
                logger.error("setNX redis error, key : {}", key);
            }
            return obj != null ? (String) obj : null;
        }
    
        /**
         * 获得 lock. 实现思路: 主要是使用了redis 的setnx命令,缓存了锁. reids缓存的key是锁的key,所有的共享,
         * value是锁的到期时间(注意:这里把过期时间放在value了,没有时间上设置其超时时间) 执行过程:
         * 1.通过setnx尝试设置某个key的值,成功(当前没有这个锁)则返回,成功获得锁
         * 2.锁已经存在则获取锁的到期时间,和当前时间比较,超时的话,则设置新的值
         *
         * @return true if lock is acquired, false acquire timeouted
         * @throws InterruptedException
         *             in case of thread interruption
         */
        public boolean lock(String lockKey) throws InterruptedException {
            lockKey = LOCK_PREFIX + lockKey;
            int timeout = timeoutMsecs;
            while (timeout >= 0) {
                long expires = System.currentTimeMillis() + expireMsecs + 1;
                String expiresStr = String.valueOf(expires); // 锁到期时间
                if (this.setNX(lockKey, expiresStr)) {
                    return true;
                }
    
                String currentValueStr = this.get(lockKey); // redis里的时间
                if (currentValueStr != null && Long.parseLong(currentValueStr) < System.currentTimeMillis()) {
                    // 判断是否为空,不为空的情况下,如果被其他线程设置了值,则第二个条件判断是过不去的
                    // lock is expired
    
                    String oldValueStr = this.getSet(lockKey, expiresStr);
                    // 获取上一个锁到期时间,并设置现在的锁到期时间,
                    // 只有一个线程才能获取上一个线上的设置时间,因为jedis.getSet是同步的
                    if (oldValueStr != null && oldValueStr.equals(currentValueStr)) {
                        // 防止误删(覆盖,因为key是相同的)了他人的锁——这里达不到效果,这里值会被覆盖,但是因为什么相差了很少的时间,所以可以接受
    
                        // [分布式的情况下]:如过这个时候,多个线程恰好都到了这里,但是只有一个线程的设置值和当前值相同,他才有权利获取锁
                        return true;
                    }
                }
                timeout -= DEFAULT_ACQUIRY_RESOLUTION_MILLIS;
    
                /*
                 * 延迟100 毫秒, 这里使用随机时间可能会好一点,可以防止饥饿进程的出现,即,当同时到达多个进程,
                 * 只会有一个进程获得锁,其他的都用同样的频率进行尝试,后面有来了一些进行,也以同样的频率申请锁,这将可能导致前面来的锁得不到满足.
                 * 使用随机的等待时间可以一定程度上保证公平性
                 */
                Thread.sleep(DEFAULT_ACQUIRY_RESOLUTION_MILLIS);
    
            }
            return false;
        }
    
        /**
         * Acqurired lock release.
         */
        public void unlock(String lockKey) {
            lockKey = LOCK_PREFIX + lockKey;
            redisTemplate.delete(lockKey);
        }
    
    }

    redis消息队列:RedisQueue

    package com.cashloan.analytics.utils;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    /**
     * redis消息队列
     */
    @Component
    public class RedisQueue {
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
    
        /** ---------------------------------- redis消息队列 ---------------------------------- */
        /**
         * 存值
         * @param key 键
         * @param value 值
         * @return
         */
        public boolean lpush(String key, Object value) {
            try {
                redisTemplate.opsForList().leftPush(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 取值 - <rpop:非阻塞式>
         * @param key 键
         * @return
         */
        public Object rpop(String key) {
            try {
                return redisTemplate.opsForList().rightPop(key);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 取值 - <brpop:阻塞式> - 推荐使用
         * @param key 键
         * @param timeout 超时时间
         * @param timeUnit 给定单元粒度的时间段
         *                 TimeUnit.DAYS          //天
         *                 TimeUnit.HOURS         //小时
         *                 TimeUnit.MINUTES       //分钟
         *                 TimeUnit.SECONDS       //秒
         *                 TimeUnit.MILLISECONDS  //毫秒
         * @return
         */
        public Object brpop(String key, long timeout, TimeUnit timeUnit) {
            try {
                return redisTemplate.opsForList().rightPop(key, timeout, timeUnit);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 查看值
         * @param key 键
         * @param start 开始
         * @param end 结束 0 到 -1代表所有值
         * @return
         */
        public List<Object> lrange(String key, long start, long end) {
            try {
                return redisTemplate.opsForList().range(key, start, end);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    }

    测试类controller:Test

    package com.cashloan.analytics.controller;
    
    import com.cashloan.analytics.utils.RedisLock;
    import com.cashloan.analytics.utils.RedisQueue;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.*;
    
    @RestController
    @RequestMapping("/test")
    public class Test {
        private final static String MESSAGE = "testmq";
        @Autowired
        private RedisQueue redisQueue;
        @Autowired
        private RedisLock redisLock;
    
        @GetMapping("/add")
        public String add() {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            Map map = new HashMap();
            map.put("id", uuid);
            // 加入redis消息队列
            redisQueue.lpush(MESSAGE, map);
            addBatch();
            return "success";
        }
    
        public void addBatch() {
            try {
                if (redisLock.lock(MESSAGE)) {
                    List<Object> lrange = redisQueue.lrange(MESSAGE, 0, -1);
                    int size = lrange.size();
                    if (size >= 10) {
                        List<Map> maps = new ArrayList<>();
                        for (int i = 0; i < size; i++) {
                            Object brpop = redisQueue.rpop(MESSAGE);
                            if (brpop != null) {
                                maps.add((Map) brpop);
                            }
                        }
                        // 记录数据
                        if (!maps.isEmpty()) {
                            for (int i = 0; i < maps.size(); i++) {
                                System.out.println(maps.get(i).get("id"));
                                Thread.sleep(100);
                            }
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                redisLock.unlock(MESSAGE);
            }
        }
    
    }

    另有一份模拟高并发多线程请求的工具(python3):

    # -*- coding: utf-8 -*-
    import requests
    import threading
    
    class postrequests():
        def __init__(self):
            self.url = 'http://localhost:9090/test/add'
        def post(self):
            try:
                r = requests.get(self.url)
                print(r.text)
            except Exception as e:
                print(e)
    
    def test():
        test = postrequests()
        return test.post()
    try:
        i = 0
        # 开启线程数目
        tasks_number = 105
        print('测试启动')
        while i < tasks_number:
            t = threading.Thread(target=test)
            t.start()
            i += 1
    except Exception as e:
        print(e)
  • 相关阅读:
    Taxes
    Tennis Championship
    Urbanization
    字符串的匹配
    Alyona and a tree
    Alyona and mex
    Alyona and flowers
    Alyona and copybooks
    Subordinates
    线程的暂停、恢复和终止
  • 原文地址:https://www.cnblogs.com/007sx/p/11268469.html
Copyright © 2011-2022 走看看