redislock - 基于redis的分布式可重入锁
redislock - 基于redis的分布式可重入锁
Java 分布式应用
访问GitHub主页
共3Star
详细介绍
redislock - 基于redis的分布式可重入锁
是什么
redislock 是一个基于Redis的分布式可重入锁
怎么用
maven
<dependency>
<groupId>com.imadcn.framework</groupId>
<artifactId>lock</artifactId>
<version>0.0.1</version>
</dependency>
XML配置
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:lock="http://code.imadcn.com/schema/lock"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.imadcn.com/schema/lock
http://code.imadcn.com/schema/lock/lock.xsd">
<!-- redis pool -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="true" />
</bean>
<!-- redis sentinel -->
<bean id="redisSentinelConfig" class="com.imadcn.framework.lock.config.RedisSentinelConfig">
<property name="masterName" value="mymaster" />
<property name="sentinelAddrs" value="127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />
</bean>
<!-- redis connectionFactory -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<constructor-arg index="0" ref="redisSentinelConfig" />
<constructor-arg index="1" ref="jedisPoolConfig" />
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<!-- redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
<property name="stringSerializer" ref="stringRedisSerializer" />
</bean>
<!-- redisMessageListenerContainer -->
<bean id="redisMessageListenerContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="topicSerializer" ref="stringRedisSerializer" />
</bean>
<lock:config id="lockManager" group="physical-exam" redisTemplate="redisTemplate" messageContainer="redisMessageListenerContainer"/>
</beans>
API
@Autowired
private RedisLockManager manager;
public void lock() {
RedisLock redisLock = manager.getLock("asd");
redisLock.lock();
redisLock.unlock();
}
配置参考
<redislock:config /> redislock 配置
属性 | 类型 | 必填 | 缺省值 | 描述 |
---|---|---|---|---|
id | String | 是 | Spring容器中的ID | |
group | String | 是 | 分组名,可以为不同业务分配分组 | |
redisTemplate | String | 是 | redisTemplate | |
messageContainer | String | 是 | RedisMessageListenerContainer |
推荐源码
暂无源码 更多源码...