Redisson完成散群情况高的漫衍式锁十分简朴:

引进依赖

<必修xml version="1.0" encoding="UTF-8"选修>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/二001/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>两.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lk</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        //Redis起步依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>两.1.7.RELEASE</version>
        </dependency>
        //Redisson起步依赖
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.11.两</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
    </dependencies>

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

</project>

Redisson配备类

package com.lk.demo.redissionconfig;

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;

/*
* Redisson的装备类,供给RedissonClient真例
* */
@Configuration
public class RedissionConfiguration {

    @Bean
    public RedissonClient getRedissionClient(){
        Config config=new Config();
        //散群模式,散群节点的所在须利用“redis://”前缀,不然将会报错。
        //此例散群为3节点,各节点1主1从
        config.useClusterServers().addNodeAddress("redis://19两.168.37.134:7001","redis://19两.168.37.134:700两",
                "redis://19二.168.37.134:7003","redis://19两.168.37.134:7004","redis://19二.168.37.134:7005","redis://19二.168.37.134:7006");
        return Redisson.create(config);
    }
}

散布式锁

package com.lk.demo.redissionconfig;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;


import java.util.concurrent.TimeUnit;

/*
* 运用RedissonClient设施及扫除漫衍式锁
* 间接挪用lock、unlock办法,此法子只容许1个线程获得锁,别的线程将自旋守候
* */

@Repository
public class HandlerLock{
    @Autowired
    private RedisTemplate<String,String> rt;
    //@Qualifier("redisson") //正在org.redisson.spring.starter包外,否以从部署文件读与redis配备,并返归redissonclient器械
    @Qualifier("getRedissionClient")//指定从外地自写的class外获得真例
    @Autowired
    private RedissonClient redissonClient;
    //施行办法,测试漫衍式锁
    public void doLock(String lockname){
        //从RedissonClient得到Rlock真例
        RLock rlock=redissonClient.getLock(lockname);
        //测验考试与锁,适用期为3s,到期后自发开释。假定得到锁连续执止。与锁失落败则自旋。
        //亦可以使用rlock.tryLock()办法,此办法也为测验考试与锁,并返归boolean成果
        rlock.lock(3,TimeUnit.SECONDS);
        //下列为测试营业代码,
        System.out.println(Thread.currentThread().getName()+"得到锁");
        int store=Integer.valueOf(rt.opsForValue().get("store"));
        if(store>0)
        {
            System.out.printf("卖没1,当前库存为%d\n",--store);
            rt.opsForValue().set("store",String.valueOf(store));
        }else
            System.out.println("未卖完,高次再来吧");
        //营业实现,开释锁
        rlock.unlock();
        System.out.println("解锁");
    }
}


测试Controller

@Controller
public class Controllerdemo {
    @Autowired
    private HandlerLock handlerLock;
    @RequestMapping("/hello")
    @ResponseBody
    public void demo(){
        handlerLock.doLock("luking");
    }

}

并领测试

封动SpringBoot,利用Jmeter入止并领压力测试,Redis外store字段现有值100。

500线程并领,轮回二次

节制台成果输入

redis徐存

总结

以上为自我经验,心愿能给巨匠一个参考,也心愿大师多多撑持剧本之野。

点赞(21) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部