• redis客户端Jedis和Lettuce


    Jedis和Lettuce的区别

    • Jedis是同步的,不支持异步,Jedis客户端实例不是线程安全的,需要每个线程一个Jedis实例,所以一般通过连接池来使用Jedis
    • Lettuce是基于Netty框架的事件驱动的Redis客户端,其方法调用是异步的,Lettuce的API也是线程安全的,所以多个线程可以操作单个Lettuce连接来完成各种操作,同时Lettuce也支持连接池
    • SpringBoot2.0之前默认使用Jedis,而2.0之后默认就都是使用的Lettuce这个客户端连接Redis服务器

    springBoot 使用 Jedis

    导入依赖

    1. <dependency>
    2. <groupId>redis.clientsgroupId>
    3. <artifactId>jedisartifactId>
    4. <version>2.9.0version>
    5. dependency>

    配置Jedis及pool

    建立redis.properties,配置如下

    redis.node.maxTotal = 10

    reids.node.host = xxxx

    redis.node.port= 6379

    1. package com.buba.configuration;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.boot.SpringBootConfiguration;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.PropertySource;
    6. import redis.clients.jedis.JedisPool;
    7. import redis.clients.jedis.JedisPoolConfig;
    8. @SpringBootConfiguration
    9. @PropertySource(value = {"classpath:redis/redis.properties"})
    10. public class RedisConfiguration {
    11. @Value("${redis.node.maxTotal}")
    12. private Integer maxTotal;
    13. @Value("${redis.node.host}")
    14. private String host;
    15. @Value("${redis.node.port}")
    16. private Integer port;
    17. public JedisPoolConfig jedisPoolConfig(){
    18. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    19. jedisPoolConfig.setMaxTotal(maxTotal);
    20. return jedisPoolConfig;
    21. }
    22. @Bean //这个注解注入工厂的名称是方法名
    23. public JedisPool jedisPool(){
    24. JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
    25. return new JedisPool(jedisPoolConfig,host,port);
    26. }
    27. }

    测试jedis

    1. @Autowired
    2. private JedisPool jedisPool
    3. Jedis resource = jedisPool. getResource();
    4. resource. set("test","springboot");
    5. String test = resource.get("test");
    6. System.outprintln(test);

    集群版连接

    1. @SpringBootConfiguration
    2. public class RedisClusterConfiguration {
    3. // 读取redis.node
    4. @Value("${redis.cluster.nodes}")
    5. private String nodes;
    6. @Bean
    7. public JedisCluster jedisCluster(){
    8. Set set = new HashSet<>();
    9. HostAndPort hp = null;
    10. String[] nodeStr = nodes.split(",");
    11. if(nodeStr!=null&&nodeStr.length>0){
    12. for(int i=0;i
    13. String[] hostPort = nodeStr[i].split(":");
    14. if(hostPort!=null&&hostPort.length>0){
    15. hp = new HostAndPort(hostPort[0],Integer.valueOf(hostPort[1]));
    16. set.add(hp);
    17. }
    18. }
    19. }
    20. JedisCluster jedisCluster = new JedisCluster(set);
    21. return jedisCluster;
    22. }
    23. }

     测试集群版

    1. @Autowired
    2. private JedisCluster jedisCluster;
    3. jedisCluster.set("test","springbootcluster");
    4. String test = jedisCluster.get("test");
    5. System. out.println(test);

    springBoot使用Lettuce

    引入依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. dependency>

    如果想要使用Jedis,需要排除Lettuce依赖,并引入Jedis以及Jedis依赖的commons-pool2

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. <exclusions>
    5. <exclusion>
    6. <groupId>io.lettucegroupId>
    7. <artifactId>lettuce-coreartifactId>
    8. exclusion>
    9. exclusions>
    10. dependency>
    11. <dependency>
    12. <groupId>org.apache.commonsgroupId>
    13. <artifactId>commons-pool2artifactId>
    14. dependency>
    15. <dependency>
    16. <groupId>redis.clientsgroupId>
    17. <artifactId>jedisartifactId>
    18. dependency>

    配置lettuce

    1. # 连接池最大连接数(使用负值表示没有限制) 默认 8
    2. spring.redis.lettuce.pool.max-active=8
    3. # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
    4. spring.redis.lettuce.pool.max-wait=-1
    5. # 连接池中的最大空闲连接 默认 8
    6. spring.redis.lettuce.pool.max-idle=8
    7. # 连接池中的最小空闲连接 默认 0
    8. spring.redis.lettuce.pool.min-idle=0

    测试使用 

    1. @Autowired
    2. private StringRedisTemplate redisTemplate;
    3. String s = redisTemplate.opsForValue().get(key);
    4. System.out.println(s);

    集群模式的lettuce使用StringRedisTemplate同样适配,内部做了封装,直接使用即可 

     

  • 相关阅读:
    Java进阶篇--Executors类创建常见线程池
    无线耳机能不能设计成我想象的这样的?
    浏览器添加油猴(tampermonkey)扩展
    基于Android平台的手机安全卫士的设计与实现
    设计模式01———简单工厂模式 c#
    java面试题:java中的单例设计模式及两种实现方法的代码举例
    Gartner 2022年顶级战略技术趋势报告
    【前端Vue3】——JQuery知识点总结(超详细)
    android 13.0 在自定义系统服务中通过代码设置默认laucher动态切换
    代码随想录算法训练营第三十八天| 509. 斐波那契数 ,70. 爬楼梯,746. 使用最小花费爬楼梯
  • 原文地址:https://blog.csdn.net/m0_65775063/article/details/132859619