Redisson锁简单使用
前言:
1,redisson锁解决分布式锁,锁失效
依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.3</version>
</dependency>
使用
配置:
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;
/**
* @author psd redisson配置类
*/
@Configuration
public class RedissonConfig{
@Bean
public RedissonClient redissonClient(){
Config config = new Config();
config.useSingleServer().setAddress("redis://192.168.124.104:6379");
return Redisson.create(config);
}
}
@Autowired
private RedissonClient redissonClient;
public void testLock1208() {
// 获取锁
RLock rLock = this.redissonClient.getLock("lock");
rLock.lock();
String numString = this.redisTemplate.opsForValue().get("num");
if (StringUtils.isBlank(numString)) {
return;
}
int num = Integer.parseInt(numString);
this.redisTemplate.opsForValue().set("num", String.valueOf(++num));
// 解锁
rLock.unlock();
}
测试:
ab压测:
ab -n 1000 -c 100 http://172.116.22.454:8888/index/test/testLock1208
喜欢我的文章记得点个在看,或者点赞,持续更新中ing…