Skip to content

Commit

Permalink
Merge pull request #2 from TaXueWWL/dev
Browse files Browse the repository at this point in the history
版本1.1.0
  • Loading branch information
TaXueWWL authored Jul 11, 2018
2 parents 53485ff + 2026cf7 commit 97239b9
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 17 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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节点的服务IP和端口,sentinel2节点的服务IP和端口,sentinel3节点的服务IP和端口
<br/>比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-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从用来保证主宕机后可以高可用)
<br/>地址格式为: 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分布式锁配置--主从
地址格式为**主节点,子节点,子节点**
<br/>比如:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
<br/>代表主节点: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/)
22 changes: 20 additions & 2 deletions redis-distributed-lock-starter/conf/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
#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
5 changes: 5 additions & 0 deletions redis-distributed-lock-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${google-guava}</version>
</dependency>
<!-- aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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("初始化standalone方式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];
/**设置主节点ip*/
config.useMasterSlaveServers().setMasterAddress(masterNodeAddr);
/**设置从节点,移除第一个节点,默认第一个为主节点*/
List<String> 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节点的服务IP和端口*/
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配置的sentinel别名*/
config.useSentinelServers()
.setMasterName(sentinelAliasName);
/**设置sentinel节点的服务IP和端口*/
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;
}

}

}


0 comments on commit 97239b9

Please sign in to comment.