1. 程式人生 > 實用技巧 >通過Redisson快速實現分散式鎖

通過Redisson快速實現分散式鎖

1,專案的pom檔案

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dream21th.redisson</groupId>
    <artifactId>dream21th-redisson</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dream21th-redisson</name>
    <description>redisson分散式鎖學習</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.13.5</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.13</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2,專案的yml檔案

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: root
      initial-size: 10
      max-active: 100
      min-idle: 10
      max-wait: 60000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      #Oracle需要開啟註釋
      #validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        #login-username: admin
        #login-password: admin
      filter:
        stat:
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: false
        wall:
          config:
            multi-statement-allow: true
#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  #實體掃描,多個package用逗號或者分號分隔
  typeAliasesPackage: com.dream21th.redisson.entity

3,建立表和初始化資料

CREATE TABLE `order_info` (

`id` int(11) NOT NULL COMMENT '主鍵ID',

`num` int(11) NOT NULL COMMENT '庫存',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO order_info VALUES(1,100)

4,編寫程式碼

4.1 配置redis連線

package com.dream21th.redisson.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
 * @Author dream21th
 **/
@Configuration
public class RedissionConf {

    @Bean
    public RedissonClient redission(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }

}

4.2 配置mybatis

package com.dream21th.redisson.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/*
 * @Author dream21th
 **/
@Configuration
@MapperScan("com.dream21th.redisson.mapper*")
public class MyBatisConfig {
}

4.3編寫實體類

package com.dream21th.redisson.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/*
 * @Author dream21th
 **/
@Data
@TableName("order_info")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order extends Model<Order> {

    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;

    @TableField(value = "num")
    private Integer num;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }
}

4.4編寫mapper類

package com.dream21th.redisson.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dream21th.redisson.entity.Order;

/*
 * @Author dream21th
 **/
public interface OrderMapper extends BaseMapper<Order> {
}

4.5編寫controller

package com.dream21th.redisson.controller;

import com.dream21th.redisson.entity.Order;
import com.dream21th.redisson.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author dream21th
 **/
@Slf4j
@RestController
public class TestController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedissonClient redisson;

    @GetMapping("testKill")
    public void testKill(){
        RLock lock = redisson.getLock("KILL-LOCK");
        lock.lock();
        try{
            Order order = orderMapper.selectById(1);
            if(order.getNum()==0){
                log.info("------------------->>>{}沒有庫存了<<<------------------",Thread.currentThread().getId());
            }else {
                log.error("搶到庫存還是剩:{}",order.getNum());
                order.setNum(order.getNum()-1);
                orderMapper.updateById(order);
            }
        }finally {
            lock.unlock();
        }
    }
}

5,測試準備

5.1開啟jmter(準備兩個)

壓力測試8081埠(10個執行緒執行10次)

壓力測試8081埠(10個執行緒執行10次)

5.2啟動應用分別(埠:8080,8081)

6,開始測試

6.1分別啟動兩個jmter

7,測試結果

8080埠執行結果

2020-10-08 15:44:47.561 ERROR 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:70

2020-10-08 15:44:47.593 ERROR 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:69

2020-10-08 15:44:47.619 ERROR 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:68

2020-10-08 15:44:47.643 ERROR 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:67

2020-10-08 15:44:47.701 ERROR 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:65

2020-10-08 15:44:47.751 ERROR 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:63

2020-10-08 15:44:47.793 ERROR 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:61

2020-10-08 15:44:47.835 ERROR 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:59

2020-10-08 15:44:47.885 ERROR 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:57

2020-10-08 15:44:47.934 ERROR 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:55

2020-10-08 15:44:48.059 ERROR 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:50

2020-10-08 15:44:48.086 ERROR 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:49

2020-10-08 15:44:48.109 ERROR 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:48

2020-10-08 15:44:48.137 ERROR 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:47

2020-10-08 15:44:48.176 ERROR 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:45

2020-10-08 15:44:48.226 ERROR 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:43

2020-10-08 15:44:48.275 ERROR 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:41

2020-10-08 15:44:48.318 ERROR 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:39

2020-10-08 15:44:48.376 ERROR 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:37

2020-10-08 15:44:48.493 ERROR 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:35

2020-10-08 15:44:48.568 ERROR 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:32

2020-10-08 15:44:48.618 ERROR 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:30

2020-10-08 15:44:48.682 ERROR 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:28

2020-10-08 15:44:48.814 ERROR 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:26

2020-10-08 15:44:48.868 ERROR 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:24

2020-10-08 15:44:48.917 ERROR 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:22

2020-10-08 15:44:48.959 ERROR 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:20

2020-10-08 15:44:49.011 ERROR 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:18

2020-10-08 15:44:49.065 ERROR 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:16

2020-10-08 15:44:49.088 ERROR 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:15

2020-10-08 15:44:49.109 ERROR 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:14

2020-10-08 15:44:49.135 ERROR 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:13

2020-10-08 15:44:49.159 ERROR 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:12

2020-10-08 15:44:49.301 ERROR 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:6

2020-10-08 15:44:49.405 ERROR 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:2

2020-10-08 15:44:49.451 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:49.459 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:49.464 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:49.470 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:49.473 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:49.552 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:49.557 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:49.561 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:49.567 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:49.574 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:49.580 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:49.586 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:49.591 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:49.595 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:49.601 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:50.057 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:50.064 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:50.069 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:50.073 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:50.078 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:50.083 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:50.089 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:50.092 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:50.097 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:50.101 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:50.553 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:50.557 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:50.561 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:50.565 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:50.569 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:50.573 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:50.578 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:50.582 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:50.587 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:50.590 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:51.051 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:51.055 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:51.058 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:51.063 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:51.067 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:51.071 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:51.075 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:51.079 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:51.084 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:51.087 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:51.552 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:51.556 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:51.561 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:51.566 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:51.572 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:51.577 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:51.582 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:51.588 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:51.592 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:51.598 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:52.051 INFO 17776 --- [io-8080-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:52.056 INFO 17776 --- [nio-8080-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:52.061 INFO 17776 --- [nio-8080-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:52.065 INFO 17776 --- [nio-8080-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:52.070 INFO 17776 --- [nio-8080-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:52.073 INFO 17776 --- [nio-8080-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:52.077 INFO 17776 --- [nio-8080-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:52.081 INFO 17776 --- [nio-8080-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:52.087 INFO 17776 --- [nio-8080-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:52.090 INFO 17776 --- [nio-8080-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

8081執行結果

2020-10-08 15:44:46.166 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:100

2020-10-08 15:44:46.212 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:99

2020-10-08 15:44:46.236 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:98

2020-10-08 15:44:46.260 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:97

2020-10-08 15:44:46.287 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:96

2020-10-08 15:44:46.309 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:95

2020-10-08 15:44:46.335 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:94

2020-10-08 15:44:46.360 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:93

2020-10-08 15:44:46.386 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:92

2020-10-08 15:44:46.410 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:91

2020-10-08 15:44:46.655 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:90

2020-10-08 15:44:46.743 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:89

2020-10-08 15:44:46.770 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:88

2020-10-08 15:44:46.793 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:87

2020-10-08 15:44:46.818 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:86

2020-10-08 15:44:46.844 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:85

2020-10-08 15:44:46.868 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:84

2020-10-08 15:44:46.895 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:83

2020-10-08 15:44:46.918 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:82

2020-10-08 15:44:46.944 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:81

2020-10-08 15:44:47.155 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:80

2020-10-08 15:44:47.178 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:79

2020-10-08 15:44:47.201 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:78

2020-10-08 15:44:47.226 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:77

2020-10-08 15:44:47.251 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:76

2020-10-08 15:44:47.276 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:75

2020-10-08 15:44:47.302 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:74

2020-10-08 15:44:47.330 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:73

2020-10-08 15:44:47.352 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:72

2020-10-08 15:44:47.378 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:71

2020-10-08 15:44:47.667 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:66

2020-10-08 15:44:47.726 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:64

2020-10-08 15:44:47.776 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:62

2020-10-08 15:44:47.809 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:60

2020-10-08 15:44:47.859 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:58

2020-10-08 15:44:47.909 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:56

2020-10-08 15:44:47.959 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:54

2020-10-08 15:44:47.986 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:53

2020-10-08 15:44:48.011 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:52

2020-10-08 15:44:48.037 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:51

2020-10-08 15:44:48.159 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:46

2020-10-08 15:44:48.201 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:44

2020-10-08 15:44:48.251 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:42

2020-10-08 15:44:48.293 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:40

2020-10-08 15:44:48.352 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:38

2020-10-08 15:44:48.476 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:36

2020-10-08 15:44:48.517 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:34

2020-10-08 15:44:48.543 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:33

2020-10-08 15:44:48.594 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:31

2020-10-08 15:44:48.649 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:29

2020-10-08 15:44:48.724 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:27

2020-10-08 15:44:48.843 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:25

2020-10-08 15:44:48.893 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:23

2020-10-08 15:44:48.935 ERROR 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : 搶到庫存還是剩:21

2020-10-08 15:44:48.984 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:19

2020-10-08 15:44:49.035 ERROR 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : 搶到庫存還是剩:17

2020-10-08 15:44:49.176 ERROR 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : 搶到庫存還是剩:11

2020-10-08 15:44:49.201 ERROR 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : 搶到庫存還是剩:10

2020-10-08 15:44:49.226 ERROR 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : 搶到庫存還是剩:9

2020-10-08 15:44:49.243 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:8

2020-10-08 15:44:49.279 ERROR 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : 搶到庫存還是剩:7

2020-10-08 15:44:49.325 ERROR 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : 搶到庫存還是剩:5

2020-10-08 15:44:49.342 ERROR 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : 搶到庫存還是剩:4

2020-10-08 15:44:49.367 ERROR 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : 搶到庫存還是剩:3

2020-10-08 15:44:49.426 ERROR 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : 搶到庫存還是剩:1

2020-10-08 15:44:49.455 INFO 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:49.461 INFO 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:49.467 INFO 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:49.471 INFO 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:49.476 INFO 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:49.656 INFO 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:49.661 INFO 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:49.666 INFO 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:49.672 INFO 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:49.676 INFO 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:49.682 INFO 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:49.687 INFO 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:49.692 INFO 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:49.697 INFO 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:49.703 INFO 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:50.156 INFO 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

2020-10-08 15:44:50.159 INFO 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:50.164 INFO 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:50.168 INFO 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:50.173 INFO 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:50.177 INFO 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:50.181 INFO 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:50.185 INFO 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:50.189 INFO 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:50.193 INFO 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:50.664 INFO 19304 --- [nio-8081-exec-8] c.d.redisson.controller.TestController : ------------------->>>66沒有庫存了<<<------------------

2020-10-08 15:44:50.673 INFO 19304 --- [nio-8081-exec-1] c.d.redisson.controller.TestController : ------------------->>>59沒有庫存了<<<------------------

2020-10-08 15:44:50.678 INFO 19304 --- [nio-8081-exec-2] c.d.redisson.controller.TestController : ------------------->>>60沒有庫存了<<<------------------

2020-10-08 15:44:50.683 INFO 19304 --- [io-8081-exec-10] c.d.redisson.controller.TestController : ------------------->>>68沒有庫存了<<<------------------

2020-10-08 15:44:50.687 INFO 19304 --- [nio-8081-exec-3] c.d.redisson.controller.TestController : ------------------->>>61沒有庫存了<<<------------------

2020-10-08 15:44:50.691 INFO 19304 --- [nio-8081-exec-4] c.d.redisson.controller.TestController : ------------------->>>62沒有庫存了<<<------------------

2020-10-08 15:44:50.696 INFO 19304 --- [nio-8081-exec-5] c.d.redisson.controller.TestController : ------------------->>>63沒有庫存了<<<------------------

2020-10-08 15:44:50.701 INFO 19304 --- [nio-8081-exec-6] c.d.redisson.controller.TestController : ------------------->>>64沒有庫存了<<<------------------

2020-10-08 15:44:50.706 INFO 19304 --- [nio-8081-exec-7] c.d.redisson.controller.TestController : ------------------->>>65沒有庫存了<<<------------------

2020-10-08 15:44:50.711 INFO 19304 --- [nio-8081-exec-9] c.d.redisson.controller.TestController : ------------------->>>67沒有庫存了<<<------------------

8.總結

可以看到在分散式環境正常執行下單任務。沒有產生重複消費和過渡消費