SpringBoot2之后,默认采用Lettuce作为redis的连接客户端,当然我们还是可以强制捡回来,使用我们熟悉的Jedis的,本篇简单介绍下使用Jedis的相关配置
I. 基本配置
1. 依赖
使用Jedis与Lettuce不同的是,需要额外的引入Jedis包的依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> 复制代码
2. 配置
redis的相关配置,和前面的差不多,只是线程池的参数稍稍有点区别
spring: redis: host: 127.0.0.1 port: 6379 password: database: 0 jedis: pool: max-idle: 6 max-active: 32 max-wait: 100 min-idle: 4 复制代码
3. AutoConfig
与前面不同的是,我们需要定义一个RedisConnectionFactory
的bean作为默认的连接工厂,以此来确定底层的连接采用的是Jedis客户端
@Configuration public class RedisAutoConfig { @Bean public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPool, RedisStandaloneConfiguration jedisConfig) { JedisConnectionFactory connectionFactory = new JedisConnectionFactory(jedisConfig); connectionFactory.setPoolConfig(jedisPool); return connectionFactory; } @Configuration public static class JedisConf { @Value("${spring.redis.host:127.0.0.1}") private String host; @Value("${spring.redis.port:6379}") private Integer port; @Value("${spring.redis.password:}") private String password; @Value("${spring.redis.database:0}") private Integer database; @Value("${spring.redis.jedis.pool.max-active:8}") private Integer maxActive; @Value("${spring.redis.jedis.pool.max-idle:8}") private Integer maxIdle; @Value("${spring.redis.jedis.pool.max-wait:-1}") private Long maxWait; @Value("${spring.redis.jedis.pool.min-idle:0}") private Integer minIdle; @Bean public JedisPoolConfig jedisPool() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWait); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); return jedisPoolConfig; } @Bean public RedisStandaloneConfiguration jedisConfig() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName(host); config.setPort(port); config.setDatabase(database); config.setPassword(RedisPassword.of(password)); return config; } } } 复制代码
4. 测试
测试主要就是查看下RedisTemplate的连接工厂类,到底是啥,简单的是截图如下