Redisson实现集群环境下的分布式锁十分简单:
引入依赖
<?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.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>2.1.7.RELEASE</version> </dependency> //Redisson起步依赖 <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.11.2</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://192.168.37.134:7001","redis://192.168.37.134:7002", "redis://192.168.37.134:7003","redis://192.168.37.134:7004","redis://192.168.37.134:7005","redis://192.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线程并发,循环2次
控制台结果输出
redis缓存
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。