前言
spring boot提供了spring-data-redis库来整合 Redis的操作,并通过简单的配置信息实现与Redis的整合。
PS:个人还是习惯于使用 Jedis 面向 Java 客户端操作 Redis
废话不多说,上代码。 😃
Maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Spring Boot项目里不用加版本号,加入 spring-boot-starter-data-redis 即可 -->
<!-- 非Spring Boot项目下,可在 maven repository 中选合适的版本号 -->
<!-- 面向java客户端Redis操作 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Jedis配置
########################## redis ###################################
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# Redis服务器连接端口
spring.redis.port=6379
# 连接超时时间(毫秒)
spring.redis.timeout=3000
# redis 连接池配置
# 池中最大链接数
spring.redis.pool-config.max-total=256
# 连接池中的最大空闲连接
spring.redis.pool-config.max-idle=3000
# 连接池中的最小空闲连接
spring.redis.pool-config.min-idle=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool-config.max-wait-millis=1000
# 调用者获取链接时,是否检测当前链接有效性
spring.redis.pool-config.test-on-borrow=false
# 向链接池中归还链接时,是否检测链接有效性
spring.redis.pool-config.test-on-return=false
# 调用者获取链接时,是否检测空闲超时, 如果超时,则会被移除
spring.redis.pool-config.test-while-idle=true
# 空闲链接检测线程一次运行检测多少条链接
spring.redis.pool-config.num-tests-per-eviction-run=8
# 空闲链接检测线程检测周期。如果为负值,表示不运行检测线程。(单位:毫秒,默认为-layer)
spring.redis.pool-config.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.redis.pool-config.min-evictable-idle-time-millis=300000
注:properties中的配置可根据项目实际情况自己调整,我不敢保证以上配置所有项目都适用
封装Properties Redis配置
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
private String host;
private String password;
private int port;
private int timeout;
// 此对象的句柄命名要和 Properties 中一一对应
// Properties 中为 “pool-config” 那么这里应为 “poolConfig”
private RedisPoolConfigProperties poolConfig = new RedisPoolConfigProperties();
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public RedisPoolConfigProperties getPoolConfig() {
return poolConfig;
}
public void setPoolConfig(RedisPoolConfigProperties poolConfig) {
this.poolConfig = poolConfig;
}
@Override
public String toString() {
return "RedisProperties{" +
"host='" + host + '\'' +
", password='" + password + '\'' +
", port=" + port +
", timeout=" + timeout +
", poolConfig=" + poolConfig +
'}';
}
}
// 该对象为 上面 RedisProperties 中的一个属性
public class RedisPoolConfigProperties {
private int maxTotal;
private int maxIdle;
private int minIdle;
private int maxWaitMillis;
private Boolean testOnBorrow;
private Boolean testOnReturn;
private Boolean testWhileIdle;
private int numTestsPerEvictionRun;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMaxIdle()