SpringBoot + Redisson Demo
redisson 官网: https://redisson.org/
一、引入 maven 包
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.17.0</version> </dependency>
二、配置 Redis 链接
redisson.redis.host=127.0.0.1 redisson.redis.port=6379 redisson.redis.password=
三、创建 Redisson 客户端Bean
import lombok.Data; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "redisson.redis") @Data public class RedissonConfig { private String host; private String port; private String password; @Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer().setAddress("redis://" + host + ":" + port); //有不同的模式此处使用Single模式,还有useCustomServers/useSentinelServers/useMasterSlaveServers return Redisson.create(config); } }
四、使用Redisson 客户端 进行 存储和查询
import org.redisson.api.RBucket; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.ExecutionException; @Component public class RedissonDemo { @Autowired private RedissonClient redissonClient; /** * 通用对象桶,可以用来存放任意类型的对象 */ public void baseOperate() throws ExecutionException, InterruptedException { //同步 RBucket<String> bucket1 = redissonClient.getBucket("bucket1"); bucket1.set("Hello World"); System.out.println(bucket1.get()); RBucket<String> bucket2 = redissonClient.getBucket("bucket2"); bucket2.setAsync("Douger").get(); bucket2.getAsync().thenAccept(System.out::println); } /** * 直接拿之前放置的数据 */ public void baseOperate1() throws ExecutionException, InterruptedException { //同步 RBucket<String> bucket1 = redissonClient.getBucket("bucket1"); System.out.println(bucket1.get()); } }
五、 测试
import com.yyds.redis.redisson.RedissonDemo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.concurrent.ExecutionException; @SpringBootTest public class RedisBaseOper { @Autowired private RedissonDemo redissonDemo; @Test public void baseTest() { try {
redissonDemo.baseOperate(); redissonDemo.baseOperate1(); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果:

此时在 redis 客户端可查看到 key 为 bucket1 和 bucket2 的数据, 除了放入的数据外, 还有一些前缀
127.0.0.1:6379> get bucket1 "\x04>\x0bHello World" 127.0.0.1:6379> get bucket2 "\x04>\x06Douger" 127.0.0.1:6379>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结