springboot整合redisson单机模式
文档
官方文档
说明
- redis版本:7.0.0
- springboot版本:3.2.0
springboot整合redisson单机模式
安装单机版redis
- 安装单机版redis参考文档:redis单机安装
方式1:手动创建RedissonClient
-
说明:用RedisProperties手动创建RedissonClient Bean,并且复用Spring的Redis配置
-
参考:springboot整合redis-RedisTemplate单机模式,配置
application.yml配置文件yamlspring: data: redis: database: 0 host: 192.168.145.132 port: 6379 password: 123456 client-type: lettuce lettuce: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0- 这里的配置是
spring-boot-starter-data-redis的配置,一般情况下都会先引用Spring的redis。在这里,集成redisson时,复用Spring的Redis配置,手动创建RedissonClient - 实际上,集成redisson不必须依赖 Spring 的 spring-boot-starter-data-redis
- 这里的配置是
-
引入依赖
xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.37.0</version> </dependency>- spring-boot-starter-data-redis的版本号:
3.2.0,与springboot版本一致 - redisson的版本号:
3.37.0
- spring-boot-starter-data-redis的版本号:
-
RedissonConfig.java配置类创建RedissonClient Beanjava@Configuration @EnableConfigurationProperties(RedisProperties.class) public class RedissonConfig { private final RedisProperties redisProperties; public RedissonConfig(RedisProperties redisProperties) { this.redisProperties = redisProperties; } @Bean(destroyMethod = "shutdown") public RedissonClient redissonClient() { Config config = new Config(); // 是否启用 SSL String prefix = "redis://"; // 连接池参数(来自 spring.data.redis.lettuce.pool) RedisProperties.Pool pool = redisProperties.getLettuce() != null ? redisProperties.getLettuce().getPool() : null; SingleServerConfig singleServerConfig = config.useSingleServer() .setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort()) .setDatabase(redisProperties.getDatabase()); if (pool != null) { // max-active 对应 Redisson 的连接池大小 singleServerConfig.setConnectionPoolSize(pool.getMaxActive()); // min-idle 对应 Redisson 的最小空闲连接 singleServerConfig.setConnectionMinimumIdleSize(pool.getMinIdle()); } if (StringUtils.hasText(redisProperties.getPassword())) { singleServerConfig.setPassword(redisProperties.getPassword()); } return Redisson.create(config); } }
方式2:引用redisson-spring-boot-starter,自动装配RedissonClient
配置连接信息
-
修改
application.yml配置文件yamlspring: data: redis: database: 0 host: 192.168.145.132 port: 6379 password: 123456 client-type: lettuce lettuce: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0 -
引入依赖
xml<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.37.0</version> </dependency>- spring-boot-starter-data-redis的版本号:
3.2.0,与springboot版本一致 - redisson-spring-boot-starter的版本号:
3.37.0
- spring-boot-starter-data-redis的版本号:
-
starter 会按
spring.data.redis.*自动创建 Redisson 单机客户端。 -
当需要集群、哨兵、连接池等更细的配置时,用独立 Redisson 配置文件
示例
-
在
RedissonController.java中,注入RedissonClient对象来操作reidsjava@RestController @RequestMapping("redisson") public class RedissonController { @Autowired private RedissonClient redissonClient; @GetMapping("redisson-test") public String redissonTest() { // ================== 1. 字符串 / 简单对象:RBucket ================== // 新增 / 更新 RBucket<String> bucket = redissonClient.getBucket("test:string:key"); bucket.set("hello redisson"); // 默认不过期 // bucket.set("hello redisson", 10, TimeUnit.MINUTES); // 10 分钟过期 // 查询 String value = bucket.get(); // 删除 boolean deleted = bucket.delete(); // ================== 2. Hash 结构:RMap ================== RMap<String, String> map = redissonClient.getMap("test:map:key"); // 添加 / 修改 map.put("name", "Tom"); map.put("age", "18"); // 查询单个字段 String name = map.get("name"); // 查询所有 Map<String, String> all = new HashMap<>(map); // 删除字段 map.remove("age"); // 清空整个 map // map.delete(); // ================== 3. 分布式锁:RLock(可选示例) ================== RLock lock = redissonClient.getLock("lock:test"); try { // 尝试加锁,最多等待 3 秒,拿到锁后 10 秒自动释放 if (lock.tryLock(3, 10, TimeUnit.SECONDS)) { // 在这里执行业务逻辑(加锁保护的代码) System.out.println("获取到锁,执行业务逻辑..."); } else { System.out.println("未获取到锁"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { // 判断当前线程是否持有锁,避免误解锁 if (lock.isHeldByCurrentThread()) { lock.unlock(); } } // 简单返回下结果,方便你在浏览器里看到 return "bucketValue=" + value + ", bucketDeleted=" + deleted + ", mapName=" + name + ", mapSize=" + all.size(); } }
参考资料
注意事项
- 部分内容由AI生成
- 如有不对,欢迎指正!!!