com.imadcn.framework:lock

distributed redis reentrant lock

License

License

GroupId

GroupId

com.imadcn.framework
ArtifactId

ArtifactId

lock
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.imadcn.framework:lock
distributed redis reentrant lock
Project URL

Project URL

https://github.com/imadcn/redislock
Source Code Management

Source Code Management

https://github.com/imadcn/redislock.git

Download lock

How to add to project

<!-- https://jarcasting.com/artifacts/com.imadcn.framework/lock/ -->
<dependency>
    <groupId>com.imadcn.framework</groupId>
    <artifactId>lock</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.imadcn.framework/lock/
implementation 'com.imadcn.framework:lock:1.1.0'
// https://jarcasting.com/artifacts/com.imadcn.framework/lock/
implementation ("com.imadcn.framework:lock:1.1.0")
'com.imadcn.framework:lock:jar:1.1.0'
<dependency org="com.imadcn.framework" name="lock" rev="1.1.0">
  <artifact name="lock" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.imadcn.framework', module='lock', version='1.1.0')
)
libraryDependencies += "com.imadcn.framework" % "lock" % "1.1.0"
[com.imadcn.framework/lock "1.1.0"]

Dependencies

compile (7)

Group / Artifact Type Version
org.springframework : spring-core jar 4.3.15.RELEASE
org.springframework : spring-context jar 4.3.15.RELEASE
org.springframework : spring-beans jar 4.3.15.RELEASE
org.springframework : spring-context-support jar 4.3.15.RELEASE
org.springframework : spring-aop jar 4.3.15.RELEASE
org.springframework.data : spring-data-redis jar 1.8.11.RELEASE
redis.clients : jedis jar 2.9.0

Project Modules

There are no modules declared in this project.

redislock - 基于redis的分布式可重入锁

Build Status Maven Central License

是什么

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

Versions

Version
1.1.0
0.0.1