# redis-distribute-lock
**Repository Path**: zhongmh/redis-distribute-lock
## Basic Information
- **Project Name**: redis-distribute-lock
- **Description**: 基于redis、Spring实现的分布式锁,对业务方法无侵入
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2021-08-30
- **Last Updated**: 2022-07-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
#### 使用教程
引入jar包仓库地址
```aidl
yunmaozj
Nexus Repository
http://maven.yunmaozj.com/repository/yunmaozj/
```
坐标
```aidl
com.yunmaozj.tools
redis-lock-spring
1.0.0
```
###使用步骤
1. 使用 @EnableDistributedLock 注解
```aidl
@EnableDistributedLock
@Configuration
public class ApplicationTestConfiguration {
}
```
2. 在业务方法上使用 @DistributedLock 注解
```aidl
@Override
@DistributedLock
public User getUserById(int userid) {
return new User(userid, "Strickland", 10);
}
```
### 配置
在spring项目加入配置
```aidl
redis.host=127.0.0.1
redis.port=6379
```
### 关于锁的粒度问题
我们主要是使用方法类名+方法名字来控制锁的粒度(例如:com.yunmaozj.tools.lock.service.UserService#getUserById),
如果想要自定义可以参考以下示例:
```aidl
@DistributedLock(businessIdGenerator = UserBusinessIdGenerator.class)
User getUserById(int userid);
class UserBusinessIdGenerator implements BusinessIdGenerator {
@Override
public String getBusinessId(Method method, Class> targetClass, Object[] params) {
return targetClass.getName()+"#"+method.getName()+"-"+params[0];
}
}
```