利用Redisson实现分布式锁,以及分布式锁的常用注意事项

        废话少说,直接上代码

        

 <dependency>
     <groupId>org.redisson</groupId>
     <artifactId>redisson-spring-boot-starter</artifactId>
     <version>3.15.6</version>
</dependency>
package com.yanxi.order.redissonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis 配置类
 *
 * @author Shaoyu Liu
 * @date 2021/5/28 15:50
 **/
@Configuration
public class MyRedissonConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> myRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }


}
package com.yanxi.order.redissonConfig;

import com.yanxi.common.core.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRedissonTemplate {
    @Value("${spring.redis.host}")
    private String redisAddress;
    @Value("${spring.redis.port}")
    private Integer port;

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


    @Bean
    public Redisson redisson() {
        Config config = new Config();
          //配置地址、数据库
        if(!StringUtils.isEmpty(password)){
            config.useSingleServer()
                    .setAddress("redis://"+redisAddress+":"+port)
                    .setPassword(password)
                    .setDatabase(0);
        }else {
            config.useSingleServer()
                    .setAddress("redis://"+redisAddress+":"+port)
                    .setDatabase(0);
        }

        return (Redisson) Redisson.create(config);
    }
}

yml:

        

 实战:

        

 

 RLock lock = redissonTemplate.redisson().getLock(callBack.getTransaction_id());
        lock.lock();
        try {
            //业务逻辑
        }catch (Exception e){
            log.error(e.getMessage());
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值