diff --git a/center.manager/src/main/resources/app-context.xml b/center.manager/src/main/resources/app-context.xml index dcb0a90..778ff2d 100644 --- a/center.manager/src/main/resources/app-context.xml +++ b/center.manager/src/main/resources/app-context.xml @@ -16,7 +16,8 @@ - + + diff --git a/center.manager/src/main/resources/dev/redis.properties b/center.manager/src/main/resources/dev/redis.properties new file mode 100644 index 0000000..b7843bc --- /dev/null +++ b/center.manager/src/main/resources/dev/redis.properties @@ -0,0 +1,33 @@ +redis.host1=127.0.0.1 +redis.port1=6379 +redis.password1= +#redis.host1=127.0.0.1 +#redis.port1=6379 +#redis.password1=Lyms123456 + + +#redis.password1= +#redis.host2=192.168.56.254 +#redis.port2=6382 +#redis.password2= +#redis.host3=192.168.56.254 +#redis.port3=6383 +#redis.password2= +#redis.host4=192.168.56.254 +#redis.port4=6384 +#redis.password4= +#redis.host5=192.168.56.254 +#redis.port5=6385 +#redis.password5= +#redis.host6=192.168.56.254 +#redis.port6=6386 +#redis.password6= + + +redis.minIdle=1 +redis.maxIdle=300 +redis.maxActive=600 +redis.maxWait=1000 +redis.testOnBorrow=true +redis.HttpSession.redisNamespace=hospital.mac +spring.redis.cluster.max-redirects= 3 diff --git a/center.manager/src/main/resources/xml/app-redis.xml b/center.manager/src/main/resources/xml/app-redis.xml new file mode 100644 index 0000000..b6faac6 --- /dev/null +++ b/center.manager/src/main/resources/xml/app-redis.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/center.manager/src/test/java/center/manager/test/user/JeditLockTest.java b/center.manager/src/test/java/center/manager/test/user/JeditLockTest.java new file mode 100644 index 0000000..2c87c71 --- /dev/null +++ b/center.manager/src/test/java/center/manager/test/user/JeditLockTest.java @@ -0,0 +1,63 @@ +package center.manager.test.user; + +import java.io.Serializable; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; + +import com.lyms.spring.redis.operation.RedisLock; + +/** + *
  • @ClassName: redis 分布式锁测试 + *
  • @Description: TODO(类描述) + *
  • @author maliang + *
  • @date 2017年3月10日 + *
  • + */ +public class JeditLockTest extends BaseTest { + + private RedisLock lock = null; + + @Autowired + private RedisTemplate redisTemplate; + + @Before + public void bef() { + lock = new RedisLock(redisTemplate); + } + + @Test + public void tryLock() { + for (int i = 0; i < 500; i++) { + new Thread(new Runnable() { + @Override + public void run() { + try { + if (lock.acquireLock("ack", 50000L)) { + System.out.println("thread: " + Thread.currentThread().getName() + " : 执行任务"); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + lock.releaseLock(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }).start(); + } + while (true) { + + } + } + +} diff --git a/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java b/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java index e6e419a..56f9ab0 100644 --- a/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java +++ b/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java @@ -1,12 +1,12 @@ package com.lyms.spring.redis.operation; +import java.util.concurrent.TimeUnit; + import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; -import com.lyms.spring.SpringContextHolder; - /** *
  • ClassName:RedisLock
    *
  • @Description: 锁 @@ -16,15 +16,41 @@ import com.lyms.spring.SpringContextHolder; @SuppressWarnings({ "rawtypes", "unchecked" }) public class RedisLock { - private RedisTemplate redisTemplate = (RedisTemplate) SpringContextHolder - .getBean(RedisTemplate.class); + private RedisTemplate redisTemplate = null;// (RedisTemplate) + // SpringContextHolder.getBean(RedisTemplate.class); private static final String PREFIX_KEY = "my_lock:"; + private String lockNode = null; + public RedisLock(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } + /** + *
  • @Description:获取锁 + *
  • @param lockName 锁地址 + *
  • @param waitNano 等待锁时间 + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月10日 + *
  • 修改人: + *
  • 修改时间: + */ + public boolean acquireLock(String lockName, Long waitmill) throws Exception { + Long nano = System.nanoTime(); + while (System.nanoTime() - nano < TimeUnit.MILLISECONDS.toNanos(waitmill)) { + boolean tag = acquireLock(lockName); + // 获取到锁资源 + if (tag) + return true; + // 暂停资源的抢占 + System.out.println(Thread.currentThread().getName() + "等待锁..."); + Thread.sleep(100); + } + return false; + } + // 获取锁 public boolean acquireLock(String lockName) throws Exception { String redisKey = PREFIX_KEY + lockName; @@ -33,6 +59,7 @@ public class RedisLock { long redisValue = System.currentTimeMillis() + timeout + 1; // 通过SETNX试图获取一个lock if (setNX(redisKey, String.valueOf(redisValue), expire)) {// SETNX成功,则成功获取一个锁 + this.lockNode = lockName; return true; } else {// SETNX失败,说明锁仍然被其他对象保持,检查其是否已经超时 long oldValue = Long.valueOf(get(redisKey).toString()); @@ -41,9 +68,10 @@ public class RedisLock { if (oldValue < System.currentTimeMillis()) { String getValue = getAndSet(redisKey, String.valueOf(redisValue)); // 获取锁成功 - if (getValue.equals(oldValue)) + if (getValue.equals(oldValue)) { + this.lockNode = lockName; return true; - else {// 已被其他进程捷足先登了 + } else {// 已被其他进程捷足先登了 return false; } } else {// 未超时,则直接返回失败 @@ -53,8 +81,8 @@ public class RedisLock { } // 释放锁 - public void releaseLock(String lockName) throws Exception { - String redisKey = PREFIX_KEY + lockName; + public void releaseLock() throws Exception { + String redisKey = PREFIX_KEY + this.lockNode; redisTemplate.delete(redisKey); } diff --git a/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java b/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java index edae07e..a8cbba6 100644 --- a/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java +++ b/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java @@ -12,7 +12,6 @@ import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.util.StringUtils; import org.springframework.web.bind.ServletRequestDataBinder; @@ -33,10 +32,10 @@ public class BaseController { protected Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired + // @Autowired protected HttpServletRequest request; - @Autowired + // @Autowired protected HttpServletResponse response; @InitBinder