基于redis的高并发秒杀的JAVA-DEMO实现!

在Redis的事务中,WATCH命令可用于提供CAS(check-and-set)功能。假设我们通过WATCH命令在事务执行之前监控了多个Keys,倘若在WATCH之后有任何Key的值发生了变化,EXEC命令执行的事务都将被放弃,同时返回Null multi-bulk应答以通知调用者事务执行失败。例如,我们再次假设Redis中并未提供incr命令来完成键值的原子性递增,如果要实现该功能,我们只能自行编写相应的代码。其伪码如下:
      val = GET mykey
      val = val + 1
      SET mykey $val
      以上代码只有在单连接的情况下才可以保证执行结果是正确的,因为如果在同一时刻有多个客户端在同时执行该段代码,那么就会出现多线程程序中经常出现的一种错误场景--竞态争用(race condition)。比如,客户端A和B都在同一时刻读取了mykey的原有值,假设该值为10,此后两个客户端又均将该值加一后set回Redis服务器,这样就会导致mykey的结果为11,而不是我们认为的12。为了解决类似的问题,我们需要借助WATCH命令的帮助,见如下代码:
      WATCH mykey
      val = GET mykey
      val = val + 1
      MULTI
      SET mykey $val
      EXEC
      和此前代码不同的是,新代码在获取mykey的值之前先通过WATCH命令监控了该键,此后又将set命令包围在事务中,这样就可以有效的保证每个连接在执行EXEC之前,如果当前连接获取的mykey的值被其它连接的客户端修改,那么当前连接的EXEC命令将执行失败。这样调用者在判断返回值后就可以获悉val是否被重新设置成功。

      根据这样的思路,我们在JAVA下进行实现:

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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做线程操作:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

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 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中插入的key值

你可能感兴趣的:(基于redis的高并发秒杀的JAVA-DEMO实现!)