当前位置:   article > 正文

Redis 入门及应用 ( 七 ) redisson 实现 分布锁_redisson-spring-data-21

redisson-spring-data-21

7.3.***redisson

1.引入依赖

直接使用 redisson依赖

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.11.1</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

或者

整合到springboot

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.1</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

需要注意的目前3.13.1所以依赖的 是redisson-spring-data-22
说明如果是redisson-spring-data-22 的话对应的springboot版本就是2.2.x 的版本
如果 不是springboot 2.2.x 的版本 , 可以 如下排除

<dependency>
     <groupId>org.redisson</groupId>
     <artifactId>redisson-spring-boot-starter</artifactId>
     <version>3.13.1</version>
     <exclusions>
         <exclusion>
             <groupId>org.redisson</groupId>
             <artifactId>redisson-spring-data-22</artifactId>
         </exclusion>
     </exclusions>
 </dependency>

 <dependency>
     <groupId>org.redisson</groupId>
     <!-- for Spring Data Redis v.2.3.x -->
     <artifactId>redisson-spring-data-21</artifactId>
     <version>3.13.1</version>
 </dependency>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2.修改application配置文件

yml 版本

spring:
  redis:
    redisson:
      # 配置单点模式
      config: classpath:redisson.yml


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

properties 版本

# 配置单点模式
spring.redis.redisson.config: classpath:redisson.yml

  • 1
  • 2
  • 3

3.增加 redisson.yml 配置文件

# 单节点配置
singleServerConfig:
  # 连接空闲超时,单位:毫秒
  idleConnectionTimeout: 10000
  # 连接超时,单位:毫秒
  connectTimeout: 10000
  # 命令等待超时,单位:毫秒
  timeout: 3000
  # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
  # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
  retryAttempts: 3
  # 命令重试发送时间间隔,单位:毫秒
  retryInterval: 1500
  #  # 重新连接时间间隔,单位:毫秒
  #  reconnectionTimeout: 3000
  #  # 执行失败最大次数
  #  failedAttempts: 3
  # 密码
  password: turing
  # 单个连接最大订阅数量
  subscriptionsPerConnection: 5
  # 客户端名称
  clientName: null
  #  # 节点地址
  address: redis://192.168.43.200:6379
  # 发布和订阅连接的最小空闲连接数
  subscriptionConnectionMinimumIdleSize: 1
  # 发布和订阅连接池大小
  subscriptionConnectionPoolSize: 50
  # 最小空闲连接数
  connectionMinimumIdleSize: 32
  # 连接池大小
  connectionPoolSize: 64
  # 数据库编号
  database: 0
  # DNS监测时间间隔,单位:毫秒
  dnsMonitoringInterval: 5000
# 线程池数量,默认值: 当前处理核数量 * 2
threads: 0
# Netty线程池数量,默认值: 当前处理核数量 * 2
nettyThreads: 0
# 编码
codec: !<org.redisson.codec.JsonJacksonCodec> {}
# 传输模式
transportMode : "NIO"

  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

4.redisson客户端

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;


@Configuration
public class RedissonConfig
{
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() throws IOException
    {
        RedissonClient redisson = Redisson.create(Config.fromYAML(new ClassPathResource("redisson.yml").getInputStream()));
        return redisson;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

或者

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;

@Configuration
 public class RedissonConfig {


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

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

    @Bean(destroyMethod = "shutdown")
    public RedissonClient getRedisson (){
        // 默认连接地址 127.0.0.1:6379
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + hostAddr + ":6379")
                .setPassword(password);
        RedissonClient redisson = Redisson.create(config);

        return redisson;
    }

 }
  • 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

5.加锁,解锁测试

    @Autowired
    private CategoryThreeService categoryThreeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/list")
    public List<CategoryThree> query(){
        List<CategoryThree> list = new ArrayList<>();
        ValueOperations valueOperations = redisTemplate.opsForValue();

        RLock lock = redissonClient.getLock("my-lock");
        lock.lock();
        System.out.println("得到锁:" + Thread.currentThread().getId());
        try {
            Object ll = valueOperations.get("list");
            if (ll != null) {
                // 取出来
                list = (List<CategoryThree>) ll;
                System.out.println(" 从 redis 中 取出");
    
            }else {
                list = categoryThreeService.list();
                valueOperations.set("list", list);
                System.out.println(" 从 数据库中 取出 , 再 在存储到 redis 里");
            }
        } finally {
            System.out.println("释放锁:" + Thread.currentThread().getId());
            lock.unlock();
        }
        return list;
    }
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/815233
推荐阅读
相关标签
  

闽ICP备14008679号