java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Redis分布式锁

基于Redis分布式锁Redisson及SpringBoot集成Redisson

作者:_灯火阑珊处

这篇文章主要介绍了基于Redis分布式锁Redisson及SpringBoot集成Redisson,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小小伙伴可以参考一下

- 分布式锁需要具备的条件和刚需

- Redisson使用

引入 redisson 依赖:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.16.2</version>
</dependency>

添加 redisson 配置:

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        // 设置key序列号方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value的序列化方式json
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    @Bean
    public Redisson redisson() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.10.233:6379").setDatabase(0);
        return (Redisson) Redisson.create(config);
    }

}

接口测试:

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private Redisson redisson;

    private static final String KEY = "spike";

    @GetMapping("/buy")
    public String bugGood() {
        RLock redissonLock = redisson.getLock(KEY);
        redissonLock.lock();
        try {
            String result = stringRedisTemplate.opsForValue().get("goods:001");
            int goodNumber = result == null ? 0 : Integer.parseInt(result);
            if (goodNumber > 0) {
                int realNum = goodNumber - 1;
                stringRedisTemplate.opsForValue().set("goods:001", realNum + "");
                return "秒杀成功,剩余库存:" + realNum;
            }
            return "商品已售罄!";
        } finally {
            redissonLock.unlock();
        }
    }

}

多节点的情况下,代码相同,要保证 lock 的 key 一样

- SpringBoot集成Redisson

引入 redis 和 redisson starter:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
</dependency>

redis 配置:

spring:
  redis:
    # 地址
    host: 192.168.20.26
    # 端口,默认为6379
    port: 6379
    password: 123456
    # 连接超时时间
    timeout: 30s
    database: 0
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms

redisson配置:

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

/**
 * redisson配置
 */
@Configuration
public class RedissonConfig {

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

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        // 单体配置,如果是SSL连接,使用 rediss://
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://" + host + ":" + port);
        singleServerConfig.setPassword(password);
        return Redisson.create(config);
    }
}

使用:

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
public class TestController {

    @Resource
    private RedissonClient redissonClient;

    public void doAction1() {
        String lockKey = "lockKey";
        RLock lock = redissonClient.getLock(lockKey);
        // 加锁,无参数
        lock.lock();
        // 锁有效时间,时间单位
        // lock.lock(5, TimeUnit.SECONDS);
        try {
            // do something
        } finally {
            // 解锁
            lock.unlock();
        }
    }
    public void doAction2() throws InterruptedException {
        String lockKey = "lockKey";
        RLock lock = redissonClient.getLock(lockKey);
        // 1. 无参数,直接上锁
        // lock.tryLock();
        // 2. 等待时间,时间单位
        // lock.tryLock(3, TimeUnit.SECONDS);
        // 3. 等待时间,锁有效时间,时间单位
        // lock.tryLock(3, 5, TimeUnit.SECONDS);
        try {
            // 常规写法
            if (lock.tryLock(3, 5, TimeUnit.SECONDS)) {
                // do something
            } else {
                // 没有获取到锁
            }
        } finally {
            // 解锁
            lock.unlock();
        }
    }

}

到此这篇关于基于Redis分布式锁Redisson及SpringBoot集成Redisson的文章就介绍到这了,更多相关Redis分布式锁内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文