这个东西之前配置过,今天本来记录一下,加深印象,结果忙了一下午,问题出在redis的配置中,即将崩溃,这里提个醒,redis的配置文件中,对于每一行配置前面别有空格,正确做法一直顶到头,这个真是恶心的配置,一点都不智能,因为开启key过期要修改配置文件,所以在修改的时候别忘了这个坑。
我使用的windows版本的redis为Redis-x64-3.2.100,使用的是.msi版本的,使用.zip有点问题,也有可能你的没有问题。
redis下载链接:https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100
1、依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
2、文件配置
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=2000
spring.redis.timeout=1000
spring.redis.lettuce.pool.MaxTotal=10
spring.redis.lettuce.pool.minIdle=1
spring.redis.lettuce.pool.maxWaitMillis=50000
spring.redis.lettuce.pool.maxIdle=5
spring.redis.lettuce.pool.testOnBorrow=true
spring.redis.lettuce.pool.testOnReturn=true
spring.redis.lettuce.pool.testWhileIdle=true
3、代码配置
@Configuration
public class RedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
public GenericObjectPoolConfig redisPool() {
return new GenericObjectPoolConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisStandaloneConfiguration redisConfig() {
return new RedisStandaloneConfiguration();
}
@Bean
@Primary
public LettuceConnectionFactory connectionFactory(GenericObjectPoolConfig redisPool, RedisStandaloneConfiguration redisConfig) {
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).commandTimeout(Duration.ofMillis(redisPool.getMaxWaitMillis())).build();
return new LettuceConnectionFactory(redisConfig, clientConfiguration);
}
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(LettuceConnectionFactory connectionFactory, MessageListenerAdapter redisMsgListener) {
RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
listenerContainer.setConnectionFactory(connectionFactory);
listenerContainer.addMessageListener(redisMsgListener, new PatternTopic("__keyevent@0__:*"));
return listenerContainer;
}
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(connectionFactory);
return stringRedisTemplate;
}
}
4、配置监听器,后续业务处理
@Component
public class RedisMsgListener extends MessageListenerAdapter {
@Override
public void onMessage(Message message, byte[] pattern) {
String topic = new String(pattern);
RedisSerializer<String> string = RedisSerializer.string();
String deserialize = string.deserialize(message.getBody());
System.out.println("topic名称:" + message.getChannel());
System.out.println("Msg:" + deserialize);
}
}
5、修改redis配置,重启
redis.windows-service.conf
notify-keyspace-events Ex
6、测试
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/push")
public void push(String lee) {
stringRedisTemplate.opsForValue().set(lee, String.valueOf(System.nanoTime()), 3L, TimeUnit.SECONDS);
}
3秒之后,打印如下:
如果能帮到你,麻烦点个赞哦!