Redisson中分布式锁的应用

Redisson中分布式锁的应用,第1张

Redisson中分布式锁的应用

首先我们需要在application.properties文件中配置我们redis集群节点,以及密码.

首先不要忘记在pom文件中引入相应的依赖
     
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
    
        
            org.redisson
            redisson-spring-boot-starter
            3.13.3
        
1. 第一步 application.properties中配置代码如下:
#Redis
spring.redis.cluster.nodes=192.168.165.251:6379,192.168.165.251:6380,192.168.165.251:6381,192.168.165.251:6382,192.168.165.251:6383,192.168.165.251:6384
spring.redis.password=test
2.第二步

创建RedissonConfiguration类,这个类是Redisson的配置类,提供RedissonClient实例.

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;



@Configuration
public class RedissonConfiguration {
    @Value("#{'${spring.redis.cluster.nodes}'.split(',')}")
    private List nodes;

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

    @Bean
    public RedissonClient getRedissonClient() {
        Config config = new Config();
        config.useClusterServers().setPassword(password);
        //集群模式,集群节点的地址须使用“redis://”前缀,否则将会报错。
        //此例集群为3节点,各节点1主1从
        for (String node : nodes) {
            String redisNode = "redis://" + node;
            config.useClusterServers().addNodeAddress(redisNode);
        }
        return Redisson.create(config);
    }
}

3.举个运用的栗子
 @Override
    public ResponseDto /confirm/iReceive(/confirm/iReceiveDto /confirm/iReceiveDto) {

        ResponseDto responseDto = new ResponseDto();

        CommonClientResponse resp = new CommonClientResponse<>();

        String membershipId = /confirm/iReceiveDto.getMembershipId();
        
        if (ObjectUtils.isEmpty(/confirm/iReceiveDto.getAddressId())) {
            throw MyException.build(ErrorConstant.NO_ADDRESS_EXCEPTION);
        }
        if (ObjectUtils.isEmpty(/confirm/iReceiveDto.getType())) {
            throw MyException.build(ErrorConstant.NO_TYPE_ERROR);
        }

        //分布式锁  从RedissonClient取得RLock实例 锁住会员id,防止用户多设备端薅羊毛
        //尝试取锁,有效期为1min,到期后自动释放。如果取得锁继续执行。取锁失败则自旋。
        RLock rLock = redissonClient.getLock(RECEIVE_GIFT + membershipId);

        try {

            boolean isLock = rLock.tryLock(2, 60, TimeUnit.SECONDS);
            if (isLock) {
                /confirm/iReceiveGift(/confirm/iReceiveDto, responseDto, resp, membershipId);
            }

        } catch (Exception e) {
            responseDto.setCode("20000");
            responseDto.setMessage(e.getMessage());
            log.error("领取失败 ===> {}", e.getMessage());
        } finally {

            // 释放锁
            rLock.unlock();
        }
        return responseDto;
    }

好啦,这就是分布式锁的一个简单运用栗子,记录下来,希望能帮助到你.

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5687538.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存