配置地址:
1
2
3
4
|
redisson: # Redis服务地址 如果集群使用","进行分割 server-address: redis: // ${spring.redis.host}:${spring.redis.port} database: ${spring.redis.database} |
创建配置类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@ConfigurationProperties(prefix = "redisson" ) @Configuration public class RedissonConfig { /** Redis服务地址 如果集群使用 "," 进行分割 */ private String serverAddress; private Integer database; public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddresss) { this.serverAddress = serverAddresss; } public Integer getDatabase() { return database; } public void setDatabase(Integer database) { this.database = database; } } |
注册redission Bean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * RedissonClient * @ return */ @Bean public RedissonClient redissonClient(){ String splitChar = "," ; String serverAddress = redissonConfig.getServerAddress(); String[] serverAddressArr = serverAddress. split (splitChar); Config config = new Config(); if (serverAddressArr.length == 1) { // 单例redis config.useSingleServer() .setAddress(redissonConfig.getServerAddress()) .setDatabase(redissonConfig.getDatabase()); } else { // 集群redis config.useClusterServers().addNodeAddress(serverAddressArr); } return Redisson.create(config); } |
防止重复初始化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
** * 初始化告警统计记录 保证告警统计不会被重复初始化 * * @param areaId 部门 id * @throws BizException */ public void initWarningStatisticsSafe(String areaId) throws BizException { // 创建锁名称 String lockName = generateInitWarningStatisticsLockName(areaId); RLock lock = redisClient.getLock(lockName); try { // 尝试获取锁 (最多尝试10秒,获取到后15秒后释放锁) boolean isAcquireLock = lock.tryLock(10, 15, TimeUnit.SECONDS); if (!isAcquireLock) { logger.error( "初始化消息统计 --- 获取锁失败 lockName = " + lockName); throw new BizException( "系统异常" ); } else { try { // 查询【告警统计】 (能够查询到最新的数据,因为默认隔离级别是 read committed List<WarningStatisticsPO> warningStatisticsPOS = warningStatisticsDAO.selectByArea(areaId); if (CollectionUtils.isEmpty(warningStatisticsPOS)) { // 如果部门不存在告警统计,才进行初始化 initWarningStatistics()会开启一个新事务 warningStatisticsService.initWarningStatistics(areaId); } } finally { // 释放锁 (在事务提交后才释放锁。保证其它事务在获取锁后能查询到数据,不会再进行初始化。) lock.unlock(); } } } catch (InterruptedException e) { e.printStackTrace(); } } |
到此这篇关于redission分布式锁防止重复初始化问题的文章就介绍到这了,更多相关redission分布式锁内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_41434746/article/details/109841021