在单进程的系统中,当存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步,使其在修改这种变量时能够线性执行消除并发修改变量。
而Java提供的同步锁synchronized只能解决单台服务器上的并发问题,一般线上环境都是多台服务器部署同时运行,跨jvm的环境下synchronized的作用就不大了。这个时候redis就可以作为分布锁来使用了,一般都是基于redis setNx命令来实现自己的锁,建出来的键值一般都是有一个超时时间的(这个是Redis自带的超时特性),所以每个锁最终都会释放
这里介绍的是github上的一个开源项目,国外大神些的算法毕竟比自己实现的靠谱点
项目源码地址,里面不光有分布锁,还有分布锁队列等等一系列模块
https:
pom.xml配置文件引人redisson相关包
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-all</artifactId>
<version>2.10.5</version>
</dependency>
2. 配置方法
2.1. 程序化配置方法
Redisson程序化的配置方法是通过构建Config对象实例来实现的。例如:
public class RedissonUtils {
private static RedissonClient redissonClient;
static{
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword(null);
redissonClient = Redisson.create(config);
}
public static RedissonClient getRedisson(){
return redissonClient;
}
public static void main(String[] args) throws InterruptedException{
RLock fairLock = getRedisson().getLock("TEST_KEY");
System.out.println(fairLock.toString());
boolean res = fairLock.tryLock(10, 10, TimeUnit.SECONDS);
System.out.println(res);
fairLock.unlock();
}
2 spring集成的方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:redisson="http://redisson.org/schema/redisson"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://redisson.org/schema/redisson
http://redisson.org/schema/redisson/redisson.xsd">
<context:component-scan base-package="component.controller.RedisServiceImpl" />
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis_max_idle}"></property>
<property name="testOnBorrow" value="${redis_test_on_borrow}"></property>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis_addr}"></property>
<property name="port" value="${redis_port}"></property>
<property name="password" value="${redis_auth}"></property>
<property name="poolConfig" ref="jedisConfig"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<redisson:client id="redissonClient">
<redisson:single-server address="redis://127.0.0.1:6379" connection-pool-size="30" />
</redisson:client>
</beans>
引入RedissonClient就可以使用了
@Autowired
private RedissonClient redissonClient;
//获取redissson分布式锁
RLock fairLock = redissonClient.getLock("TEST_KEY");
//尝试加锁 最多等待100秒 20秒后自动销毁
boolean res = fairLock.tryLock(100, 20, TimeUnit.SECONDS);
//释放锁
fairLock.unlock();
提醒:redis版本一定要3.0以上的 不然会报错,redis lua脚本