From 761c1af866446ed45fec300e10acbae56308d945 Mon Sep 17 00:00:00 2001 From: TaXueWWL <1210812591@qq.com> Date: Thu, 12 Jul 2018 00:10:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?snowalker--=E5=9F=BA=E6=9C=AC=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=9B=9B=E7=A7=8D=E8=BF=9E=E6=8E=A5=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../conf/application.properties | 22 +++- redis-distributed-lock-starter/pom.xml | 5 + .../lock/redisson/RedissonManager.java | 116 +++++++++++++++--- 3 files changed, 126 insertions(+), 17 deletions(-) diff --git a/redis-distributed-lock-starter/conf/application.properties b/redis-distributed-lock-starter/conf/application.properties index ef3b39f..14de0e4 100644 --- a/redis-distributed-lock-starter/conf/application.properties +++ b/redis-distributed-lock-starter/conf/application.properties @@ -8,7 +8,25 @@ redisson.lock.server.type=standalone ######################################################################## # # redisson分布式锁配置--哨兵 +# 格式为: sentinel.conf配置里的sentinel别名,sentinel1节点的服务IP和端口,sentinel2节点的服务IP和端口,sentinel3节点的服务IP和端口 +# 比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name # ######################################################################### -#redisson.server.address=127.0.0.1:6379 -#redisson.server.type=sentinel \ No newline at end of file +#redisson.server.address=my-sentinel-name,127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399 +#redisson.server.type=sentinel +######################################################################## +# +# redisson分布式锁配置--集群方式 +# cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用) +# 格式为: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 +######################################################################### +#redisson.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 +#redisson.server.type=cluster +######################################################################## +# +# redisson分布式锁配置--主从 +# 主节点,子节点,子节点 +# 格式为: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 +######################################################################### +#redisson.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 +#redisson.server.type=masterslave \ No newline at end of file diff --git a/redis-distributed-lock-starter/pom.xml b/redis-distributed-lock-starter/pom.xml index 6f92a3b..0f78a1a 100644 --- a/redis-distributed-lock-starter/pom.xml +++ b/redis-distributed-lock-starter/pom.xml @@ -30,6 +30,11 @@ + + com.google.guava + guava + ${google-guava} + org.aspectj 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 fac1078..6cc0e3c 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,11 +1,15 @@ package com.snowalker.lock.redisson; +import com.google.common.base.Preconditions; import com.snowalker.lock.redisson.constant.RedisConnectionType; import org.redisson.Redisson; import org.redisson.config.Config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; + /** * @author snowalker * @date 2018/7/10 @@ -64,28 +68,110 @@ public static RedissonConfigFactory getInstance() { * @return Config */ Config createConfig(String connectionType, String address) { + Preconditions.checkNotNull(connectionType); + Preconditions.checkNotNull(address); if (connectionType.equals(RedisConnectionType.STANDALONE.getConnection_type())) { - try { - String redisAddr = REDIS_CONNECTION_PREFIX + address; - config.useSingleServer().setAddress(redisAddr); - LOGGER.info("鍒濆鍖杝tandalone鏂瑰紡Config,redisAddress:" + redisAddr); - } catch (Exception e) { - LOGGER.error("standalone Redisson init error", e); - e.printStackTrace(); - } - return config; + return createStandaloneConfig(address); } else if (connectionType.equals(RedisConnectionType.SENTINEL.getConnection_type())) { - return null; + return createSentinelConfig(address); + } else if (connectionType.equals(RedisConnectionType.CLUSTER.getConnection_type())) { + return createClusterConfig(address); + } else if (connectionType.equals(RedisConnectionType.MASTERSLAVE.getConnection_type())) { + return createMasterSlaveConfig(address); } throw new RuntimeException("鍒涘缓Redisson杩炴帴Config澶辫触锛佸綋鍓嶈繛鎺ユ柟寮:" + connectionType); } - } - public static void main(String[] args) { - Config config = new Config(); - config.useSingleServer().setAddress("redis://127.0.0.1:6379"); - Redisson redisson = (Redisson) Redisson.create(config); + /** + * 涓讳粠鏂瑰紡閰嶇疆 + * @param address + * @return + */ + private Config createMasterSlaveConfig(String address) { + try { + String[] addrTokens = address.split(","); + String masterNodeAddr = addrTokens[0]; + /**璁剧疆涓昏妭鐐筰p*/ + config.useMasterSlaveServers().setMasterAddress(masterNodeAddr); + /**璁剧疆浠庤妭鐐癸紝绉婚櫎绗竴涓妭鐐癸紝榛樿绗竴涓负涓昏妭鐐*/ + List slaveList = new ArrayList<>(); + for (String addrToken : addrTokens) { + slaveList.add(REDIS_CONNECTION_PREFIX + addrToken); + } + slaveList.remove(0); + + config.useMasterSlaveServers().addSlaveAddress((String[]) slaveList.toArray()); + LOGGER.info("鍒濆鍖朳MASTERSLAVE]鏂瑰紡Config,redisAddress:" + address); + } catch (Exception e) { + LOGGER.error("MASTERSLAVE Redisson init error", e); + e.printStackTrace(); + } + return config; + } + + /** + * 闆嗙兢鏂瑰紡閰嶇疆 + * @param address + * @return + */ + private Config createClusterConfig(String address) { + try { + String[] addrTokens = address.split(","); + /**璁剧疆cluster鑺傜偣鐨勬湇鍔P鍜岀鍙*/ + for (int i = 0; i < addrTokens.length; i++) { + config.useClusterServers().addNodeAddress(REDIS_CONNECTION_PREFIX + addrTokens[i]); + } + LOGGER.info("鍒濆鍖朳cluster]鏂瑰紡Config,redisAddress:" + address); + } catch (Exception e) { + LOGGER.error("cluster Redisson init error", e); + e.printStackTrace(); + } + return config; + } + + /** + * 鍝ㄥ叺鏂瑰紡閰嶇疆 + * @param address + * @return + */ + private Config createSentinelConfig(String address) { + try { + String[] addrTokens = address.split(","); + String sentinelAliasName = addrTokens[0]; + /**璁剧疆redis閰嶇疆鏂囦欢sentinel.conf閰嶇疆鐨剆entinel鍒悕*/ + config.useSentinelServers() + .setMasterName(sentinelAliasName); + /**璁剧疆sentinel鑺傜偣鐨勬湇鍔P鍜岀鍙*/ + for (int i = 1; i < addrTokens.length; i++) { + config.useSentinelServers().addSentinelAddress(REDIS_CONNECTION_PREFIX + addrTokens[i]); + } + LOGGER.info("鍒濆鍖朳sentinel]鏂瑰紡Config,redisAddress:" + address); + } catch (Exception e) { + LOGGER.error("sentinel Redisson init error", e); + e.printStackTrace(); + } + return config; + } + + /** + * 鍗曟満鏂瑰紡閰嶇疆 + * @param address + * @return + */ + private Config createStandaloneConfig(String address) { + try { + String redisAddr = REDIS_CONNECTION_PREFIX + address; + config.useSingleServer().setAddress(redisAddr); + LOGGER.info("鍒濆鍖朳standalone]鏂瑰紡Config,redisAddress:" + address); + } catch (Exception e) { + LOGGER.error("standalone Redisson init error", e); + e.printStackTrace(); + } + return config; + } + } + } From 2026cf75a30e1861064cbf0ac14522ef5c58cdec Mon Sep 17 00:00:00 2001 From: TaXueWWL <1210812591@qq.com> Date: Thu, 12 Jul 2018 00:16:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?snowalker--readme=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 367dacd..29de6fd 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,34 @@ redis-distributed-lock-demo-spring   redis-distributed-lock-core 璋 2018-07-11 09:48:13.627 |-INFO [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [46] -| 閲婃斁Redis鍒嗗竷寮忛攣[鎴愬姛]锛岃В閿佸畬鎴愶紝缁撴潫涓氬姟閫昏緫... 2018-07-11 09:48:13.628 |-INFO [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [50] -| [缁撴潫]鎵цRedisLock鐜粫閫氱煡 +## application.properties閰嶇疆鏂瑰紡(yaml鍚岀悊) +### redisson鍒嗗竷寮忛攣閰嶇疆--鍗曟満 + + redisson.lock.server.address=127.0.0.1:6379 + redisson.lock.server.type=standalone + +### redisson鍒嗗竷寮忛攣閰嶇疆--鍝ㄥ叺 +**redisson.lock.server.address** 鏍煎紡涓: sentinel.conf閰嶇疆閲岀殑sentinel鍒悕,sentinel1鑺傜偣鐨勬湇鍔P鍜岀鍙o紝sentinel2鑺傜偣鐨勬湇鍔P鍜岀鍙o紝sentinel3鑺傜偣鐨勬湇鍔P鍜岀鍙 +
姣斿sentinel.conf閲岄厤缃负sentinel monitor my-sentinel-name 127.0.0.1 6379 2,閭d箞杩欓噷灏遍厤缃甿y-sentinel-name + + redisson.lock.server.address=my-sentinel-name,127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399 + redisson.lock.server.type=sentinel + +### redisson鍒嗗竷寮忛攣閰嶇疆--闆嗙兢鏂瑰紡 +cluster鏂瑰紡鑷冲皯6涓妭鐐(3涓3浠庯紝3涓诲仛sharding锛3浠庣敤鏉ヤ繚璇佷富瀹曟満鍚庡彲浠ラ珮鍙敤) +
鍦板潃鏍煎紡涓: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 + + redisson.lock.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 + redisson.lock.server.type=cluster + +### redisson鍒嗗竷寮忛攣閰嶇疆--涓讳粠 +鍦板潃鏍煎紡涓**涓昏妭鐐,瀛愯妭鐐,瀛愯妭鐐** +
姣斿:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 +
浠h〃涓昏妭鐐:127.0.0.1:6379锛屼粠鑺傜偣127.0.0.1:6380锛127.0.0.1:6381 + + redisson.lock.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 + redisson.lock.server.type=masterslave + ## 鍙傝冭祫鏂 [鍒嗗竷寮忛攣鐨勫绉嶅疄鐜癩(http://wuwenliang.net/2018/07/08/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E7%9A%84%E5%A4%9A%E7%A7%8D%E5%AE%9E%E7%8E%B0/) \ No newline at end of file