redis队列实现高并发怎么配置?java整合redis

TheDisguiser 2020-07-29 11:01:03 java常见问答 11498

高并发是如淘宝等一些大型项目不可避免的实现功能,否则数据会冲垮一切,那么在redis中如何实现高并发呢?这次就来看看吧。

实例

新建一个项目,首先引入JAVA中redis操作库:Jedis,这里使用的是jedis-2.9.0.jar

创建一个类MyRedistest.class做线程操作

package com.myredistest;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import redis.clients.jedis.Jedis;
/**
 * redis
 *
 * @author 10255_000
 *
 */
public class MyRedistest
{
    public static void main(String[] args)
    {
        final String watchkeys = "watchkeys";
        ExecutorService executor = Executors.newFixedThreadPool(20); //20个线程池并发数
        final Jedis jedis = new Jedis("192.168.56.101", 6379);
        jedis.set(watchkeys, "100"); //设置起始的抢购数
        // jedis.del("setsucc", "setfail");
        jedis.close();
        for (int i = 0; i < 1000; i++)
        { //设置1000个人来发起抢购
            executor.execute(new MyRunnable("user" + getRandomString(6)));
        }
        executor.shutdown();
    }
    public static String getRandomString(int length)
    { //length是随机字符串长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++)
        {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}

再建一个类MyRunnable.class 实现Runnable做线程操作

package com.myredistest;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class MyRunnable implements Runnable
{
    String watchkeys = "watchkeys"; // 监视keys
    Jedis jedis = new Jedis("192.168.56.101", 6379);
    String userinfo;
    public MyRunnable()
    {}
    public MyRunnable(String uinfo)
    {
        this.userinfo = uinfo;
    }
    @Override
    public void run()
    {
        try
        {
            jedis.watch(watchkeys); // watchkeys
            String val = jedis.get(watchkeys);
            int valint = Integer.valueOf(val);
            if (valint <= 100 && valint >= 1)
            {
                Transaction tx = jedis.multi(); // 开启事务
                // tx.incr("watchkeys");
                tx.incrBy("watchkeys", -1);
                List < Object > list = tx.exec(); // 提交事务,如果此时watchkeys被改动了,则返回null
                if (list == null || list.size() == 0)
                {
                    String failuserifo = "fail" + userinfo;
                    String failinfo = "用户:" + failuserifo + "商品争抢失败,抢购失败";
                    System.out.println(failinfo);
                    /* 抢购失败业务逻辑 */
                    jedis.setnx(failuserifo, failinfo);
                }
                else
                {
                    for (Object succ: list)
                    {
                        String succuserifo = "succ" + succ.toString() + userinfo;
                        String succinfo = "用户:" + succuserifo + "抢购成功,当前抢购成功人数:" +
                            (1 - (valint - 100));
                        System.out.println(succinfo);
                        /* 抢购成功业务逻辑 */
                        jedis.setnx(succuserifo, succinfo);
                    }
                }
            }
            else
            {
                String failuserifo = "kcfail" + userinfo;
                String failinfo1 = "用户:" + failuserifo + "商品被抢购完毕,抢购失败";
                System.out.println(failinfo1);
                jedis.setnx(failuserifo, failinfo1);
                // Thread.sleep(500);
                return;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            jedis.close();
        }
    }
}

最后启动MyRedistest,基于redis队列实现高并发就完成了。

以上就是今天的全部内容,掌握高并发是一名程序员进阶的必然功能,如果还有想了解的java常见问题及解决方法,欢迎来奇Q工具网了解详情。

推荐阅读:

redis数据结构有哪些?redis数据结构使用方式

redis配置方式如何实现?有哪些常见参数?

Redis的使用场景有哪些?Redis的数据类型有哪些?