springboot 集成redis有两种方式:
1.是集成redis官方推荐的jredis客户端;
2.springboot 自带的redisTemplate
一.集成 jredis
1.项目目录
引入依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
在resource文件夹下建立一个redis的配置文件redis.properties, 内容如下:
#主机和端口
redis-config.pool.hostAndPort=***.***.***.**:6379,***.***.***.**:6379,***.***.***.**:6379
#设置最大连接数,默认值为8.如果赋值为-1,则表示不限制;
redis-config.pool.maxTotal=100
#最大空闲连接数 默认值:8
redis-config.pool.maxIdle=10
#最小空闲连接数 默认值:0
redis-config.pool.minIdle=10
#当资源池连接用尽后,获取Jedis连接的最大等待时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
redis-config.pool.maxWaitMillis=10000
#连接空闲多久后释放, 当空闲时间大于该值 且 空闲连接大于最大空闲连接数 时直接释放, 默认值:-1L
redis-config.pool.softMinEvictableIdleTimeMillis=10000
#向资源池借用连接时,是否做连接有效性检测,无效连接会被移除,默认值:false ,业务量很大时建议为false,因为会多一次ping的开销
redis-config.pool.testOnBorrow=true
#向资源池归还连接时,是否做连接有效性检测,无效连接会被移除,默认值:false,业务量很大时建议为false,因为会多一次ping的开销
redis-config.pool.testOnReturn=true
#自动测试池中的空闲连接是否都是可用连接(是否开启空闲资源监测,默认值:false)
redis-config.pool.testWhileIdle=true
#当需要对空闲资源进行监测时, testWhileIdle 参数开启后与下列几个参数(timeBetweenEvictionRunsMillis minEvictableIdleTimeMillis numTestsPerEvictionRun )组合完成监测任务。
#空闲资源的检测周期,单位为毫秒,默认值:-1,表示不检测,建议设置一个合理的值,周期性运行监测任务
redis-config.pool.timeBetweenEvictionRunsMillis=30000
#资源池中资源最小空闲时间,单位为毫秒,默认值:30分钟(1000 60L 30L)=1800000,当达到该值后空闲资源将被移除,建议根据业务自身设定
redis-config.pool.minEvictableIdleTimeMillis=1800000
#做空闲资源检测时,每次的采样数,默认值:3,可根据自身应用连接数进行微调,如果设置为 -1,表示对所有连接做空闲监测
redis-config.pool.numTestsPerEvictionRun=3
#当资源池用尽后,调用者是否要等待(阻塞)。默认值:true,当为true时,maxWaitMillis参数才会生效,建议使用默认值, false会报异常
redis-config.pool.blockWhenExhausted=true
#是否启用pool的jmx管理功能, 默认true
redis-config.pool.jmxEnabled=true
#lifo 资源池里放池对象的方式,LIFO Last In First Out 后进先出,true(默认值),表示放在空闲队列最前面,false:放在空闲队列最后面
redis-config.pool.lifo=true
读取配置文件到RedisPoolProperties对象
package com.study.redisdemo.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @author yangmin
* @deprecated redis连接池的配置信息, 具体的配置文件:redis.properties
* @date 2021.01.19
*/
@Configuration
@PropertySource(value = "classpath:redis.properties")
@ConfigurationProperties(prefix = "redis-config.pool")
public class RedisPoolProperties {
private String hostAndPort;
private int maxTotal;
private int maxIdle;
private int minIdle;
private Long maxWaitMillis;
private Long timeBetweenEvictionRunsMillis;
private Long minEvictableIdleTimeMillis;
private Long softMinEvictableIdleTimeMillis;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean testWhileIdle;
private boolean blockWhenExhausted;
private boolean jmxEnabled;
private boolean lifo;
private int numTestsPerEvictionRun;
public String getHostAndPort() {
return hostAndPort;
}
public void setHostAndPort(String hostAndPort) {
this.hostAndPort = hostAndPort;
}
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public Long getMaxWaitMillis() {
return maxWaitMillis;
}
public void setMaxWaitMillis(Long maxWaitMillis) {
this.maxWaitMillis = maxWaitMillis;
}
public Long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public Long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public Long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleTimeMillis;
}
public void setSoftMinEvictableIdleTimeMillis(Long softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public boolean isBlockWhenExhausted() {
return blockWhenExhausted;
}
public void setBlockWhenExhausted(boolean blockWhenExhausted) {
this.blockWhenExhausted = blockWhenExhausted;
}
public boolean isJmxEnabled() {
return jmxEnabled;
}
public void setJmxEnabled(boolean jmxEnabled) {
this.jmxEnabled = jmxEnabled;
}
public boolean isLifo() {
return lifo;
}
public void setLifo(boolean lifo) {
this.lifo = lifo;
}
public int getNumTestsPerEvictionRun() {
return numTestsPerEvictionRun;
}
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
}
}
redispool配置信息
package com.study.redisdemo.config;
import com.study.redisdemo.properties.RedisPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
/**
* @author yangmin
* @deprecated redis连接池配置
*/
@Configuration
@EnableConfigurationProperties({RedisPoolProperties.class})
public class RedisPoolConfig {
@Autowired
private RedisPoolProperties redisPoolProperties;
/**
* 连接池的基本配置
*
* @return JedisPoolConfig
*/
@Bean
public JedisPoolConfig initPoolConfig() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 设置最大连接数,默认值为8.如果赋值为-1,则表示不限制;
poolConfig.setMaxTotal(redisPoolProperties.getMaxTotal());
// 最大空闲连接数
poolConfig.setMaxIdle(redisPoolProperties.getMaxIdle());
// 最小空闲连接数
poolConfig.setMinIdle(redisPoolProperties.getMinIdle());
// 获取Jedis连接的最大等待时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
poolConfig.setMaxWaitMillis(redisPoolProperties.getMaxWaitMillis());
// 每次释放连接的最大数目
poolConfig.setNumTestsPerEvictionRun(redisPoolProperties.getNumTestsPerEvictionRun());
// 释放连接的扫描间隔(毫秒),如果为负数,则不运行逐出线程, 默认-1
poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolProperties.getTimeBetweenEvictionRunsMillis());
// 连接最小空闲时间
poolConfig.setMinEvictableIdleTimeMillis(redisPoolProperties.getMinEvictableIdleTimeMillis());
// 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放
poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolProperties.getSoftMinEvictableIdleTimeMillis());
// 在获取Jedis连接时,自动检验连接是否可用
poolConfig.setTestOnBorrow(redisPoolProperties.isTestOnBorrow());
// 在将连接放回池中前,自动检验连接是否有效
poolConfig.setTestOnReturn(redisPoolProperties.isTestOnReturn());
// 自动测试池中的空闲连接是否都是可用连接
poolConfig.setTestWhileIdle(redisPoolProperties.isTestWhileIdle());
// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
poolConfig.setBlockWhenExhausted(redisPoolProperties.isBlockWhenExhausted());
// 是否启用pool的jmx管理功能, 默认true
poolConfig.setJmxEnabled(redisPoolProperties.isJmxEnabled());
// 是否启用后进先出, 默认true
poolConfig.setLifo(redisPoolProperties.isLifo());
// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
poolConfig.setNumTestsPerEvictionRun(redisPoolProperties.getNumTestsPerEvictionRun());
return poolConfig;
}
/**
* 创建JedisCluster客户端, 将redis客户端放到容器中; 连接redis集群
* Jedis 连接集群
*
* @return JedisCluster 使用完成后不需要手动释放连接,返回客户端, 使用完成后不需要手动释放连接, 客户端会自动释放连接
*/
@Bean
@Qualifier("JedisCluster")
public JedisCluster getJedisCluster() {
return new JedisCluster(getSet(), initPoolConfig());
}
/**
* 单价版 需要手动获取jedis实例,用完后需要手动释放
*
* @return 返回redis连接池,
*/
@Bean
public JedisPool getRedisPool() {
return new JedisPool(initPoolConfig(), "127.0.0.1", 6379);
}
/**
* 获取集群对象集合
*
* @return Set
*/
public Set<HostAndPort> getSet() {
String hostAndPortStr = redisPoolProperties.getHostAndPort();
String[] hostAndPortArrays = hostAndPortStr.trim().split(",");
Set<HostAndPort> hostAndPorts = new HashSet<>();
for (String hostAndPort : hostAndPortArrays) {
hostAndPorts.add(new HostAndPort(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1])));
}
return hostAndPorts;
}
}
测试代码演示
package com.study.redisdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisdemoApplicationTests {
@Autowired
private JedisCluster jedisCluster;
@Autowired
private JedisPool jedisPool;
/**
* 集群版测试
*/
@Test
public void contextLoads() {
jedisCluster.set("name", "woyaoce");
System.out.println(jedisCluster.get("name"));
jedisCluster.del("name");
}
/**
* 单机版测试
*/
@Test
public void testJedisPool() {
//从连接池中获取实例
Jedis jedis = jedisPool.getResource();
//操作redis
jedis.set("name", "yangmin");
System.out.println(jedis.get("name"));
//释放连接
jedis.close();
}
}
二. springboot 自带的redisTemplate(不建议使用)
更多推荐