一.创建一个简单的maven项目引入Redisson

<dependencies>
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <version>2.7.0</version>
    </dependency>
</dependencies>

二.配置Redisson并实例化

 1 public class RedissonManager {
 2
 3     private static Redisson redisson;
 4
 5     static {
 6         Config c = new Config();
 7         c.useSingleServer().setAddress("127.0.0.1:6379");
 8         redisson = (Redisson) Redisson.create(c);
 9     }
10
11     public static Redisson getRedission() {
12
13         if (null == redisson)
14             throw new RuntimeException("无法获取redisson");
15
16         return redisson;
17     }
18
19     // 初始化10张票,在测试之前先执行此main方法初始化10张票
20     public static void main(String[] args) {
21         RMap<String, Integer> rm = redisson.getMap("ticket_num_1");
22         rm.put("tn", 10);
23         System.out.println(rm.get("tn"));
24     }
25
26 }

三.创建Redisson分布式锁

 1 public class RedissonLock {
 2
 3     public static final String LOCK_ = "REDISSON_LOCK_";
 4
 5     public static void lock(String lockKey) {
 6         RLock lock = RedissonManager.getRedission().getLock(LOCK_ + lockKey);
 7         lock.lock(2, TimeUnit.SECONDS);
 8     }
 9
10     public static void unLock(String lockKey) {
11         RedissonManager.getRedission().getLock(LOCK_ + lockKey).unlock();
12     }
13
14 }

四.创建Ticket类模拟买票业务

 1 public class Ticket {
 2
 3     private Redisson redisson = RedissonManager.getRedission();
 4
 5     public int buy() {
 6
 7         Thread t = Thread.currentThread();
 8         String tName = t.getName();
 9
10         RedissonLock.lock("TICKET");
11         System.out.println(tName + "成功获取到锁");
12
13         RMap<String, Integer> rm = redisson.getMap("ticket_num_1");
14         Integer ticketNum = rm.get("tn");
15         if (ticketNum <= 0) {
16             System.out.println("购票失败,余票:" + ticketNum);
17             RedissonLock.unLock("TICKET"); // 购票失败也要释放锁
18             return -1;
19         }
20
21         // 模拟买票业务操作时间
22         try {
23             Thread.sleep(200);
24         } catch (InterruptedException e) {
25             System.out.println("sleep exception");
26         }
27         ticketNum = ticketNum - 1;
28         rm.put("tn", ticketNum);
29         RedissonLock.unLock("TICKET"); // 购票成功释放锁
30         System.out.println("购票成功,余票:" + ticketNum);
31         return ticketNum;
32     }
33
34 }

五.创建两个购票程序模拟分布式环境

 1 public class App1 {
 2
 3     public static void main(String[] args) throws InterruptedException {
 4
 5         final Ticket t = new Ticket();
 6
 7         for(int i = 0; i < 10; i++) {
 8             Thread thread = new Thread(new Runnable() {
 9                 public void run() {
10                     t.buy();
11                 }
12             }, "APP1_Thread" + i);
13             thread.start();
14
15             // 这里的2000是为了让App2启动的时候还有票可买,模拟分布式场景
16             Thread.sleep(2000);
17         }
18
19     }
20
21 }
 1 public class App2 {
 2
 3     public static void main(String[] args) throws InterruptedException {
 4
 5         final Ticket t = new Ticket();
 6
 7         for(int i = 0; i < 10; i++) {
 8             Thread thread = new Thread(new Runnable() {
 9                 public void run() {
10                     t.buy();
11                 }
12             }, "APP2_Thread" + i);
13             thread.start();
14
15             // 这里的2000是为了让App1启动的时候还有票可买,模拟分布式场景
16             Thread.sleep(2000);
17         }
18
19     }
20
21 }

六.运行RedissonManager.main初始化10张票

七.先启动App1.main后立马启动App2.main观察结果

02-13 07:54