From b415903e4c326798e1f4a81947e38b74aad4f3b3 Mon Sep 17 00:00:00 2001 From: TaXueWWL <1210812591@qq.com> Date: Wed, 11 Jul 2018 11:19:56 +0800 Subject: [PATCH 1/3] snowalker-dev --- redis-distributed-lock-starter-demo/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/redis-distributed-lock-starter-demo/readme.md b/redis-distributed-lock-starter-demo/readme.md index e69de29..5eef052 100644 --- a/redis-distributed-lock-starter-demo/readme.md +++ b/redis-distributed-lock-starter-demo/readme.md @@ -0,0 +1 @@ +# 开分支到dev \ No newline at end of file From 57c389aaece451ad91c676cb4471347c8300dbe4 Mon Sep 17 00:00:00 2001 From: TaXueWWL <1210812591@qq.com> Date: Wed, 11 Jul 2018 23:01:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?snowalker-=E5=A2=9E=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E9=85=8D=E7=BD=AE=E6=96=B9=E5=BC=8F=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++ .../executor/ExecutorRedissonNormal.java | 74 +++++++++---------- .../src/main/resources/application.properties | 4 +- .../conf/application.properties | 16 +++- redis-distributed-lock-starter/readme.md | 21 +++++- .../snowalker/lock/redisson/RedissonLock.java | 1 + .../lock/redisson/RedissonManager.java | 58 +++++++++++++-- .../config/RedissonAutoConfiguration.java | 5 +- .../redisson/config/RedissonProperties.java | 24 +++--- .../constant/RedisConnectionType.java | 34 +++++++++ 10 files changed, 179 insertions(+), 63 deletions(-) create mode 100644 redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/constant/RedisConnectionType.java diff --git a/README.md b/README.md index 64dafc5..367dacd 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,11 @@ redis-distributed-lock-demo-spring   redis-distributed-lock-core 调 ## 文档 ### springboot应用(直接编程方式) +0. 配置文件application.properties中引入 + + redisson.lock.server.address=127.0.0.1:6379 + redisson.lock.server.type=standalone + 1. 针对springboot应用只需要引入依赖 diff --git a/redis-distributed-lock-starter-demo/src/main/java/com/snowalker/executor/ExecutorRedissonNormal.java b/redis-distributed-lock-starter-demo/src/main/java/com/snowalker/executor/ExecutorRedissonNormal.java index 6912787..d21e63b 100644 --- a/redis-distributed-lock-starter-demo/src/main/java/com/snowalker/executor/ExecutorRedissonNormal.java +++ b/redis-distributed-lock-starter-demo/src/main/java/com/snowalker/executor/ExecutorRedissonNormal.java @@ -1,37 +1,37 @@ -package com.snowalker.executor; - -import com.snowalker.lock.redisson.RedissonLock; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -/** - * @author wuwl@19pay.com.cn - * @date 2018-7-9 - * @desc 纯java调用 - */ -@Component -public class ExecutorRedissonNormal { - - private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorRedissonNormal.class); - - @Autowired - RedissonLock redissonLock; - - @Scheduled(cron = "${redis.lock.cron}") - public void execute() throws InterruptedException { - if (redissonLock.lock("redisson", 10)) { - LOGGER.info("[ExecutorRedisson]--执行定时任务开始,休眠三秒"); - Thread.sleep(3000); - System.out.println("=======================业务逻辑============================="); - LOGGER.info("[ExecutorRedisson]--执行定时任务结束,休眠三秒"); - redissonLock.release("redisson"); - } else { - LOGGER.info("[ExecutorRedisson]获取锁失败"); - } - - } - -} +//package com.snowalker.executor; +// +//import com.snowalker.lock.redisson.RedissonLock; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.scheduling.annotation.Scheduled; +//import org.springframework.stereotype.Component; +// +///** +// * @author wuwl@19pay.com.cn +// * @date 2018-7-9 +// * @desc 纯java调用 +// */ +//@Component +//public class ExecutorRedissonNormal { +// +// private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorRedissonNormal.class); +// +// @Autowired +// RedissonLock redissonLock; +// +// @Scheduled(cron = "${redis.lock.cron}") +// public void execute() throws InterruptedException { +// if (redissonLock.lock("redisson", 10)) { +// LOGGER.info("[ExecutorRedisson]--执行定时任务开始,休眠三秒"); +// Thread.sleep(3000); +// System.out.println("=======================业务逻辑============================="); +// LOGGER.info("[ExecutorRedisson]--执行定时任务结束,休眠三秒"); +// redissonLock.release("redisson"); +// } else { +// LOGGER.info("[ExecutorRedisson]获取锁失败"); +// } +// +// } +// +//} diff --git a/redis-distributed-lock-starter-demo/src/main/resources/application.properties b/redis-distributed-lock-starter-demo/src/main/resources/application.properties index 90b2246..2a105f5 100644 --- a/redis-distributed-lock-starter-demo/src/main/resources/application.properties +++ b/redis-distributed-lock-starter-demo/src/main/resources/application.properties @@ -22,5 +22,5 @@ redis.lock.cron=0/5 * * * * ? # redissonֲʽ # ######################################################################### -redisson.server.host=127.0.0.1 -redisson.server.port=6379 \ No newline at end of file +redisson.lock.server.address=127.0.0.1:6379 +redisson.lock.server.type=standalone \ No newline at end of file diff --git a/redis-distributed-lock-starter/conf/application.properties b/redis-distributed-lock-starter/conf/application.properties index b04eeba..ef3b39f 100644 --- a/redis-distributed-lock-starter/conf/application.properties +++ b/redis-distributed-lock-starter/conf/application.properties @@ -1,2 +1,14 @@ -redisson.server.host=127.0.0.1 -redisson.server.ip=6379 \ No newline at end of file +######################################################################## +# +# redissonֲʽ-- +# +######################################################################### +redisson.lock.server.address=127.0.0.1:6379 +redisson.lock.server.type=standalone +######################################################################## +# +# redissonֲʽ--ڱ +# +######################################################################### +#redisson.server.address=127.0.0.1:6379 +#redisson.server.type=sentinel \ No newline at end of file diff --git a/redis-distributed-lock-starter/readme.md b/redis-distributed-lock-starter/readme.md index 733fd27..d1f9834 100644 --- a/redis-distributed-lock-starter/readme.md +++ b/redis-distributed-lock-starter/readme.md @@ -6,5 +6,22 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.snowalker.dem ## redis分布式锁starter--基于Redisson ### 配置文件 - redisson.server.host=127.0.0.1 - redisson.server.ip=6379 \ No newline at end of file + redisson.lock.server.address=127.0.0.1:6379 + redisson.lock.server.type=standalone +### 更新记录 +1. 改变配置方式,增加对不同Redis连接方式的支持 +
去除以下方法RedissonManager(String redisIp, String redisPort) + + public RedissonManager (String redisIp, String redisPort) { + try { + String redisAddr = new StringBuilder("redis://") + .append(redisIp).append(":").append(redisPort) + .toString(); + config.useSingleServer().setAddress(redisAddr); + redisson = (Redisson) Redisson.create(config); + LOGGER.info("初始化Redisson结束,redisAddress:" + redisAddr); + } catch (Exception e) { + LOGGER.error("Redisson init error", e); + e.printStackTrace(); + } + } diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonLock.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonLock.java index 6d33496..28c4e74 100644 --- a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonLock.java +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonLock.java @@ -39,6 +39,7 @@ public boolean lock(String lockName, long expireSeconds) { } catch (InterruptedException e) { LOGGER.error("获取Redisson分布式锁[异常],lockName=" + lockName, e); e.printStackTrace(); + return false; } return getLock; } diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonManager.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonManager.java index 8b8441f..fac1078 100644 --- a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonManager.java +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/RedissonManager.java @@ -1,5 +1,6 @@ package com.snowalker.lock.redisson; +import com.snowalker.lock.redisson.constant.RedisConnectionType; import org.redisson.Redisson; import org.redisson.config.Config; import org.slf4j.Logger; @@ -18,14 +19,12 @@ public class RedissonManager { private Redisson redisson = null; - public RedissonManager (String redisIp, String redisPort) { + public RedissonManager() {} + + public RedissonManager (String connectionType, String address) { try { - String redisAddr = new StringBuilder("redis://") - .append(redisIp).append(":").append(redisPort) - .toString(); - config.useSingleServer().setAddress(redisAddr); + config = RedissonConfigFactory.getInstance().createConfig(connectionType, address); redisson = (Redisson) Redisson.create(config); - LOGGER.info("初始化Redisson结束,redisAddress:" + redisAddr); } catch (Exception e) { LOGGER.error("Redisson init error", e); e.printStackTrace(); @@ -36,6 +35,51 @@ public Redisson getRedisson() { return redisson; } + /** + * Redisson连接方式配置工厂 + */ + static class RedissonConfigFactory { + + private RedissonConfigFactory() {} + + private static volatile RedissonConfigFactory factory = null; + + private static final String REDIS_CONNECTION_PREFIX = "redis://"; + + public static RedissonConfigFactory getInstance() { + if (factory == null) { + synchronized (RedissonConfigFactory.class) { + factory = new RedissonConfigFactory(); + } + } + return factory; + } + + private Config config = new Config(); + + /** + * 根据连接类型及连接地址参数获取对应连接方式的配置 + * @param connectionType + * @param address + * @return Config + */ + Config createConfig(String connectionType, String address) { + if (connectionType.equals(RedisConnectionType.STANDALONE.getConnection_type())) { + try { + String redisAddr = REDIS_CONNECTION_PREFIX + address; + config.useSingleServer().setAddress(redisAddr); + LOGGER.info("初始化standalone方式Config,redisAddress:" + redisAddr); + } catch (Exception e) { + LOGGER.error("standalone Redisson init error", e); + e.printStackTrace(); + } + return config; + } else if (connectionType.equals(RedisConnectionType.SENTINEL.getConnection_type())) { + return null; + } + throw new RuntimeException("创建Redisson连接Config失败!当前连接方式:" + connectionType); + } + } public static void main(String[] args) { Config config = new Config(); @@ -43,3 +87,5 @@ public static void main(String[] args) { Redisson redisson = (Redisson) Redisson.create(config); } } + + diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonAutoConfiguration.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonAutoConfiguration.java index 4e67b32..857c920 100644 --- a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonAutoConfiguration.java +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonAutoConfiguration.java @@ -39,8 +39,9 @@ public RedissonLock redissonLock(RedissonManager redissonManager) { @Order(value = 1) public RedissonManager redissonManager(RedissonProperties redissonProperties) { RedissonManager redissonManager = - new RedissonManager(redissonProperties.getHost(), redissonProperties.getPort()); - LOGGER.info("[RedissonManager]组装完毕"); + new RedissonManager(redissonProperties.getType(), redissonProperties.getAddress()); + LOGGER.info("[RedissonManager]组装完毕,当前连接方式:" + redissonProperties.getType() + + ",连接地址:" + redissonProperties.getAddress()); return redissonManager; } } diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java index 2f1cbfa..2c9e2b0 100644 --- a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java @@ -10,24 +10,24 @@ @ConfigurationProperties(prefix = "redisson.server") public class RedissonProperties { - /**单节点redis主机*/ - private String host; - /**单节点redis端口*/ - private String port; + /**redis主机地址,ip:port,有多个用半角逗号分隔*/ + private String address; + /**连接类型,支持standalone-单机节点,sentinel-哨兵,cluster-集群,masterslave-主从*/ + private String type; - public String getHost() { - return host; + public String getAddress() { + return address; } - public void setHost(String host) { - this.host = host; + public void setAddress(String address) { + this.address = address; } - public String getPort() { - return port; + public String getType() { + return type; } - public void setPort(String port) { - this.port = port; + public void setType(String type) { + this.type = type; } } diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/constant/RedisConnectionType.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/constant/RedisConnectionType.java new file mode 100644 index 0000000..4cde06b --- /dev/null +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/constant/RedisConnectionType.java @@ -0,0 +1,34 @@ +package com.snowalker.lock.redisson.constant; + +/** + * @author snowalker + * @date 2018/7/11 + * @desc Redis连接方式 + * 包含:standalone-单节点部署方式 + * sentinel-哨兵部署方式 + * cluster-集群方式 + * masterslave-主从部署方式 + */ +public enum RedisConnectionType { + + STANDALONE("standalone", "单节点部署方式"), + SENTINEL("sentinel", "哨兵部署方式"), + CLUSTER("cluster", "集群方式"), + MASTERSLAVE("masterslave", "主从部署方式"); + + private final String connection_type; + private final String connection_desc; + + private RedisConnectionType(String connection_type, String connection_desc) { + this.connection_type = connection_type; + this.connection_desc = connection_desc; + } + + public String getConnection_type() { + return connection_type; + } + + public String getConnection_desc() { + return connection_desc; + } +} From 2d55692d738615e1fe2726a4e7f84998f1df0748 Mon Sep 17 00:00:00 2001 From: TaXueWWL <1210812591@qq.com> Date: Wed, 11 Jul 2018 23:04:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?snowalker-=E6=9B=B4=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/snowalker/lock/redisson/config/RedissonProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java index 2c9e2b0..325ad8e 100644 --- a/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java +++ b/redis-distributed-lock-starter/src/main/java/com/snowalker/lock/redisson/config/RedissonProperties.java @@ -7,7 +7,7 @@ * @date 2018/7/10 * @desc Redisson配置映射类 */ -@ConfigurationProperties(prefix = "redisson.server") +@ConfigurationProperties(prefix = "redisson.lock.server") public class RedissonProperties { /**redis主机地址,ip:port,有多个用半角逗号分隔*/