Browse Source

redisson锁

dev_shibei_match
zxc 5 years ago
parent
commit
cd418b03bc
  1. 79
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/DistributedLock.java
  2. 12
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/LockConstants.java
  3. 23
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java
  4. 48
      epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java
  5. 21
      epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/zxc/ZxcDTO.java

79
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/DistributedLock.java

@ -0,0 +1,79 @@
package com.epmet.commons.tools.distributedlock;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.redis.RedisKeys;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Author zxc
* @DateTime 2020/10/28 9:05 上午
*/
@Component
public class DistributedLock {
@Autowired
private RedissonClient redissonClient;
/**
* @Description 抢锁🔒 每个锁持有十分钟
* @Param name
* @author zxc
* @date 2020/10/28 2:52 下午
*/
public RLock getLock(String name){
RLock lock = null;
if (StringUtils.isNotBlank(name)) {
lock = redissonClient.getLock(name);
// 持续时间为 -1 时,直到其明确释放锁才会释放【宕机就完蛋了】,
lock.lock(10,TimeUnit.MINUTES);
}
return lock;
}
/**
* @Description
* @Param name 锁名
* @Param leaseTime 持锁时间 单位min
* @Param waitTime 获取锁最长等待时间 单位min
* @author zxc
* @date 2020/10/29 9:04 上午
*/
public RLock getLock(String name,Long leaseTime,Long waitTime){
RLock lock = null;
if (StringUtils.isNotBlank(name) && leaseTime > 0 && waitTime > 0 ){
lock = redissonClient.getLock(name);
Boolean lockStatus;
try {
lockStatus = lock.tryLock(waitTime,leaseTime,TimeUnit.MINUTES);
if (!lockStatus){
throw new RenException("获取锁🔒失败了......");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return lock;
}
/**
* @Description 释放锁🔒
* @Param rLock
* @author zxc
* @date 2020/10/28 2:52 下午
*/
public void unLock(RLock rLock){
if (null != rLock){
if (rLock.isHeldByCurrentThread()){
rLock.unlock();
}
}
}
}

12
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/LockConstants.java

@ -0,0 +1,12 @@
package com.epmet.commons.tools.distributedlock;
/**
* @Author zxc
* @DateTime 2020/10/28 9:23 上午
*/
public interface LockConstants {
String TEST_LOCK_NAME = "testLock";
String STATS_LOCK_NAME = "stats";
}

23
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java

@ -3,7 +3,8 @@ package com.epmet.service.evaluationindex.indexcoll.impl;
import com.epmet.commons.dynamic.datasource.annotation.DataSource;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.distributedlock.DistributedLock;
import com.epmet.commons.tools.distributedlock.LockConstants;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.constant.DataSourceConstant;
import com.epmet.constant.OrgTypeConstant;
@ -29,7 +30,6 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -89,7 +89,7 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
@Autowired
private ScreenCustomerAgencyDao screenCustomerAgencyDao;
@Autowired
private RedissonClient redissonClient;
private DistributedLock distributedLock;
@Override
@Transactional(rollbackFor = Exception.class)
@ -216,15 +216,9 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
if (NumConstant.SIX != monthId.length()) {
throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId);
}
RLock lock = redissonClient.getLock(RedisKeys.getScreenIndexDataLockKey());
RLock lock = distributedLock.getLock(LockConstants.STATS_LOCK_NAME);
try {
AtomicInteger tryTimes = new AtomicInteger(0);
boolean lockFlag = false;
do {
lockFlag = lock.tryLock(1, 10, TimeUnit.MINUTES);
} while (!lockFlag && tryTimes.addAndGet(1) < 3);
if (!lockFlag) {
log.error("插入大屏指数方法获取锁失败");
if (null==lock) {
return;
}
allParentIds.cleanUp();
@ -250,13 +244,8 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
// 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly
this.insertIndexDataYear(monthId, customerId);
allParentIds.invalidateAll();
} catch (InterruptedException inter) {
log.error("插入计算指标数据异常", inter);
} finally {
//判断是否是当前线程 持有锁
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
distributedLock.unLock(lock);
}
}

48
epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java

@ -1,6 +1,10 @@
package com.epmet.stats.test;
import com.alibaba.fastjson.JSON;
import com.epmet.DataStatsApplication;
import com.epmet.commons.tools.distributedlock.DistributedLock;
import com.epmet.commons.tools.distributedlock.LockConstants;
import com.epmet.stats.test.zxc.ZxcDTO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -10,6 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
@ -22,8 +29,43 @@ public class RedissonTest {
@Autowired
private RedissonClient redissonClient;
@Autowired
private DistributedLock distributedLock;
@Test
public void lock(){
long start = System.currentTimeMillis();
RLock lock = distributedLock.getLock(LockConstants.TEST_LOCK_NAME);
if (null==lock){
System.out.println("shibai");
}
try {
List<ZxcDTO> list = new ArrayList<>();
for (int i = 0; i < 50000; i++) {
ZxcDTO zxcDTO = new ZxcDTO();
zxcDTO.setA(UUID.randomUUID().toString());
zxcDTO.setB(UUID.randomUUID().toString());
zxcDTO.setC(UUID.randomUUID().toString());
zxcDTO.setD(UUID.randomUUID().toString());
zxcDTO.setE(UUID.randomUUID().toString());
zxcDTO.setF(i);
zxcDTO.setG(i+1);
zxcDTO.setH(i+2);
list.add(zxcDTO);
}
// log.info(JSON.toJSONString(list));
}finally {
// distributedLock.unLock(lock);
}
long end = System.currentTimeMillis();
log.info((end-start)+"hs");
}
/*@Test
public void lockTest() {
//获取一个名为 lockName 的锁实例
RLock lock = redissonClient.getLock("lockName");
@ -38,10 +80,10 @@ public class RedissonTest {
//lock.lock();
//异步方式
/* RFuture<Void> future = lock.lockAsync();
*//* RFuture<Void> future = lock.lockAsync();
if (future.isSuccess()){
//todo something
}*/
}*//*
if (bs) {
// 业务代码
System.out.println("进入业务代码: " + 123);
@ -56,6 +98,6 @@ public class RedissonTest {
lock.unlock();
}
}
}
}*/
}

21
epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/zxc/ZxcDTO.java

@ -0,0 +1,21 @@
package com.epmet.stats.test.zxc;
import lombok.Data;
/**
* @Author zxc
* @DateTime 2020/10/28 3:15 下午
*/
@Data
public class ZxcDTO {
private String a;
private String b;
private String c;
private String d;
private String e;
private Integer f;
private Integer g;
private Integer h;
}
Loading…
Cancel
Save