Java
package com.example.springboot_redis.contoller;
import com.example.springboot_redis.domain.User;
import com.example.springboot_redis.redis.DistributedRedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
/**
* 测试Redis的controller
*
* @author qp
* @date 2019/7/19 9:50
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private HttpServletRequest request;
@Autowired
private DistributedRedisLock distributedRedisLock;
@GetMapping("/test")
public String getRedis() throws InterruptedException {
String requestkey = UUID.randomUUID().toString();
distributedRedisLock.lock("LOCK");
System.out.println(requestkey+" start");
Thread.sleep(5000);
System.out.println(requestkey+" end ");
distributedRedisLock.unlock("LOCK");
return "SUCCESS";
}
}
String requestkey = UUID.randomUUID().toString(); 请求key 保持请求唯一性
distributedRedisLock.lock("LOCK"); 加锁Thread.sleep(5000); 模拟页面请求时间5s distributedRedisLock.unlock("LOCK"); 释放锁
DistributedRedisLock.jva
package com.example.springboot_redis.redis;
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.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 分布式Redis锁
*
* @author qp
* @date 2019/7/19 15:21
*/
@Slf4j
@Component
public class DistributedRedisLock {
@Autowired
private RedissonClient redissonClient;
// 加锁
public Boolean lock(String lockName) {
try {
if (redissonClient == null) {
// log.info("DistributedRedisLock redissonClient is null");
return false;
}
RLock lock = redissonClient.getLock(lockName);
// 锁10秒后自动释放,防止死锁
lock.lock(5, TimeUnit.SECONDS);
// log.info("Thread [{}] DistributedRedisLock lock [{}] success", Thread.currentThread().getName(), lockName);
// 加锁成功
return true;
} catch (Exception e) {
// log.error("DistributedRedisLock lock [{}] Exception:", lockName, e);
return false;
}
}
// 释放锁
public Boolean unlock(String lockName) {
try {
if (redissonClient == null) {
// log.info("DistributedRedisLock redissonClient is null");
return false;
}
RLock lock = redissonClient.getLock(lockName);
lock.unlock();
//log.info("Thread [{}] DistributedRedisLock unlock [{}] success", Thread.currentThread().getName(), lockName);
// 释放锁成功
return true;
} catch (Exception e) {
// log.error("DistributedRedisLock unlock [{}] Exception:", lockName, e);
return false;
}
}
}
测试
Jmeter 测试10次并发
如果去掉Lock锁 ,效果如下
public String getRedis() throws InterruptedException {
String requestkey = UUID.randomUUID().toString();
//distributedRedisLock.lock("LOCK");
System.out.println(requestkey+" start");
Thread.sleep(new Random(25).nextInt(100));
System.out.println(requestkey+" end ");
// distributedRedisLock.unlock("LOCK");
return "SUCCESS";
}