SpringBoot 整合Redisson

  • A+
所属分类:redis

  Redis安装参考:【Redis】安装及简单使用

一、Redission整合

1、创建SpringBoot项目

  引入SpringBoot的redission启动器:

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

   查看jar包相关依赖

  SpringBoot 整合Redisson

2、配置redission

  1) 在application.yml 中添加redission 配置文件路径

SpringBoot 整合Redisson

 1 spring: 2   redis: 3     database: 0 4     timeout: 3000 5     password: 
 6     # 单节点模式 7     host: 127.0.0.1 8     port: 6379 9     # redisson配置文件路径10     redisson:11       file: classpath:redisson.yml

SpringBoot 整合Redisson

  2) 新建resources/redission.yml文件, 内容如下: 

SpringBoot 整合Redisson

 1 # 单节点配置 2 singleServerConfig: 3   # 连接空闲超时,单位:毫秒 4   idleConnectionTimeout: 10000 5   # 连接超时,单位:毫秒 6   connectTimeout: 10000 7   # 命令等待超时,单位:毫秒 8   timeout: 3000 9   # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。10   # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。11   retryAttempts: 312   # 命令重试发送时间间隔,单位:毫秒13   retryInterval: 150014   # 密码15   password:16   # 单个连接最大订阅数量17   subscriptionsPerConnection: 518   # 客户端名称19   clientName: myredis20   # 节点地址21   address: redis://127.0.0.1:637922   # 发布和订阅连接的最小空闲连接数23   subscriptionConnectionMinimumIdleSize: 124   # 发布和订阅连接池大小25   subscriptionConnectionPoolSize: 5026   # 最小空闲连接数27   connectionMinimumIdleSize: 3228   # 连接池大小29   connectionPoolSize: 6430   # 数据库编号31   database: 032   # DNS监测时间间隔,单位:毫秒33   dnsMonitoringInterval: 500034 # 线程池数量,默认值: 当前处理核数量 * 235 #threads: 036 # Netty线程池数量,默认值: 当前处理核数量 * 237 #nettyThreads: 038 # 编码39 codec: !<org.redisson.codec.JsonJacksonCodec> {}40 # 传输模式41 transportMode : "NIO"

SpringBoot 整合Redisson

3、编辑启动类 和 配置类

  1) 主启动类 Application.java

SpringBoot 整合Redisson

 1 @SpringBootApplication 2 public class Application { 3  4     /** 5      * 测试 Redis客户端 Redission 的使用 6      * @param args 7      */ 8     public static void main(String[] args) { 9         SpringApplication.run(Application.class);10     }11 }

SpringBoot 整合Redisson

  2) 测试类 TestApplication.java

SpringBoot 整合Redisson

 1 package com.test.redis; 2  3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.redisson.api.RLock; 6 import org.redisson.api.RedissonClient; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.boot.test.context.SpringBootTest; 9 import org.springframework.context.ApplicationContext;10 import org.springframework.test.context.junit4.SpringRunner;11 12 @RunWith(SpringRunner.class)13 @SpringBootTest14 public class TestApplication {15 16     @Autowired17     ApplicationContext context;18 19     // redisson客户端20     @Autowired21     RedissonClient redissonClient;22 23     // 测试分布式锁24     @Test25     public void terst1() throws InterruptedException {26         RLock lock = redissonClient.getLock("anyLock");27 28         new Thread(() -> {29             lock.lock();30 31             try {32                 System.out.println(Thread.currentThread().getName() + ":\t 获得锁");33                 Thread.sleep(3000);34             } catch (InterruptedException e) {35                 e.printStackTrace();36             } finally {37                 System.out.println(Thread.currentThread().getName() + ":\t 释放锁");38                 lock.unlock();39             }40         }).start();41 42 43         new Thread(() -> {44             lock.lock();45             try {46                 System.out.println(Thread.currentThread().getName() + ":\t 获得锁");47                 Thread.sleep(3000);48             } catch (InterruptedException e) {49                 e.printStackTrace();50             } finally {51                 System.out.println(Thread.currentThread().getName() + ":\t 释放锁");52                 lock.unlock();53             }54         }).start();55 56         Thread.sleep(100000);57     }58 }

SpringBoot 整合Redisson

4、测试运行

  正常运行,且能加锁成功

  控制台输出: SpringBoot 整合Redisson  redis数据:SpringBoot 整合Redisson

  分布式锁原理参考:【Redis】分布式锁之Redis实现

二、Redission整合原理

1、Redission的自动配置类 RedissonAutoConfiguration

SpringBoot 整合Redisson

 1 @Configuration 2 @ConditionalOnClass({Redisson.class, RedisOperations.class}) 3  4 // @AutoConfigureBefore表示在 RedisAutoConfiguration 之前注入 5 @AutoConfigureBefore(RedisAutoConfiguration.class) 6  7 @EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class}) 8 public class RedissonAutoConfiguration { 9     ......10     11     // Redisson属性配置12     @Autowired13     private RedissonProperties redissonProperties;14     // Redis属性配置15     @Autowired16 17 18     // 注入了 redisTemplate19     @Bean20     @ConditionalOnMissingBean(name = "redisTemplate")21     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {22         RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();23         template.setConnectionFactory(redisConnectionFactory);24         return template;25     }26 27     // 注入了 stringRedisTemplate28     @Bean29     @ConditionalOnMissingBean(StringRedisTemplate.class)30     public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {31         StringRedisTemplate template = new StringRedisTemplate();32         template.setConnectionFactory(redisConnectionFactory);33         return template;34     }35 36     // 注入了 redissonConnectionFactory redission连接工厂37     @Bean38     @ConditionalOnMissingBean(RedisConnectionFactory.class)39     public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {40         return new RedissonConnectionFactory(redisson);41     }42 43     // 注入了 RedissonClient 客户端44     @Bean(destroyMethod = "shutdown")45     @ConditionalOnMissingBean(RedissonClient.class)46     public RedissonClient redisson() throws IOException {47         Config config = null;48         ......49         return Redisson.create(config);50     }51     ......52 }

SpringBoot 整合Redisson

  可以看到配置类中注入了Redission客户端对象 RedissonClient

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: