Skip to content

Commit

Permalink
Redis分布式锁-RedisLockHelper测试
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed May 12, 2022
1 parent 3ea26f6 commit 14440ea
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
9 changes: 8 additions & 1 deletion SpringBoot/DelayTask/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.1</version>
<version>3.13.6</version>
</dependency>

<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.2</version>
</dependency>

<!--<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,20 @@ public void putName(@RequestParam("name") String name) {
private RedisLockHelper redisLockHelper;

@GetMapping("test")
public void test() {
public void test() throws Exception {

redisLockHelper.tryLock("", () -> {
// 业务逻辑
FileInputStream fileInputStream = null;
fileInputStream.read();
});

String redisLockKey = "";
redisLockHelper.lock(redisLockKey, () -> {
// 业务逻辑
// ...
});

try {
redisLockHelper.tryLock("order:pay:" + 1, () -> {
// 业务逻辑
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public RedissonClient redissonClient() {
redissonClient = Redisson.create(config);
return redissonClient;
} catch (Exception e) {
logger.error("RedissonClient init redis url:[{}], Exception:", url, e);
logger.error("RedissonClient Init Url:[{}], Exception: {}", url, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.example.redisson;

import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* RedissonConfig配置
*
* @author wliduo[[email protected]]
* @date 2020/8/14 16:16
*/
// @Configuration
public class RedissonConfigNew {

private static final Logger logger = LoggerFactory.getLogger(RedissonConfig.class);

@Value("${spring.redis.hostName}")
private String host;

@Value("${spring.redis.port}")
private int port;

@Value("${spring.redis.password}")
private String password;

@Value("${spring.redis.database}")
private int database;

/**
* RedissonClient单机配置
*
* @param
* @return org.redisson.api.RedissonClient
* @throws
* @author wliduo[[email protected]]
* @date 2020/8/14 16:25
*/
@Bean
@ConditionalOnProperty(name = {"spring.redis.cluster"}, havingValue = "false")
public RedissonClient redissonClientSingle() {
RedissonClient redissonClient = null;
Config config = new Config();
String url = "redis://" + host + ":" + port;
// 单节点配置
config.useSingleServer().setAddress(url).setDatabase(database);
if (StrUtil.isNotBlank(password)) {
config.useSingleServer().setPassword(password);
}
try {
redissonClient = Redisson.create(config);
return redissonClient;
} catch (Exception e) {
logger.error("RedissonClient Init Url:[{}], Exception: {}", url, e);
return null;
}
}

@Value("${spring.redis.cluster.nodes}")
private String cluster;

/**
* RedissonClient集群配置
*
* @param
* @return org.redisson.api.RedissonClient
* @throws
* @author wliduo[[email protected]]
* @date 2020/8/14 16:25
*/
@Bean
@ConditionalOnProperty(name = {"spring.redis.cluster"}, havingValue = "true")
public RedissonClient redissonClientCluster() {
RedissonClient redissonClient = null;
Config config = new Config();
// 集群状态扫描间隔时间,单位是毫秒
config.useClusterServers().setScanInterval(2000);
if (StrUtil.isNotBlank(password)) {
config.useClusterServers().setPassword(password);
}
String[] serverArray = cluster.split(",");
for (String ipPort : serverArray) {
String[] ipAndPort = ipPort.split(":");
String url = "redis://" + ipAndPort[0].trim() + ":" + ipAndPort[1].trim();
config.useClusterServers().addNodeAddress(url);
}
try {
redissonClient = Redisson.create(config);
return redissonClient;
} catch (Exception e) {
logger.error("RedissonClient init redis url:[{}], Exception:", cluster, e);
return null;
}
}
}

0 comments on commit 14440ea

Please sign in to comment.