SpringBoot集成Jedis

news/2024/10/24 17:31:52

SpringBoot集成Jedis

Jedis/Redis/JDK版本关系

Jedis version Supported Redis versions JDK Compatibility
3.9+ 5.0 to 6.2 Family of releases 8, 11
>= 4.0 Version 5.0 to 7.2 Family of releases 8, 11, 17
>= 5.0 Version 6.0 to current 8, 11, 17, 21
>= 5.2 Version 7.2 to current 8, 11, 17, 21

前提条件:

  • 已创建SpringBoot项目

操作步骤

1、standalone连接配置

  • 在Pom.xml文件中增加spring-boot-starter-data-redis依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><!-- springboot2.0以上默认使用lettuce-core --><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions>
</dependency>
<!-- 原本使用jedis5.2.0,但由于找不到 redis.clients.jedis.JedisShardInfo,将版本改为3.9.0 -->
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.9.0</version><type>jar</type>
</dependency>
  • 创建配置类
@Configuration
public class AppConfig {@BeanJedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("192.168.1.19", 6379); // 192.168.1.19为局域网单机redis主机IPreturn new JedisConnectionFactory(config);}@BeanRedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, String> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);return template;}
}
  • 创建测试类
@SpringBootTest
public class RedisOperationTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void setValue() {redisTemplate.opsForValue().set("tayun", "1234");}
}

2、连接池配置

  • 创建application.yml文件,配置redis连接池属性
spring:redis:host: 192.168.1.19port: 6379database: 0jedis:pool:max-active: 10max-wait: 60max-idle: 10min-idle: 0max-wait-millis: 1500timeout: 30000
  • 创建配置类
@Configuration
@PropertySource("classpath:application.yml")
public class AppConfig {@Value("${spring.redis.jedis.pool.max-idle}")private Integer maxIdle;@Value("${spring.redis.jedis.pool.max-wait-millis}")private int maxWaitMillis;@Value("${spring.redis.jedis.pool.max-active}")private Integer maxActive;@Value("${spring.redis.jedis.pool.min-idle}")private Integer minIdle;@Value("${spring.redis.timeout}")private Integer timeout;public JedisPoolConfig jedisPoolConfig() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxIdle(maxIdle);jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);jedisPoolConfig.setMaxTotal(maxActive);jedisPoolConfig.setMinIdle(minIdle);jedisPoolConfig.setBlockWhenExhausted(false);// 是否启用pool的jmx管理功能, 默认truejedisPoolConfig.setJmxEnabled(true);return jedisPoolConfig;}@Beanpublic JedisPool jedisPool() {return new JedisPool(jedisPoolConfig(), "192.168.1.19", 6379, timeout);}
}
  • 创建测试类
@SpringBootTest
public class RedisOperationTest {@Autowiredprivate JedisPool jedisPool;@Testpublic void setValue() {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.setnx("tayun2", "1234");} catch (Exception e) {throw new RuntimeException("向Redis中存值失败!");} finally {if (jedis != null) {jedis.close();}}}
}

附:

1.Could not get a resource from the pool

报错信息截取:

org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

最终解决:

端口号写错导致,修改正确端口号正常

参考

  • Spring Data Redis

  • ClassNotFound找不到 redis.clients.jedis.JedisShardInfo

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/75539.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

扩展被恢复分区挡住的 C 盘

之前装系统的时候想着以后要不要装个 Ubuntu 以作备用,所以给 SSD 分区的时候留了一小部分,没有全分给 C 盘。结果后来用 WSL 用得乐不思蜀了,觉得剩下的空间留着没必要,于是想把剩下的空间扩容给 C 盘。结果操作的时候发现 C 盘后面跟了一个恢复分区!无法给 C 盘扩容了。…

若依开启注册功能

若依开启用户注册功能 1、修改数据库,如下:2、修改前端:参考鸣谢: https://blog.csdn.net/weixin_43684214/article/details/121609310

OCR视图识别(Tess4J)

1.概述 图片文字识别 OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程 2.Tess4j快速入门 1.导入依赖<dependencies><…

『模拟赛』多校A层冲刺NOIP2024模拟赛12

『模拟赛记录』多校A层冲刺NOIP2024模拟赛12Rank 挂了不少,还行A. Alice 和璀璨花 签。 一眼最长上升子序列,昨天在 AT 专题里刚见过,不过赛时没想到离散化之后树状数组,所以打的动态开点,结果细节挂了 30pts。 和最长上升子序列思路基本一致,直接区间查询 \([1,a_i-1]\)…

分享一些利用商品详情数据挖掘潜在需求的成功案例

以下是一些利用商品详情数据挖掘潜在需求的成功案例: 一、亚马逊的个性化推荐系统:案例背景:亚马逊是全球知名的电商平台,拥有海量的商品和庞大的用户群体。为了提高用户的购物体验和增加销售额,亚马逊投入大量资源开发个性化推荐系统。 数据挖掘过程:亚马逊通过分析用户…

抖音2024推文副业,欢迎来咨询

不管钱多钱少,俗话说:苍蝇再少也是肉 这个道理希望大家明白,想做就一起探讨,嫌少的也不用来了哈(只能做朋友,不能做副友),想做的欢迎来咨询想做收益多的可看这边文章(仅供参考) https://mp.weixin.qq.com/s?__biz=MzkxNTg0NDI4OA==&mid=2247483656&idx=1&am…

Zabbix添加企业微信机器人告警

环境查看 系统环境# cat /etc/redhat-release CentOS Stream release 9 # uname -a Linux CentOSStream9Zabbix203 5.14.0-391.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 28 20:35:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux软件环境 # zabbix_server --version zabbix_se…