一、修改xml配置文件pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
二、修改配置文件application.yml 文件
spring:
#redis配置信息
redis:
#redis 服务器地址
hostName: 192.168.253.130
#redis端口
port: 6379
#redis 密码
password: 123456
#客户端超时时间单位是毫秒 默认是2000
timeout: 5000
#最大空闲数
maxIdle: 20
#连接池的最大数据库连接数
maxActive: -1
#控制一个pool可分配多少个jedis实例,用来替换上面的maxActive
maxTotal: 100
#最大建立连接等待时间。如果超过此时间将接到异常
maxWaitMillis: 100
#连接的最小空闲时间
minEvictableIdleTimeMillis: 864000000
#每次释放连接的最大数目
numTestsPerEvictionRun: 10
#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程
timeBetweenEvictionRunsMillis: 300000
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
testOnBorrow: true
#在空闲时检查有效性
testWhileIdle: false
#数据库
database: 0
三、增加RedisConfig.java配置类
package com.example.redis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.nio.charset.Charset;
@Configuration
public class RedisConfig {
@Value("${spring.redis.hostName}")
private String hostName;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private Integer timeout;
@Value("${spring.redis.maxIdle}")
private Integer maxIdle;
@Value("${spring.redis.maxActive}")
private String maxActive;
@Value("${spring.redis.maxTotal}")
private Integer maxTotal;
@Value("${spring.redis.maxWaitMillis}")
private Long maxWaitMillis;
@Value("${spring.redis.minEvictableIdleTimeMillis}")
private Long minEvictableIdleTimeMillis;
@Value("${spring.redis.numTestsPerEvictionRun}")
private Integer numTestsPerEvictionRun;
@Value("${spring.redis.timeBetweenEvictionRunsMillis}")
private Long timeBetweenEvictionRunsMillis;
@Value("${spring.redis.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.redis.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.redis.database}")
private Integer database;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
jedisPoolConfig.setTestWhileIdle(testWhileIdle);
return jedisPoolConfig;
}
@Bean
public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(hostName);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(password);
redisStandaloneConfiguration.setDatabase(database);
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcf = (JedisClien