当前位置:   article > 正文

springboot整合spring-data-redis_spring-boot-data-redis

spring-boot-data-redis

前言

官方网站:Spring Data Redis

其实,整合是一个循序渐进的学习,你肯定是要了解之前底层的相关知识,才能够具体知道现在框架方法api到底tm有什么作用,所以建议先看看我之前的redis博客。

可以不看,但是可以以我这个为目录,针对性得去了解相关知识(学习任何东西,都应该这样)。

(41条消息) Cenos7 --- Redis下载和安装(Linux版本)_本郡主是喵的博客-CSDN博客

(41条消息) redis学习 -- 常用指令_本郡主是喵的博客-CSDN博客

(41条消息) redis -- 持久化存储方案_本郡主是喵的博客-CSDN博客

(41条消息) redis的4种模式,单机,哨兵、主从复制、集群_本郡主是喵的博客-CSDN博客

1.快速入门

1.1导入相关依赖

  1. <!--spring-data-redis组件-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!--commons-pools连接池,lettuce没有内置的数据库连接池所以要用第三方的 -->
  7. <dependency>
  8. <groupId>org.apache.commons</groupId>
  9. <artifactId>commons-pool2</artifactId>
  10. </dependency>
  11. <!--web组件-->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <!--test组件-->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>

1.2 配置文件

application.yml

  1. server:
  2. port: 8080
  3. # redis配置
  4. spring:
  5. redis:
  6. password: 123456
  7. # 默认0库
  8. database: 0
  9. #连接超时时间
  10. timeout: 10000ms
  11. port: 6379
  12. host: 192.168.88.135
  13. lettuce:
  14. pool:
  15. # 设置最大连接数
  16. max-active: 1024
  17. # 最大阻塞时间
  18. max-wait: 10000ms
  19. # 最大空间连接,默认8
  20. max-idle: 200
  21. # 最小空间连接,默认5
  22. min-idle: 5

1.3 核心代码

  1. @SpringBootTest
  2. class SpringDataRedisDemo1ApplicationTests {
  3. @Autowired
  4. // 这一种完全够用(与第二种,稍微一点差别,具体看下文)
  5. private RedisTemplate redisTemplate;
  6. @Autowired
  7. // 转对redis的string类型的
  8. private StringRedisTemplate stringRedisTemplate;
  9. @Test
  10. void testDemo() {
  11. ValueOperations value = redisTemplate.opsForValue();
  12. value.set("qhx","name");
  13. System.out.println(value.get("qhx"));
  14. ValueOperations<String, String> stringValueOperations = stringRedisTemplate.opsForValue();
  15. System.out.println(stringValueOperations.get("name"));
  16. }
  17. }

此处可以忽略。

我们在application.yml配置文件信息,按住ctrl + 单击点进去,发现是一个映射redis配置信息的类。

 就是,假如有些配置你不知道,可以依托这个去相应的官网文档上去查找相应的用法。

1.4 自定义模版解决序列化问题

打开redis可视化客户端,发现我们成功存入数据,但是是java字节数据。如何解决?

先看下图。

 RedisConfig.java

  1. @Configuration
  2. public class RedisConf {
  3. @Bean
  4. public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
  5. RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
  6. // 为string 的key设置序列化
  7. redisTemplate.setKeySerializer(new StringRedisSerializer());
  8. // 为string类型的value设置序列化
  9. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  10. // 为hash类型的value设置json序列化
  11. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  12. // 为hash类型的key设置json序列化
  13. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
  14. // 设置数据库连接池
  15. redisTemplate.setConnectionFactory(lettuceConnectionFactory);
  16. return redisTemplate;
  17. }
  18. }

再次运行。

成功!

此处可以忽略。

可以看出,我们设置key先转成java字节数据,最后存入数据库中。

自定义后,我们设置key先转成java string数据,最后存入数据库中。

2.操作各种类型数据

此处可以忽略

其实,这些api如果你没有学过,需要跟着敲一遍,敲得途中你可能会发现这些api的意思(有基础的前提下)。

2.1 操作string类型数据

  1. // 添加一条数据
  2. ValueOperations ops = redisTemplate.opsForValue();
  3. ops.set("name","zhangsan");
  4. // 获取一条数据
  5. String name = (String) ops.get("name");
  6. System.out.println(name);
  7. // 层级关系
  8. ops.set("user:01","lisi");
  9. // 添加多条数据
  10. HashMap<String, String> hashMap = new HashMap<>();
  11. hashMap.put("age","20");
  12. hashMap.put("address","wei");
  13. ops.multiSet(hashMap);
  14. // 获取多条数据
  15. ArrayList<Object> list = new ArrayList<>();
  16. list.add("age");
  17. list.add("address");
  18. List listValue = ops.multiGet(list);
  19. listValue.forEach(System.out::println);

2.2 操作hash类型数据

  1. HashOperations hashOps = redisTemplate.opsForHash();
  2. // 添加hash单条数据
  3. hashOps.put("user","name","qhx");
  4. // 获取hash单条数据
  5. String hash = (String) hashOps.get("user", "name");
  6. // 添加hash多条数据
  7. HashMap<String, String> hashMap = new HashMap<>();
  8. hashMap.put("age","20");
  9. hashMap.put("address","wei");
  10. hashOps.putAll("user",hashMap);
  11. // 获取hash类型的多条数据
  12. ArrayList<String> list = new ArrayList<>();
  13. list.add("age");
  14. list.add("address");
  15. List user = hashOps.multiGet("user", list);
  16. user.forEach(System.out::println);
  17. // 获取hash类型所有数据
  18. Map entries = hashOps.entries("user");
  19. entries.forEach((key,value)->{
  20. System.out.println(key+"-->"+value);
  21. });
  22. // 删除Hash类型数据
  23. hashOps.delete("user","age","name");

2.3 操作list类型数据

  1. ListOperations listOps = redisTemplate.opsForList();
  2. // 左添加
  3. listOps.leftPush("students","w1");
  4. listOps.leftPushAll("students","w2","w3");
  5. // 右添加
  6. listOps.leftPush("students","w4");
  7. // 在w1前面左添加w0
  8. listOps.leftPush("students","w1","w0");
  9. // 获取数据
  10. List list = listOps.range("students", 0, 4);
  11. System.out.println(list);
  12. // 获取总条数
  13. Long students = listOps.size("students");
  14. // 删除1条数据
  15. listOps.remove("students",1,"w0");
  16. // 左弹出
  17. listOps.leftPop("students");
  18. listOps.rightPop("students");

2.4 操作set类型数据

  1. SetOperations setOps = redisTemplate.opsForSet();
  2. // 添加数据
  3. setOps.add("ids","1","2");
  4. // 获取数据
  5. Set ids = setOps.members("ids");
  6. ids.forEach(System.out::println);
  7. // 删除数据
  8. setOps.remove("ids","1","2");

2.5 操作sorted-set类型数据

  1. ZSetOperations zSetOps = redisTemplate.opsForZSet();
  2. // 添加多条数据(我自己也属实看不懂)将数据封装到hash里。
  3. ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("wls",1D);
  4. ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("qhx",2D);
  5. ZSetOperations.TypedTuple<Object> objectTypedTuple3 = new DefaultTypedTuple<>("zlx",3D);
  6. Set<ZSetOperations.TypedTuple> tuples = new HashSet<>();
  7. tuples.add(objectTypedTuple1);
  8. tuples.add(objectTypedTuple2);
  9. tuples.add(objectTypedTuple3);
  10. zSetOps.add("names",tuples);
  11. // 添加单条数据
  12. zSetOps.add("names","qhx1",4D);
  13. // 获取数据
  14. Set names = zSetOps.range("names", 0, 3);
  15. names.forEach(System.out::println);
  16. // 删除数据
  17. zSetOps.remove("names","qhx","wls");

2.6 获取所有的key和给key设置过期时间

  1. // 获取所有的keys(当前数据库)
  2. Set keys = redisTemplate.keys("*");
  3. keys.forEach(System.out::println);
  4. // 给已存在key设置失效时间
  5. ValueOperations ops = redisTemplate.opsForValue();
  6. ops.set("name","qhx",200, TimeUnit.SECONDS);
  7. // 给key设置失效时间
  8. redisTemplate.expire("age",30,TimeUnit.SECONDS);

3.配置哨兵模式

先在redis中启用哨兵模式。

还有别忘了用Linux防火墙把相应的端口打开(或者,直接关闭防火墙)。

1.方式一,在aplication.yml文件中新加 spring.redis.sentinel

预览图:

  1. sentinel:
  2. # 主节点名称
  3. master: mysaster
  4. # 主服务器密码
  5. password: 123456
  6. # 哨兵节点
  7. nodes: 192.168.88.135:26379,192.168.88.135:26380,192.168.88.135:26381

2.方式二,

在上文RedisCong.java中添加

  1. @Bean
  2. public RedisSentinelConfiguration redisSentinelConfiguration(){
  3. RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration()
  4. // 主节点名称
  5. .master("mymaster")
  6. // 哨兵
  7. .sentinel("192.168.88.135",26379)
  8. .sentinel("192.168.88.135",26380)
  9. .sentinel("192.168.88.135",26381);
  10. // 密码
  11. redisSentinelConfiguration.setPassword("123456");
  12. return redisSentinelConfiguration;
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/715790
推荐阅读
相关标签
  

闽ICP备14008679号