pom文件配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
application.yml配置:
server:
port: 8081
spring:
redis:
jedis:
pool:
# 最大空闲连接数
max-idle: 500
# 最小空闲连接数
min-idle: 50
# 等待可用连接的最大时间,负数为不限制
max-wait: -1s
# 最大活跃连接数,负数为不限制
max-active: -1
host: 192.168.67.129
port: 6378
timeout: 30s
RedisLockConfiguration配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
@Configuration
public class RedisLockConfiguration {
@Bean
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
return new RedisLockRegistry(redisConnectionFactory, "redis-lock", 5000L);
}
}
controller:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.locks.Lock;
@RestController
@RequestMapping("/redis")
public class RedisLockController {
Logger logger = LoggerFactory.getLogger(RedisLockController.class);
private int item = 50;//模拟只有50个库存量
private static final String KEY = "item";
@Autowired
private RedisLockRegistry redisLockRegistry;
@RequestMapping("/sale")
public String sale() {
while (item > 0) {
Lock lock = redisLockRegistry.obtain(KEY);
lock(lock);
try {
if (item > 0){
logger.info(Thread.currentThread().getName()+"抢到资源");
item--;
return Thread.currentThread().getName()+"抢到资源,剩余:"+item+"个";
}
}finally {
lock.unlock();
}
}
logger.info(Thread.currentThread().getName()+"抢不到资源");
return Thread.currentThread().getName()+"抢不到资源";
}
@RequestMapping("/test")
public String test(){
return "测试";
}
public void lock(Lock lock){
//1.尝试加锁
try {
if(lock.tryLock()){
return ;
}
} catch (Exception e) {
e.printStackTrace();
}
//2.加锁失败,当前任务休眠一段时间
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//3.递归调用,再次去抢锁
lock(lock);
}
}
使用jmeter发送请求:
调用日志:
2020-04-09 12:16:27.948 INFO 13832 --- [io-8081-exec-25] c.sune.controller.RedisLockController : http-nio-8081-exec-25抢到资源
2020-04-09 12:16:28.236 INFO 13832 --- [nio-8081-exec-5] c.sune.controller.RedisLockController : http-nio-8081-exec-5抢到资源
2020-04-09 12:16:28.509 INFO 13832 --- [io-8081-exec-19] c.sune.controller.RedisLockController : http-nio-8081-exec-19抢到资源
2020-04-09 12:16:28.802 INFO 13832 --- [io-8081-exec-11] c.sune.controller.RedisLockController : http-nio-8081-exec-11抢到资源
2020-04-09 12:16:29.127 INFO 13832 --- [nio-8081-exec-6] c.sune.controller.RedisLockController : http-nio-8081-exec-6抢到资源
2020-04-09 12:16:29.452 INFO 13832 --- [io-8081-exec-48] c.sune.controller.RedisLockController : http-nio-8081-exec-48抢到资源
2020-04-09 12:16:29.754 INFO 13832 --- [io-8081-exec-14] c.sune.controller.RedisLockController : http-nio-8081-exec-14抢到资源
2020-04-09 12:16:29.983 INFO 13832 --- [io-8081-exec-20] c.sune.controller.RedisLockController : http-nio-8081-exec-20抢到资源
2020-04-09 12:16:30.234 INFO 13832 --- [io-8081-exec-21] c.sune.controller.RedisLockController : http-nio-8081-exec-21抢到资源
2020-04-09 12:16:30.453 INFO 13832 --- [nio-8081-exec-3] c.sune.controller.RedisLockController : http-nio-8081-exec-3抢到资源
2020-04-09 12:16:30.646 INFO 13832 --- [nio-8081-exec-7] c.sune.controller.RedisLockController : http-nio-8081-exec-7抢到资源
2020-04-09 12:16:30.911 INFO 13832 --- [io-8081-exec-54] c.sune.controller.RedisLockController : http-nio-8081-exec-54抢到资源
2020-04-09 12:16:31.126 INFO 13832 --- [io-8081-exec-41] c.sune.controller.RedisLockController : http-nio-8081-exec-41抢到资源
2020-04-09 12:16:31.390 INFO 13832 --- [io-8081-exec-13] c.sune.controller.RedisLockController : http-nio-8081-exec-13抢到资源
2020-04-09 12:16:31.561 INFO 13832 --- [io-8081-exec-46] c.sune.controller.RedisLockController : http-nio-8081-exec-46抢到资源
2020-04-09 12:16:31.773 INFO 13832 --- [io-8081-exec-44] c.sune.controller.RedisLockController : http-nio-8081-exec-44抢到资源
2020-04-09 12:16:31.957 INFO 13832 --- [io-8081-exec-43] c.sune.controller.RedisLockController : http-nio-8081-exec-43抢到资源
2020-04-09 12:16:32.246 INFO 13832 --- [io-8081-exec-36] c.sune.controller.RedisLockController : http-nio-8081-exec-36抢到资源
2020-04-09 12:16:32.424 INFO 13832 --- [io-8081-exec-15] c.sune.controller.RedisLockController : http-nio-8081-exec-15抢到资源
2020-04-09 12:16:32.626 INFO 13832 --- [io-8081-exec-51] c.sune.controller.RedisLockController : http-nio-8081-exec-51抢到资源
2020-04-09 12:16:32.833 INFO 13832 --- [io-8081-exec-37] c.sune.controller.RedisLockController : http-nio-8081-exec-37抢到资源
2020-04-09 12:16:33.000 INFO 13832 --- [io-8081-exec-32] c.sune.controller.RedisLockController : http-nio-8081-exec-32抢到资源
2020-04-09 12:16:33.189 INFO 13832 --- [io-8081-exec-35] c.sune.controller.RedisLockController : http-nio-8081-exec-35抢到资源
2020-04-09 12:16:33.354 INFO 13832 --- [io-8081-exec-33] c.sune.controller.RedisLockController : http-nio-8081-exec-33抢到资源
2020-04-09 12:16:33.519 INFO 13832 --- [io-8081-exec-53] c.sune.controller.RedisLockController : http-nio-8081-exec-53抢到资源
2020-04-09 12:16:33.680 INFO 13832 --- [io-8081-exec-34] c.sune.controller.RedisLockController : http-nio-8081-exec-34抢到资源
2020-04-09 12:16:33.847 INFO 13832 --- [io-8081-exec-26] c.sune.controller.RedisLockController : http-nio-8081-exec-26抢到资源
2020-04-09 12:16:34.032 INFO 13832 --- [io-8081-exec-24] c.sune.controller.RedisLockController : http-nio-8081-exec-24抢到资源
2020-04-09 12:16:34.216 INFO 13832 --- [io-8081-exec-30] c.sune.controller.RedisLockController : http-nio-8081-exec-30抢到资源
2020-04-09 12:16:34.408 INFO 13832 --- [nio-8081-exec-8] c.sune.controller.RedisLockController : http-nio-8081-exec-8抢到资源
2020-04-09 12:16:34.577 INFO 13832 --- [nio-8081-exec-2] c.sune.controller.RedisLockController : http-nio-8081-exec-2抢到资源
2020-04-09 12:16:34.736 INFO 13832 --- [io-8081-exec-40] c.sune.controller.RedisLockController : http-nio-8081-exec-40抢到资源
2020-04-09 12:16:34.906 INFO 13832 --- [io-8081-exec-42] c.sune.controller.RedisLockController : http-nio-8081-exec-42抢到资源
2020-04-09 12:16:35.094 INFO 13832 --- [io-8081-exec-28] c.sune.controller.RedisLockController : http-nio-8081-exec-28抢到资源
2020-04-09 12:16:35.260 INFO 13832 --- [io-8081-exec-52] c.sune.controller.RedisLockController : http-nio-8081-exec-52抢到资源
2020-04-09 12:16:35.441 INFO 13832 --- [io-8081-exec-12] c.sune.controller.RedisLockController : http-nio-8081-exec-12抢到资源
2020-04-09 12:16:35.609 INFO 13832 --- [io-8081-exec-22] c.sune.controller.RedisLockController : http-nio-8081-exec-22抢到资源
2020-04-09 12:16:35.778 INFO 13832 --- [io-8081-exec-17] c.sune.controller.RedisLockController : http-nio-8081-exec-17抢到资源
2020-04-09 12:16:35.949 INFO 13832 --- [io-8081-exec-45] c.sune.controller.RedisLockController : http-nio-8081-exec-45抢到资源
2020-04-09 12:16:36.119 INFO 13832 --- [io-8081-exec-49] c.sune.controller.RedisLockController : http-nio-8081-exec-49抢到资源
2020-04-09 12:16:36.284 INFO 13832 --- [io-8081-exec-29] c.sune.controller.RedisLockController : http-nio-8081-exec-29抢到资源
2020-04-09 12:16:36.460 INFO 13832 --- [io-8081-exec-38] c.sune.controller.RedisLockController : http-nio-8081-exec-38抢到资源
2020-04-09 12:16:36.621 INFO 13832 --- [nio-8081-exec-4] c.sune.controller.RedisLockController : http-nio-8081-exec-4抢到资源
2020-04-09 12:16:36.788 INFO 13832 --- [nio-8081-exec-9] c.sune.controller.RedisLockController : http-nio-8081-exec-9抢到资源
2020-04-09 12:16:36.965 INFO 13832 --- [nio-8081-exec-1] c.sune.controller.RedisLockController : http-nio-8081-exec-1抢到资源
2020-04-09 12:16:37.141 INFO 13832 --- [io-8081-exec-16] c.sune.controller.RedisLockController : http-nio-8081-exec-16抢到资源
2020-04-09 12:16:37.316 INFO 13832 --- [io-8081-exec-27] c.sune.controller.RedisLockController : http-nio-8081-exec-27抢到资源
2020-04-09 12:16:37.485 INFO 13832 --- [io-8081-exec-39] c.sune.controller.RedisLockController : http-nio-8081-exec-39抢到资源
2020-04-09 12:16:37.662 INFO 13832 --- [io-8081-exec-50] c.sune.controller.RedisLockController : http-nio-8081-exec-50抢到资源
2020-04-09 12:16:37.829 INFO 13832 --- [io-8081-exec-31] c.sune.controller.RedisLockController : http-nio-8081-exec-31抢到资源
2020-04-09 12:16:38.121 INFO 13832 --- [io-8081-exec-55] c.sune.controller.RedisLockController : http-nio-8081-exec-55抢不到资源
2020-04-09 12:16:38.285 INFO 13832 --- [io-8081-exec-23] c.sune.controller.RedisLockController : http-nio-8081-exec-23抢不到资源
2020-04-09 12:16:38.454 INFO 13832 --- [io-8081-exec-18] c.sune.controller.RedisLockController : http-nio-8081-exec-18抢不到资源
2020-04-09 12:16:38.614 INFO 13832 --- [io-8081-exec-10] c.sune.controller.RedisLockController : http-nio-8081-exec-10抢不到资源
2020-04-09 12:16:38.853 INFO 13832 --- [io-8081-exec-47] c.sune.controller.RedisLockController : http-nio-8081-exec-47抢不到资源
有5个线程是抢不到资源,结果正确.