0 概述
在實際工作中,服務都是在分佈式環境下,需要有一個分佈式鎖,來解決分佈式環境下的併發問題。本文主要講述如何用tair 實現分佈式鎖。
依賴pom
<dependency>
<groupId>com.taobao.tair</groupId>
<artifactId>tair-client</artifactId>
<version>2.3.4</version>
</dependency
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);
}
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] https://yq.aliyun.com/articles/58928