在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Redis 过期监听场景业务中有类似等待一定时间之后执行某种行为的需求 , 比如 30 分钟之后关闭订单 . 网上有很多使用 Redis 过期监听的 Demo redis配置 把notify-keyspace-events Ex 这一行的注释打开 项目demo工程项目结构如下图 maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>kim-redis</artifactId> <groupId>com.kim</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>kim-redis-expiration-notice</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> </dependencies> </project> 配置文件 server: port: 20103 spring: redis: #数据库索引 database: 0 host: 127.0.0.1 port: 6379 password: 123456 lettuce: pool: #最大连接数 max-active: 8 #最大阻塞等待时间(负数表示没限制) max-wait: -1 #最大空闲 max-idle: 8 #最小空闲 min-idle: 0 #连接超时时间 timeout: 10000 启动类 /** * @Project: kim-redis * @PackageName: com.kim.redis.expiration.notice * @FileName: NoticeApplication.java * @Description: The NoticeApplication is... * @Author: kimwu * @Time: 2020-12-19 14:01:56 */ @SpringBootApplication public class NoticeApplication { public static void main(String[] args) { SpringApplication.run(NoticeApplication.class, args); } } 配置类 @Configuration public class RedisTimeoutConfiguration { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisMessageListenerContainer redisMessageListenerContainer() { RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory); return redisMessageListenerContainer; } @Bean public KeyExpiredListener keyExpiredListener() { return new KeyExpiredListener(this.redisMessageListenerContainer()); } } 监听类 @Slf4j public class KeyExpiredListener extends KeyExpirationEventMessageListener { public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String channel = new String(message.getChannel(), StandardCharsets.UTF_8); //过期的key String key = new String(message.getBody(), StandardCharsets.UTF_8); log.info("redis key 过期:pattern={},channel={},key={}", new String(pattern), channel, key); } } 异常情况测试当key过期时,项目宕机了 当key过期时,redis服务宕机了 结论redis的键过期本身不可靠,并不像rabbitmq一样保证了可靠性。 当使用场景对数据完整性不那么精确时,可以使用redis的键过期策略。否则不太建议使用redis的键过期策略。 到此这篇关于使用redis实现延迟通知功能(Redis过期键通知)的文章就介绍到这了,更多相关Redis过期键通知内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论