Redis应用场景-分布式锁

1.创建springboot项目


2.添加依赖

<?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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>distinct-lock</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>distinct-lock</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


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


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

        <!--①依赖-->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.24.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.实体类

@Data
@TableName("tbl_stock")
public class Stock {
    @TableId(type = IdType.AUTO)
    private int productid;
    @TableField("num")
    private int stock;
}

4.dao

@Mapper
public interface StockDao {
    @Select("select num from tbl_stock where productid=#{productid}")
    int findById(Integer productid);

    @Update("update tbl_stock set num=num-1 where productid=#{productid} ")
    void update(Integer productid);
}

5.server

@Service
public class StockService {

    @Autowired
    private StockDao stockDao;
    @Autowired
    private RedissonClient redisson;

    //
    public String decrement(Integer productid) {
        RLock lock = redisson.getLock("product::" + productid);
        lock.lock();
        try {
            //根据id查询商品的库存: 提前预热到redis缓存中
            int num = stockDao.findById(productid);
            if (num > 0) {
                //修改库存---incr---定时器[redis  数据库同步]
                stockDao.update(productid);
                System.out.println("商品编号为:" + productid + "的商品库存剩余:" + (num - 1) + "个");
                return "商品编号为:" + productid + "的商品库存剩余:" + (num - 1) + "个";
            } else {
                System.out.println("商品编号为:" + productid + "的商品库存不足。");
                return "商品编号为:" + productid + "的商品库存不足。";
            }
        }finally {
            lock.unlock();
        }
    }
}

6.controller

@RestController
public class StockController {

    @Autowired
    private StockService stockService;

    //根据商品编号减库存 每次减一个
    @GetMapping("/incr/{productid}")
    public String incr(@PathVariable Integer productid){
         return stockService.decrement(productid);
    }
}

7.config

@Configuration
public class RedissonConfig {

    @Bean
    public RedissonClient redisson(){
        Config config = new Config();
//        //连接的为redis集群
//        config.useClusterServers()
//                // use "rediss://" for SSL connection
//                .addNodeAddress("redis://127.0.0.1:7181","","","")
//        ;
        //连接单机
        config.useSingleServer().setAddress("redis://192.168.31.137:6379");
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}

8.配置

9.使用Apipost测试

11.使用集群测试

12.安装window版的nginx,把配置文件修改为集群模式-nginx.conf

13.安装jmeter进行压测

启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值