public interface DistributedLock { public static final long TIMEOUT_MILLIS = 30000; public static final int RETRY_TIMES = 10; public static final long SLEEP_MILLIS = 500; public boolean lock(String key); public boolean lock(String key, int retryTimes); public boolean lock(String key, int retryTimes, long sleepMillis); public boolean lock(String key, long expire); public boolean lock(String key, long expire, int retryTimes); public boolean lock(String key, long expire, int retryTimes, long sleepMillis); public boolean releaseLock(String key);}复制代码
2.依然是接口
public abstract class AbstractDistributedLock implements DistributedLock { @Override public boolean lock(String key) { return lock(key , TIMEOUT_MILLIS, RETRY_TIMES, SLEEP_MILLIS); } @Override public boolean lock(String key, int retryTimes) { return lock(key, TIMEOUT_MILLIS, retryTimes, SLEEP_MILLIS); } @Override public boolean lock(String key, int retryTimes, long sleepMillis) { return lock(key, TIMEOUT_MILLIS, retryTimes, sleepMillis); } @Override public boolean lock(String key, long expire) { return lock(key, expire, RETRY_TIMES, SLEEP_MILLIS); } @Override public boolean lock(String key, long expire, int retryTimes) { return lock(key, expire, retryTimes, SLEEP_MILLIS); }}复制代码
3.实现类
@Componentpublic class RedisDistributedLock extends AbstractDistributedLock { @Autowired @Resource(name="redisTemplateMaster") private RedisTemplate