0 概述
在实际工作中,服务都是在分布式环境下,需要有一个分布式锁,来解决分布式环境下的并发问题。本文主要讲述如何用tair 实现分布式锁。
依赖pom
<dependency>
<groupId>com.taobao.tair</groupId>
<artifactId>tair-client</artifactId>
<version>2.3.4</version>
</dependency
- 1
- 2
- 3
- 4
- 5
1 实现思路
TairManager.put 传入version(version>0)版本进行校验,cas原则会保证只有一个能成功,tair 在第一次put时候 version=1。
具体实现代码
import com.taobao.tair.DataEntry;
import com.taobao.tair.Result;
import com.taobao.tair.ResultCode;
import com.taobao.tair.impl.DefaultTairManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hsc on 17/10/18.
*/
@Component
public class TairWrapper {
private DefaultTairManager tairManager;
@Value("${tair.master}")
private String master;
@Value("${tair.slave}")
private String slave;
@Value("${tair.groupname}")
private String groupname;
@Value("${tair.namespace}")
private int namespace;
@PostConstruct
private void init() throws Exception {
tairManager = new DefaultTairManager();
List<String> configserverList = new ArrayList<String>();
configserverList.add(master);
configserverList.add(slave);
tairManager.setConfigServerList(configserverList);
tairManager.setGroupName(groupname);
tairManager.init();
}
public ResultCode put(String key, String value, int expireTime) {
return tairManager.put(namespace, key, value, expireTime);
}
public ResultCode put(String key, String value, int version, int expireTime) {
return tairManager.put(namespace, key, value, version, expireTime);
}
public Result<DataEntry> get(String key) {
return tairManager.get(namespace, key);
}
public ResultCode delete(String key) {
return tairManager.delete(namespace, key);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
package com.mogujie.enzo.service.tair.impl;
import com.mogujie.enzo.tair.TairWrapper;
import com.taobao.tair.DataEntry;
import com.taobao.tair.Result;
import com.taobao.tair.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by hsc on 17/10/18.
*/
@Service("lockService")
public class LockService {
/**
* 锁过期时间 5s
*/
private static final int LOCK_EXPIRE_TIME = 5;
@Resource
private TairWrapper tairWrapper;
/**
* tair 在第一次put version=1
* 当 version 大于0时候,会根据version 比较,如果vesion 相等则更新
* version=0 时候会更新
*/
private final static int INIT_VERSION = 2;
private static final int lockWaitTimeOut = 5000;
private static final int retryTime = 200;
private static final Logger logger = LoggerFactory.getLogger(LockService.class);
//获取锁
public boolean getCacheLock(String cacheKey) {
if (StringUtils.isEmpty(cacheKey)) {
return true;
}
int waitTimeOut = lockWaitTimeOut;
try {
while (waitTimeOut > 0) {
//如果put成功说明加锁成功
if (this.setnxWithExpire(cacheKey, cacheKey)) {
return true;
}
waitTimeOut -= retryTime;
//如果 tair 挂了这里会异常,快速失败
String value = this.getLockValue(cacheKey);
if (value == null) {
continue;
}
sleep(retryTime);
}
//程序走到这里说明锁等待一定的时间,所以这里释放这个锁
delCacheLock(cacheKey);
} catch (Exception ex) {
logger.error("getCacheLock exception key:{}", cacheKey, ex);
}
return true;
}
//释放锁
public boolean delCacheLock(String cacheKey) {
try {
ResultCode result = tairWrapper.delete(cacheKey);
return ResultCode.SUCCESS.equals(result);
} catch (Exception ex) {
logger.error("delete cacheKey exception key:{}", cacheKey, ex);
}
return false;
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (Exception ex) {
logger.error("sleep exception", ex);
}
}
private String getLockValue(String lockKey) {
Result<DataEntry> getResult = tairWrapper.get(lockKey);
if (getResult != null && ResultCode.SUCCESS.equals(getResult.getRc())) {
Object obj = getResult.getValue().getValue();
return obj == null ? null : obj.toString();
}
return null;
}
private boolean setnxWithExpire(String cacheKey, String value) {
ResultCode resultCode = tairWrapper.put(cacheKey, value, INIT_VERSION, LOCK_EXPIRE_TIME);
return ResultCode.SUCCESS.equals(resultCode);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
参考文献
[1] https://yq.aliyun.com/articles/58928