SpringBoot集成Jedis

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

前提条件:

  • 已部署单机redis
  • 已创建SpringBoot项目

操作步骤

1、standalone连接配置

  • 在Pom.xml文件中增加spring-boot-starter-data-redisjedis依赖
<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 {
    @Bean
    JedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("192.168.1.19", 6379); // 192.168.1.19为局域网单机redis主机IP
        return new JedisConnectionFactory(config);
    }

    @Bean
    RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
  • 创建测试类
@SpringBootTest
public class RedisOperationTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue() {
        redisTemplate.opsForValue().set("tayun", "1234");
    }
}

2、连接池配置

  • 创建application.yml文件,配置redis连接池属性
spring:
  redis:
    host: 192.168.1.19
    port: 6379
    database: 0
    jedis:
      pool:
        max-active: 10
        max-wait: 60
        max-idle: 10
        min-idle: 0
        max-wait-millis: 1500
    timeout: 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管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        return jedisPoolConfig;
    }

    @Bean
    public JedisPool jedisPool() {
        return new JedisPool(jedisPoolConfig(), "192.168.1.19", 6379, timeout);
    }
}
  • 创建测试类
@SpringBootTest
public class RedisOperationTest {
    @Autowired
    private JedisPool jedisPool;

    @Test
    public 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

最终解决:

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

参考

posted @   litayun  阅读(570)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示