springboot整合redis過(guò)期key監(jiān)聽(tīng)實(shí)現(xiàn)訂單過(guò)期的項(xiàng)目實(shí)踐
業(yè)務(wù)場(chǎng)景說(shuō)明
對(duì)于訂單問(wèn)題,那些下單了但是沒(méi)有去支付的(占單情況),不管對(duì)于支付寶還是微信都有訂單的過(guò)期時(shí)間設(shè)置,但是對(duì)于我們自己維護(hù)的訂單呢。兩種方案:被動(dòng)修改,主動(dòng)修改。這里僅僅說(shuō)明對(duì)于主動(dòng)修改的監(jiān)聽(tīng)實(shí)現(xiàn)
修改redis的配置文件redis.conf(好像不改也可以)

K:keyspace事件,事件以__keyspace@<db>__為前綴進(jìn)行發(fā)布;
E:keyevent事件,事件以__keyevent@<db>__為前綴進(jìn)行發(fā)布;
g:一般性的,非特定類(lèi)型的命令,比如del,expire,rename等;
$:字符串特定命令;
l:列表特定命令;
s:集合特定命令;
h:哈希特定命令;
z:有序集合特定命令;
x:過(guò)期事件,當(dāng)某個(gè)鍵過(guò)期并刪除時(shí)會(huì)產(chǎn)生該事件;
e:驅(qū)逐事件,當(dāng)某個(gè)鍵因maxmemore策略而被刪除時(shí),產(chǎn)生該事件;
A:g$lshzxe的別名,因此”AKE”意味著所有事件
pom依賴坐標(biāo)的引入
<!--版本號(hào)說(shuō)明。這里我使用的是<jedis.version>2.9.3</jedis.version>,<spring.boot.version>2.3.0.RELEASE</spring.boot.version>-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring.boot.version}</version>
</dependency>
這里可能會(huì)存在的依賴沖突問(wèn)題是與io.netty
RedisConfig的配置
package test.bo.work.config.redis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import test.bo.work.util.StringUtil; /** * @author xiaobo */ @Configuration @EnableAutoConfiguration public class JedisConfig { private static final Logger LOGGER = LoggerFactory.getLogger(JedisConfig.class); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; @Value("${spring.redis.jedis.pool.max-wait}") private int maxWait; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}") private int minIdle; @Bean public JedisPool redisPoolFactory() { try { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWait); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); String pwd = StringUtil.isBlank(password) ? null : password; JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, pwd,6); LOGGER.info("初始化Redis連接池JedisPool成功!地址: " + host + ":" + port); return jedisPool; } catch (Exception e) { LOGGER.error("初始化Redis連接池JedisPool異常:" + e.getMessage()); } return null; } @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }監(jiān)聽(tīng)器實(shí)現(xiàn)
package test.bo.work.config.redis; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; import java.nio.charset.StandardCharsets; /** * @author xiaobo */ @Component @Slf4j public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); log.info("Redis Key Expiration Listener has been initialized."); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); log.error(expiredKey); // 判斷是否是想要監(jiān)聽(tīng)的過(guò)期key if (expiredKey.startsWith(KuoCaiConstants.ORDER_REDIS_PREFIX)) { // 根據(jù)過(guò)期redisKey獲取訂單號(hào) String transactionOrderId = expiredKey.substring(KuoCaiConstants.ORDER_REDIS_PREFIX.length()); transactionOrderService.updateTransactionOrderStatusByRedis(Long.valueOf(transactionOrderId)); } } }【注意】可能存在的問(wèn)題(監(jiān)聽(tīng)事件失效)
【警告】存在的問(wèn)題
自己自定義了庫(kù),如下代碼
@Override
protected void doRegister(RedisMessageListenerContainer listenerContainer) {
// 針對(duì)db6進(jìn)行監(jiān)聽(tīng)
listenerContainer.addMessageListener(this, new PatternTopic("__keyevent@6__:expired"));
}
這種情況,如果你存入的key不在db6,那么你就看不到監(jiān)聽(tīng)觸發(fā)事件(這里并不是失效,只是說(shuō)可能出現(xiàn)的你認(rèn)為失效的情況)
使用了默認(rèn)的連接工廠,但是配置文件中又沒(méi)有相關(guān)定義
首先解釋一下,默認(rèn)情況下,Spring Boot使用Jedis作為Redis客戶端,并且會(huì)自動(dòng)根據(jù)application.properties或application.yml配置文件中的
spring.redis屬性來(lái)創(chuàng)建連接工廠。如果沒(méi)有指定這些屬性,則會(huì)使用默認(rèn)的localhost:6379作為Redis服務(wù)器地址。如果你想要連接到其他的Redis服務(wù)器,可以在配置文件中設(shè)置
spring.redis.host和spring.redis.port屬性來(lái)指定Redis服務(wù)器的地址和端口號(hào)。

對(duì)于以上情況可以自定義工廠然后注入即可(上面的JedisConfig.java文件)
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setDatabase(db);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
redisStandaloneConfiguration.setPort(port);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisMessageListenerContainer container(JedisConnectionFactory jedisConnectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(jedisConnectionFactory);
return container;
}
Redis的Key事件通知機(jī)制默認(rèn)是異步的,即Redis會(huì)在Key過(guò)期時(shí)發(fā)送事件通知給所有監(jiān)聽(tīng)者,但是不能保證監(jiān)聽(tīng)者一定會(huì)及時(shí)接收到通知。如果您的應(yīng)用程序需要在Key過(guò)期后立即處理相關(guān)操作,可能需要使用其他方式來(lái)實(shí)現(xiàn)。
在Redis中,Key的過(guò)期時(shí)間只是一個(gè)近似的時(shí)間,它并不是精確的,因此不能保證過(guò)期時(shí)間到達(dá)時(shí)就一定會(huì)立即過(guò)期。如果您需要在Key過(guò)期后立即處理相關(guān)操作,建議您使用其他方式來(lái)實(shí)現(xiàn),例如使用定時(shí)任務(wù)或輪詢方式檢查過(guò)期Key。
在Redis中,Key的過(guò)期時(shí)間不能被取消或重置。如果您在設(shè)計(jì)時(shí)考慮到Key的過(guò)期時(shí)間可能需要修改,建議您使用其他方式來(lái)實(shí)現(xiàn)。
當(dāng)Redis中的Key被持久化到磁盤(pán)上時(shí),過(guò)期時(shí)間可能會(huì)受到影響,因?yàn)檫^(guò)期時(shí)間的計(jì)算是基于系統(tǒng)時(shí)間的,如果系統(tǒng)時(shí)間發(fā)生變化,過(guò)期時(shí)間可能會(huì)出現(xiàn)不準(zhǔn)確的情況。因此,建議您使用其他方式來(lái)處理需要精確過(guò)期時(shí)間的場(chǎng)景。
到此這篇關(guān)于springboot整合redis過(guò)期key監(jiān)聽(tīng)實(shí)現(xiàn)訂單過(guò)期的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)springboot redis過(guò)期key監(jiān)聽(tīng)訂單過(guò)期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)同步工具代碼分享
這篇文章主要介紹了使用redis實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)同步工具的代碼,大家參考使用吧2014-01-01
MyBatis?Plus?導(dǎo)入IdType失敗的解決
這篇文章主要介紹了MyBatis?Plus?導(dǎo)入IdType失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

