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