更多Spring文章,歡迎點擊 一灰灰Blog-Spring專題java
SpringBoot2以後,默認採用Lettuce做爲redis的鏈接客戶端,固然咱們仍是能夠強制撿回來,使用咱們熟悉的Jedis的,本篇簡單介紹下使用Jedis的相關配置git
原文連接: 181101-SpringBoot高級篇Redis之Jedis配置github
使用Jedis與Lettuce不一樣的是,須要額外的引入Jedis包的依賴redis
<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>
複製代碼
redis的相關配置,和前面的差很少,只是線程池的參數稍稍有點區別spring
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
複製代碼
與前面不一樣的是,咱們須要定義一個RedisConnectionFactory
的bean做爲默認的鏈接工廠,以此來肯定底層的鏈接採用的是Jedis客戶端apache
@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;
}
}
}
複製代碼
測試主要就是查看下RedisTemplate的鏈接工廠類,究竟是啥,簡單的是截圖以下spring-boot
一灰灰的我的博客,記錄全部學習和工做中的博文,歡迎你們前去逛逛學習
盡信書則不如,以上內容,純屬一家之言,因我的能力有限,不免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激測試
一灰灰blogui
知識星球