Skip to content

Commit

Permalink
Merge pull request #1 from TaXueWWL/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TaXueWWL authored Jul 11, 2018
2 parents ac8d8ee + 2d55692 commit 53485ff
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 64 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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应用只需要引入依赖

<!--分布式锁redisson版本-->
Expand Down
1 change: 1 addition & 0 deletions redis-distributed-lock-starter-demo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 开分支到dev
Original file line number Diff line number Diff line change
@@ -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]获取锁失败");
// }
//
// }
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ redis.lock.cron=0/5 * * * * ?
# redisson分布式锁配置
#
#########################################################################
redisson.server.host=127.0.0.1
redisson.server.port=6379
redisson.lock.server.address=127.0.0.1:6379
redisson.lock.server.type=standalone
16 changes: 14 additions & 2 deletions redis-distributed-lock-starter/conf/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
redisson.server.host=127.0.0.1
redisson.server.ip=6379
########################################################################
#
# 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
21 changes: 19 additions & 2 deletions redis-distributed-lock-starter/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
redisson.lock.server.address=127.0.0.1:6379
redisson.lock.server.type=standalone
### 更新记录
1. 改变配置方式,增加对不同Redis连接方式的支持
<br/>去除以下方法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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -36,10 +35,57 @@ 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();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
Redisson redisson = (Redisson) Redisson.create(config);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
* @date 2018/7/10
* @desc Redisson配置映射类
*/
@ConfigurationProperties(prefix = "redisson.server")
@ConfigurationProperties(prefix = "redisson.lock.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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 53485ff

Please sign in to comment.