简单搭建web项目,使用技术包括:SpringBoot、MyBatis Plus、Redis
Maven依赖
com.baomidou
mybatis-plus-boot-starter
3.1.1
com.alibaba
druid
1.1.9
mysql
mysql-connector-java
8.0.11
redis.clients
jedis
3.3.0
org.springframework.data
spring-data-redis
2.2.7.RELEASE
org.apache.commons
commons-pool2
2.6.1
com.alibaba
fastjson
1.2.68
配置application.properties
编写Redis配置类RedisSourceConfig
package com.wwy.configuraion; import com.wwy.api.RedisSource; 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.connection.*; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisSentinelConnection; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; import redis.clients.jedis.JedisPoolConfig; import java.util.HashSet; import java.util.List; import java.util.Set; @Configuration public class RedisSourceConfig { // 多个哨兵 @Value("#{'${redis.sentinel.nodes}'.split(',')}") private List
https://www.cnblogs.com/maybesuch/p/10257374.htmlnodes; @Value("${redis.host}") private String host; @Value("${redis.port}") private String port; @Value("${redis.db}") private String db; @Autowired private RedisTemplate redisTemplate; // 配置链接池信息 @Bean public JedisPoolConfig poolConfig() { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(8); poolConfig.setMaxTotal(8); poolConfig.setBlockWhenExhausted(true); poolConfig.setMaxWaitMillis(30000); poolConfig.setTestOnBorrow(true); poolConfig.setTestOnReturn(true); poolConfig.setTimeBetweenEvictionRunsMillis(1200000); poolConfig.setMinEvictableIdleTimeMillis(1800000); return poolConfig; } // 哨兵模式配置 (如果没有用哨兵,请注释) @Bean public RedisSentinelConfiguration redisSentinelConfiguration(){ RedisSentinelConfiguration sentinel = new RedisSentinelConfiguration(); sentinel.setMaster("mymaster"); Set sentinels = new HashSet<>(); nodes.forEach(li ->{ li.toString().split(":"); // 多个哨兵 sentinels.add(new RedisNode(li.split(":")[0],Integer.parseInt(li.split(":")[1]))); }); sentinel.setSentinels(sentinels); return sentinel; } // Redis链接工厂 @Bean public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) { // 方式一:使用哨兵模式(根据具体情况,选择其中一种即可,可看JedisConnectionFactory 源码,有多种链接方式,如Cluster Redis集群等) JedisConnectionFactory jedisFactory = new JedisConnectionFactory(redisSentinelConfiguration(),jedisPoolConfig);// 配置哨兵 // Redis哨兵搭建可参考: // 方式二:在这没有使用哨兵模式,可以理解为单机根据具体情况,选择其中一种即可,可看JedisConnectionFactory 源码,有多种链接方式,如Cluster Redis集群等) //JedisConnectionFactory jedisFactory = new JedisConnectionFactory(); // 工厂链接信息 jedisFactory.setPoolConfig(jedisPoolConfig); //jedisFactory.setHostName(host); // 配置哨兵无需设置 //jedisFactory.setPort(Integer.parseInt(port)); // 配置哨兵无需设置 //jedisFactory.setPassword("redis@20191210"); // 在这我是本地,没有设置密码 jedisFactory.setUsePool(true); jedisFactory.setDatabase(Integer.parseInt(db)); return jedisFactory; } // 封装Redis的各种操作,如果没有写此方法,在使用private RedisTemplate
redisTemplate;会注入失败的
@Bean
public RedisTemplate stringSerializerRedisTemplate() {
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
}
Redis key过期事件监听,配置RedisListenerConfig
1、修改redis server 配置文件,开启keyt过期听通知配置
2、redis监听配置
3、单独全库redis key过期处理
4、监听redis某个库的过期key,此种方式需要在第2步中的配置中进行配置