赞
踩
redisson的整合很简单,这里直接上代码和演示“读写锁”测试,其他更多的“并发锁”,详情见官方文档(地址见文章末尾),官方写得很详细,这里不赘述了。
<!-- 导入 redission 依赖 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @description Redisson 配置类
*/
@Configuration
public class MyRedissonConfig {
@Value(value = "${spring.redis.host}")
private String host;
@Value(value = "${spring.redis.port}")
private int port;
@Value(value = "${spring.redis.database}")
private int database;
@Value(value = "${spring.redis.password}")
private String password;
/**
* 单Redis节点模式配置方法
* 其他配置參數,看:
* <a href = "https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#26-%E5%8D%95redis%E8%8A%82%E7%82%B9%E6%A8%A1%E5%BC%8F">
* 单Redis节点模式配置方法
* </a>
*
* @return {@link RedissonClient}
*/
@Bean(destroyMethod = "shutdown")
RedissonClient redisson() {
Config config = new Config();
//Redis多节点
// config.useClusterServers()
// .addNodeAddress("redis://127.0.0.1:6379", "redis://127.0.0.1:7001");
//Redis单节点
SingleServerConfig singleServerConfig = config.useSingleServer();
//可以用"rediss://"来启用SSL连接
String address = "redis://" + host + ":" + port;
singleServerConfig.setAddress(address);
//设置 数据库编号
singleServerConfig.setDatabase(database);
singleServerConfig.setPassword(password);
//连接池大小:默认值:64
// singleServerConfig.setConnectionPoolSize()
return Redisson.create(config);
}
}
演示“读写锁”测试,其他更多的“并发锁”,详情见官方文档(地址见文章末尾),官方写得很详细,这里不赘述了。
- 读锁和写锁共用一个锁key,命名为:readWriteLock;
- 读锁接口只负责读, JMeter压测每秒并发20,持续压测;
- 写锁接口负责向缓存中写入随机字符串 UUID ,线程睡眠3秒, 模拟业务处理;
- 先启动压测并发20读接口,然后发起“写锁请求1”,再发起“写锁请求2”。观察控制台打印的结果。
/**
* 读写锁(ReadWriteLock) 测试1
* 读锁:共享锁,所有线程共享读锁
* 写锁: 独占锁,只能单独线程占用,执行完才能执行其他“读写锁”。
*/
@ResponseBody
@GetMapping("/test/readWriteLock/toRead")
public String toReadLock() {
//获取 锁
RReadWriteLock readWriteLock = redisson.getReadWriteLock("readWriteLock");
RLock rLock = readWriteLock.readLock();
//上 读锁
rLock.lock();
String writeValue = "";
try {
log.info("【读锁-{}】加锁成功,读数据...", Thread.currentThread()
.getId());
writeValue = redisTemplate.opsForValue()
.get("writeValue");
} finally {
log.info("【读锁-{}】解锁成功,uuid={}", Thread.currentThread()
.getId(),writeValue);
//解锁
rLock.unlock();
}
return writeValue;
}
/**
* 读写锁(ReadWriteLock) 测试2
*/
@ResponseBody
@GetMapping("/test/readWriteLock/toWrite")
public String toWriteLock() {
//获取 锁
RReadWriteLock readWriteLock = redisson.getReadWriteLock("readWriteLock");
RLock rLock = readWriteLock.writeLock();
//上 写锁
rLock.lock();
String uuid = UUID.randomUUID()
.toString();
try {
log.info("【写锁-{}】加锁成功,睡眠3秒,模拟执行业务...", Thread.currentThread()
.getId());
Thread.sleep(3000);
redisTemplate.opsForValue()
.set("writeValue", uuid);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
log.info("【写锁-{}】解锁成功,uuid={}", Thread.currentThread()
.getId(),uuid);
//解锁
rLock.unlock();
}
return "writeLock ok,uuid = " + uuid;
}
并发20读锁接口,观察redis缓存值变化,如下图:
写锁接口请求时,观察redis缓存值变化,如下图:
写锁请求1,控制台打印,如下图:
写锁请求2,控制台打印,如下图:
结论:读锁是共享锁,写锁是独占锁。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。