1、Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、application.yml配置redis
spring:
redis:
database: 0 # Redis数据库索引(配置自己用的)
database1: 1 # Redis数据库索引(配置自己用的)
host: 127.0.0.1 # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 8000 # 连接超时时间(毫秒)
3、RedisTemplateConfig配置
创建一个RedisTemplateConfig.class,这里配置了两个redis连接的database,没有特意配置两个连接池需要的配置,如果有需要可以自行添加,为了可以看到redis存储list、set等value,而且需要在接下来封装到StringUtils中,所以创建的是StringRedisTemlate
public class RedisTemplateConfig {
/** redis数据库*/
@Value("${spring.redis.database}")
private int redisDatabase;
/** redis数据库1*/
@Value("${spring.redis.database1}")
private int redis1Database;
/** redis数据库ip*/
@Value("${spring.redis.host}")
private String redisHost;
/** redis数据库端口*/
@Value("${spring.redis.port}")
private int redisPort;
@Bean(name = "redisTemplate0")
public StringRedisTemplate redisTemplate() {
return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(),redisDatabase));
}
@Bean(name = "redisTemplate1")
public StringRedisTemplate redisTemplate1() {
return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(),redis1Database));
}
protected StringRedisTemplate buildRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
template.setValueSerializer(stringRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
}
/**
* 连接池配置信息
* @return
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大连接数
jedisPoolConfig.setMaxTotal(100);
//最小空闲连接数
jedisPoolConfig.setMinIdle(20);
//当池内没有可用的连接时,最大等待时间
jedisPoolConfig.setMaxWaitMillis(10000);
return jedisPoolConfig;
}
/**
* jedis连接工厂
* @param jedisPoolConfig
* @param database
* @return
*/
public JedisConnectionFactory buildConnectionFactory(JedisPoolConfig jedisPoolConfig, int database) {
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
//设置redis服务器的host或者ip地址
redisStandaloneConfiguration.setHostName(redisHost);
//设置默认使用的数据库
redisStandaloneConfiguration.setDatabase(database);
//设置redis的服务的端口号
redisStandaloneConfiguration.setPort(redisPort);
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisBuilder=(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
jedisBuilder.poolConfig(jedisPoolConfig);
JedisClientConfiguration jedisClientConfiguration = jedisBuilder.build();
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
4、创建RedisUtils和Redis1Utils,分别注入config中声明的bean redisTemplate0和redisTemplate1
/**
* Redis工具类
*/
@Component
public class RedisUtils {
@Resource(name = "redisTemplate0")
private StringRedisTemplate redisTemplate;
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public StringRedisTemplate getRedisTemplate() {
return this.redisTemplate;
}
/** -------------------key相关操作--------------------- */
/**
* 删除key
*
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* 批量删除key
*
* @param keys
*/
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
/**
* 序列化key
*
* @param key
* @return
*/
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
/**
* 是否存在key
*
* @param key
* @return
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
}
@Component
public class Redis1Utils {
@Resource(name = "redisTemplate1")
private StringRedisTemplate redisTemplate;
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public StringRedisTemplate getRedisTemplate() {
return this.redisTemplate;
}
/** -------------------key相关操作--------------------- */
/**
* 删除key
*
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* 批量删除key
*
* @param keys
*/
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
/**
* 序列化key
*
* @param key
* @return
*/
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
/**
* 是否存在key
*
* @param key
* @return
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
}
RedisUtils操作太多了只截取了部分,主要展示通过@Resource注入了不同的StringRedisTemplate
5、Controller中的使用
根据个人需要各自注入,然后就可以分别操作不同的redis库了
@Autowired
private RedisUtils redisUtils;
@Autowired
private Redis1Utils redis1Utils;