一.依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
二.配置文件
properties
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 连接超时时间(毫秒)
spring.redis.timeout=10000
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=60000
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
yml
spring:
# redis
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器地址
host: localhost
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password: 123456
# 连接超时时间(毫秒)
timeout: 1000
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 5000
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
三.编写配置类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@Slf4j
public class RedisConfig {
@Value("${spring.redis.database:0}")
private int database; //Redis数据库索引(默认为0)
@Value("${spring.redis.host}")
private String host; //Redis服务器地址
@Value("${spring.redis.port}")
private int port; //Redis服务器连接端口
@Value("${spring.redis.password}")
private String password; //Redis服务器连接密码(默认为空)
@Value("${spring.redis.timeout}")
private int timeOut; //连接超时时间(毫秒)
@Value("${spring.redis.jedis.pool.max-active:8}")
private int maxActive; //连接池最大连接数(使用负值表示没有限制)
@Value("${spring.redis.jedis.pool.max-wait:-1}")
private int maxWaitMillis; //连接池最大阻塞等待时间(使用负值表示没有限制)
@Value("${spring.redis.jedis.pool.max-idle:8}")
private int maxIdle; //连接池中的最大空闲连接
@Value("${spring.redis.jedis.pool.min-idle:0}")
private int minIdle; //连接池中的最小空闲连接
@Bean
public JedisPool jedisPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxWaitMillis(maxWaitMillis);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
JedisPool jedisPool = new JedisPool(config,host,port,timeOut,password,database);
log.info("redis连接池创建成功");
log.info("redis地址:{}:{};数据库索引:{}",host,port,database);
return jedisPool;
}
}
四.工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
import java.util.Collections;
@Component
@Slf4j
public class JedisClient {
@Autowired
JedisPool jedisPool;
// ================================String================================
public void set(String key, String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
}finally {
jedis.close();
}
}
// =================================Hash=================================
//各种操作......
}