redis客户端Jedis和Lettuce

2023-11-16

Jedis和Lettuce的区别

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

springBoot 使用 Jedis

导入依赖

<!--redis客户端-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

配置Jedis及pool

建立redis.properties,配置如下

redis.node.maxTotal = 10

reids.node.host = xxxx

redis.node.port= 6379

package com.buba.configuration;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
@SpringBootConfiguration
@PropertySource(value = {"classpath:redis/redis.properties"})
public class RedisConfiguration {
    @Value("${redis.node.maxTotal}")
    private Integer maxTotal;
    @Value("${redis.node.host}")
    private String host;
    @Value("${redis.node.port}")
    private Integer port;
 
    public JedisPoolConfig jedisPoolConfig(){    
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        return jedisPoolConfig;
    }
 
    @Bean  //这个注解注入工厂的名称是方法名
    public JedisPool jedisPool(){
        JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
        return new JedisPool(jedisPoolConfig,host,port);
    }
}

测试jedis

@Autowired
private JedisPool  jedisPool

Jedis resource = jedisPool. getResource();
resource. set("test","springboot");
String test = resource.get("test");
System.outprintln(test);

集群版连接

@SpringBootConfiguration
public class RedisClusterConfiguration {

    // 读取redis.node
    @Value("${redis.cluster.nodes}")
    private String nodes;

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> set = new HashSet<>();
        HostAndPort hp = null;
        String[] nodeStr = nodes.split(",");
        if(nodeStr!=null&&nodeStr.length>0){
            for(int i=0;i<nodeStr.length;i++){
                String[] hostPort = nodeStr[i].split(":");
                if(hostPort!=null&&hostPort.length>0){
                    hp = new HostAndPort(hostPort[0],Integer.valueOf(hostPort[1]));
                    set.add(hp);
                }
            }
        }
        JedisCluster jedisCluster = new JedisCluster(set);
        return jedisCluster;
    }

}

 测试集群版

@Autowired
private JedisCluster jedisCluster;

jedisCluster.set("test","springbootcluster");
String test = jedisCluster.get("test");
System. out.println(test);

springBoot使用Lettuce

引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.lettuce</groupId>
                <artifactId>lettuce-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>

配置lettuce

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

测试使用 

 @Autowired
 private StringRedisTemplate redisTemplate;

 String s = redisTemplate.opsForValue().get(key);

 System.out.println(s);

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

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

redis客户端Jedis和Lettuce 的相关文章

随机推荐

  • etcd的简单使用

    etcd的简单使用 ETCD安装配置 安装 去https github com coreos etcd releases 下载想要的版本解压etcd包 解压后进入目录 增加x权限 chmod x etcd chmod x etcdctl 并
  • MySQL5.7安装报错:GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM

    根据官方文档使用yum安装MySQL5 7 添加mysl comunity repo如下 mysql57 community name MySQL 5 7 Community Server baseurl http repo mysql c
  • [Docker]进入容器命令

    docker exec it api bin bash docker exec it api bin sh
  • JVM入门教程

    文章目录 简介 1 Java内存区域 1 1 程序计数器 1 2 Java虚拟机栈 1 3 本地方法栈 1 4 Java堆 1 5 方法区 1 6 运行时常量池 1 7 直接内存 2 HotSpot虚拟机 2 1 对象的创建 2 2 对象的
  • MyISAM和InnoDB区别关联详解

    Mysql架构 什么存储引擎 MySQL和InnoDB对比1 2 总结 Mysql存储架构 从上图可以发现 MySQL由以下几部分组成 连接池组件 管理服务和工具组件 SQL接口组件 查询分析器组件 优化器组件 缓冲 Cache 组件 插件
  • C++引用,四区和函数

    引用变量 四区 函数 没有函数重载 代码区 全局区 堆区和栈区 代码区 全局区 栈区 堆区 new操作符 引用 函数的默认参数 函数的占位参数 代码区 全局区 堆区和栈区 注意 其中代码区和全局区是运行前的 栈区和堆区是运行后的 即如果ex
  • 微信小程序实现举报功能

    一 后台接口 userController java 前端接收一个usersReportd对象 包含数据如下 PostMapping reportUser public IMoocJSONResult reportUser RequestB
  • React Hooks

    Facebook团队对社区上的MVC框架都不太满意的情况下 开发了一套开源的前端框架react 于2013年发布第一个版本 react最开始倡导函数式编程 使用function以及内部方法React creactClass创建组件 之后在E
  • 第二章 系统设置及基本操作

    第二章 系统设置及基本操作 使用GNOME桌面套件中的首选项设置及系统管理工具执行以下任务 一 为第一块网卡设置静态IP地址 并能够与同网段中的其他主机相互通信 步骤 1 点击 系统 管理 网络 打开 网络配置 窗口 如图所示 2 在 配置
  • 华为od机试 C++ 猜字谜

    题目 玩家看到的是个错乱的单词 像 nesw 这样 他们要做的就是从一大堆备选的单词中 猜出这个错乱单词原来的模样 怎么才算猜对了呢 有两种可能 把错乱单词的字母重新排列一下 如果跟备选单词一模一样 那就对了 例如 nwes 重新排列就是
  • CTFshow 命令执行 web34

    源码
  • 小白上路~微信小程序登录授权无法获取用户信息

    1 button 标签和 open type getUserInfo 获取用户信息失败 天哪噜 必须好好记录一番由于没有看官方文档更新 api 而导致的 BUG 一觉醒来 发现准备收尾的小程序无法获取到用户信息了 怎么回事 于是一顿焦虑 骚
  • CodeWhisperer 初体验

    今年算是 AI 正式破圈的一年 无数的工具 产品横空出世 无论在面向企业的大语言模型 还是帮助个人的 AI 工具 数不胜数 其中关于 AI 编程助手领域 近年来也涌现了很多不错的产品 例如 Copilot Cursor 还是我们今天要体验的
  • 【转】在iPad的Safari上查看HTML源代码

    在网上搜索的文章 转来转去 基本上都缺少了关键脚本 所以写在这了 使用方法 1 随便保存一个书签 名称就叫查看源码之类的就好了 2 编辑该书签 删除原网址 将下面的脚本黏贴到网址中 3 在你想要查看源码的页面点击该书签 源码就出现了 jav
  • 【Pytorch】第 1 章 :强化学习和 PyTorch 入门

    大家好 我是Sonhhxg 柒 希望你看完之后 能对你有所帮助 不足请指正 共同学习交流 个人主页 Sonhhxg 柒的博客 CSDN博客 欢迎各位 点赞 收藏 留言 系列专栏 机器学习 ML 自然语言处理 NLP 深度学习 DL fore
  • html网页的基本标签

    1 标题标签 h1 一级标签 h1 h2 二级标签 h2 h3 三级标签 h3 h4 四级标签 h4 h5 五级标签 h5 2 段落标签 p 民办清华 建校三十周年 p p okok p 3 换行标签 4 水平线标签 5 字体样式标签 st
  • python写水仙花数

    水仙花数是指一个n位数 n gt 3 他的每个位上 的数字的n次幂之和等于他本身 例1 3 5 3 求1000以内的水仙花数 i 100 while i lt 1000 a i 100 b i 10 10 c i 10 if a 3 b 3
  • FPGA学习日记(五)ZYNQ——在线逻辑分析仪(ILA)硬件调试及simulator仿真软件的创建使用

    一 在线逻辑分析仪 ILA vivado的在线逻辑分析仪 ILA 其借用了传统逻辑分析仪的理念以及大部分的功能 并利用 FPGA 中的逻辑资源 将这些功能植入到 FPGA 的设计当中 如下图所示 ILA占用一部分FPGA内部逻辑资源 可看做
  • 运算放大器的关键指标详解二(噪声)

    1 噪声指标 Noise 一个正常工作的放大电路 当输入端接地时 用示波器观察输出 你看到的可能不是平直的细线 而是在一定幅度之内的杂乱无章的波形 这就是噪声 你在示波器上看到线越粗 就说明噪声幅度越大 放大电路的输出端噪声 小至 V 以下
  • redis客户端Jedis和Lettuce

    Jedis和Lettuce的区别 Jedis是同步的 不支持异步 Jedis客户端实例不是线程安全的 需要每个线程一个Jedis实例 所以一般通过连接池来使用Jedis Lettuce是基于Netty框架的事件驱动的Redis客户端 其方法